// Set.cpp // $Id$ #if !defined (ACE_SET_C) #define ACE_SET_C #define ACE_BUILD_DLL #include "ace/Set.h" #if !defined (__ACE_INLINE__) #include "ace/Set.i" #endif /* __ACE_INLINE__ */ ACE_ALLOC_HOOK_DEFINE(ACE_Fixed_Set) template void ACE_Fixed_Set::dump (void) const { ACE_TRACE ("ACE_Fixed_Set::dump"); } template ACE_Fixed_Set::~ACE_Fixed_Set (void) { ACE_TRACE ("ACE_Fixed_Set::~ACE_Fixed_Set"); this->cur_size_ = 0; } template ACE_Fixed_Set::ACE_Fixed_Set (void) : cur_size_ (0), max_size_ (SIZE) { ACE_TRACE ("ACE_Fixed_Set::ACE_Fixed_Set"); for (size_t i = 0; i < this->max_size_; i++) this->search_structure_[i].is_free_ = 1; } template int ACE_Fixed_Set::find (const T &item) const { ACE_TRACE ("ACE_Fixed_Set::find"); for (size_t i = 0; i < this->cur_size_; i++) { if (this->search_structure_[i].item_ == item && this->search_structure_[i].is_free_ == 0) return 1; } return 0; } template int ACE_Fixed_Set::insert (const T &item) { ACE_TRACE ("ACE_Fixed_Set::insert"); int first_free = -1; // Keep track of first free slot. size_t i; for (i = 0; i < this->cur_size_; i++) { // First, make sure we don't allow duplicates. if (this->search_structure_[i].item_ == item && this->search_structure_[i].is_free_ == 0) return 1; else if (this->search_structure_[i].is_free_ && first_free == -1) first_free = i; } // If we found a free spot let's reuse it. if (first_free > -1) { this->search_structure_[first_free].item_ = item; this->search_structure_[first_free].is_free_ = 0; return 0; } // Insert at the end of the active portion. else if (i < this->max_size_) { this->search_structure_[i].item_ = item; this->search_structure_[i].is_free_ = 0; this->cur_size_++; return 0; } else /* No more room! */ { errno = ENOMEM; return -1; } } template int ACE_Fixed_Set::remove (const T &item) { ACE_TRACE ("ACE_Fixed_Set::remove"); for (size_t i = 0; i < this->cur_size_; i++) { if (this->search_structure_[i].item_ == item) { // Mark this entry as being free. this->search_structure_[i].is_free_ = 1; // If we just unbound the highest entry, then we need to // figure out where the next highest active entry is. if (i + 1 == this->cur_size_) { while (i > 0 && this->search_structure_[--i].is_free_) continue; if (i == 0 && this->search_structure_[i].is_free_) this->cur_size_ = 0; else this->cur_size_ = i + 1; } return 1; } } return 0; } ACE_ALLOC_HOOK_DEFINE(ACE_Fixed_Set_Iterator) template void ACE_Fixed_Set_Iterator::dump (void) const { ACE_TRACE ("ACE_Fixed_Set_Iterator::dump"); } template ACE_Fixed_Set_Iterator::ACE_Fixed_Set_Iterator (ACE_Fixed_Set &s) : s_ (s), next_ (-1) { ACE_TRACE ("ACE_Fixed_Set_Iterator::ACE_Fixed_Set_Iterator"); this->advance (); } template int ACE_Fixed_Set_Iterator::advance (void) { ACE_TRACE ("ACE_Fixed_Set_Iterator::advance"); for (++this->next_; size_t (this->next_) < this->s_.cur_size_ && this->s_.search_structure_[this->next_].is_free_; ++this->next_) continue; return this->next_; } template int ACE_Fixed_Set_Iterator::next (T *&item) { ACE_TRACE ("ACE_Fixed_Set_Iterator::next"); if (size_t (this->next_) < this->s_.cur_size_) { item = &this->s_.search_structure_[this->next_].item_; return 1; } else return 0; } ACE_ALLOC_HOOK_DEFINE(ACE_Bounded_Set) template void ACE_Bounded_Set::dump (void) const { ACE_TRACE ("ACE_Bounded_Set::dump"); } template ACE_Bounded_Set::~ACE_Bounded_Set (void) { ACE_TRACE ("ACE_Bounded_Set::~ACE_Bounded_Set"); delete [] this->search_structure_; } template ACE_Bounded_Set::ACE_Bounded_Set (void) : cur_size_ (0), max_size_ (size_t (ACE_Bounded_Set::DEFAULT_SIZE)) { ACE_TRACE ("ACE_Bounded_Set::ACE_Bounded_Set"); ACE_NEW (this->search_structure_, ACE_Bounded_Set::Search_Structure[this->max_size_]); for (size_t i = 0; i < this->max_size_; i++) this->search_structure_[i].is_free_ = 1; } template ACE_Bounded_Set::ACE_Bounded_Set (size_t size) : cur_size_ (0), max_size_ (size) { ACE_TRACE ("ACE_Bounded_Set::ACE_Bounded_Set"); ACE_NEW (this->search_structure_, ACE_Bounded_Set::Search_Structure[size]); for (size_t i = 0; i < this->max_size_; i++) this->search_structure_[i].is_free_ = 1; } template int ACE_Bounded_Set::find (const T &item) const { ACE_TRACE ("ACE_Bounded_Set::find"); for (size_t i = 0; i < this->cur_size_; i++) { if (this->search_structure_[i].item_ == item && this->search_structure_[i].is_free_ == 0) return 1; } return 0; } template int ACE_Bounded_Set::insert (const T &item) { ACE_TRACE ("ACE_Bounded_Set::insert"); int first_free = -1; // Keep track of first free slot. size_t i; for (i = 0; i < this->cur_size_; i++) { // First, make sure we don't allow duplicates. if (this->search_structure_[i].item_ == item && this->search_structure_[i].is_free_ == 0) return 1; else if (this->search_structure_[i].is_free_ && first_free == -1) first_free = i; } if (first_free > -1) // If we found a free spot let's reuse it. { this->search_structure_[first_free].item_ = item; this->search_structure_[first_free].is_free_ = 0; return 0; } else if (i < this->max_size_) // Insert at the end of the active portion. { this->search_structure_[i].item_ = item; this->search_structure_[i].is_free_ = 0; this->cur_size_++; return 0; } else /* No more room! */ { errno = ENOMEM; return -1; } } template int ACE_Bounded_Set::remove (const T &item) { ACE_TRACE ("ACE_Bounded_Set::remove"); for (size_t i = 0; i < this->cur_size_; i++) { if (this->search_structure_[i].item_ == item) { // Mark this entry as being free. this->search_structure_[i].is_free_ = 1; // If we just unbound the highest entry, then we need to // figure out where the next highest active entry is. if (i + 1 == this->cur_size_) { while (i > 0 && this->search_structure_[--i].is_free_) continue; if (i == 0 && this->search_structure_[i].is_free_) this->cur_size_ = 0; else this->cur_size_ = i + 1; } return 1; } } return 0; } ACE_ALLOC_HOOK_DEFINE(ACE_Bounded_Set_Iterator) template void ACE_Bounded_Set_Iterator::dump (void) const { ACE_TRACE ("ACE_Bounded_Set_Iterator::dump"); } template ACE_Bounded_Set_Iterator::ACE_Bounded_Set_Iterator (ACE_Bounded_Set &s) : s_ (s), next_ (-1) { ACE_TRACE ("ACE_Bounded_Set_Iterator::ACE_Bounded_Set_Iterator"); this->advance (); } template int ACE_Bounded_Set_Iterator::advance (void) { ACE_TRACE ("ACE_Bounded_Set_Iterator::advance"); for (++this->next_; size_t (this->next_) < this->s_.cur_size_ && this->s_.search_structure_[this->next_].is_free_; ++this->next_) continue; return this->next_; } template int ACE_Bounded_Set_Iterator::next (T *&item) { ACE_TRACE ("ACE_Bounded_Set_Iterator::next"); if (size_t (this->next_) < this->s_.cur_size_) { item = &this->s_.search_structure_[this->next_].item_; return 1; } else return 0; } ACE_ALLOC_HOOK_DEFINE(ACE_Set_Node) template class ACE_Set_Node { friend class ACE_Unbounded_Set; friend class ACE_Unbounded_Set_Iterator; public: // = Initialization methods ACE_Set_Node (const T &i, ACE_Set_Node *n); ACE_Set_Node (ACE_Set_Node *n = 0); ACE_Set_Node (const ACE_Set_Node &n); private: ACE_Set_Node *next_; // Pointer to next element in the list of ACE_Set_Nodes. T item_; // Current value of the item in this node. }; template ACE_Set_Node::ACE_Set_Node (const T &i, ACE_Set_Node *n) : next_ (n), item_ (i) { // ACE_TRACE ("ACE_Set_Node::ACE_Set_Node"); } template ACE_Set_Node::ACE_Set_Node (ACE_Set_Node *n) : next_ (n) { // ACE_TRACE ("ACE_Set_Node::ACE_Set_Node"); } template ACE_Set_Node::ACE_Set_Node (const ACE_Set_Node &s) : next_ (s.next_), item_ (s.item_) { // ACE_TRACE ("ACE_Set_Node::ACE_Set_Node"); } ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set) template void ACE_Unbounded_Set::dump (void) const { // ACE_TRACE ("ACE_Unbounded_Set::dump"); } template ACE_Unbounded_Set::~ACE_Unbounded_Set (void) { // ACE_TRACE ("ACE_Unbounded_Set::~ACE_Unbounded_Set"); while (this->head_ != 0) { ACE_Set_Node *temp = this->head_; this->head_ = this->head_->next_; this->cur_size_--; delete temp; } } template int ACE_Unbounded_Set::find (const T &item) const { // ACE_TRACE ("ACE_Unbounded_Set::find"); for (ACE_Set_Node *temp = this->head_; temp != 0; temp = temp->next_) if (temp->item_ == item) return 1; return 0; } template ACE_Unbounded_Set::ACE_Unbounded_Set (void) : head_ (0), cur_size_ (0) { // ACE_TRACE ("ACE_Unbounded_Set::ACE_Unbounded_Set"); } template int ACE_Unbounded_Set::insert (const T &item) { // ACE_TRACE ("ACE_Unbounded_Set::insert"); if (this->find (item) == 0) { ACE_NEW_RETURN (this->head_, ACE_Set_Node (item, this->head_), -1); this->cur_size_++; return 0; } else return 1; } template int ACE_Unbounded_Set::remove (const T &item) { // ACE_TRACE ("ACE_Unbounded_Set::remove"); ACE_Set_Node *prev = 0; ACE_Set_Node *temp; for (temp = this->head_; temp != 0; temp = temp->next_) { if (temp->item_ == item) break; prev = temp; } if (temp == 0) return 0; else if (prev == 0) // Deleting the front of the list. this->head_ = this->head_->next_; else prev->next_ = temp->next_; this->cur_size_--; delete temp; return 1; } ACE_ALLOC_HOOK_DEFINE(ACE_Unbounded_Set_Iterator) template void ACE_Unbounded_Set_Iterator::dump (void) const { // ACE_TRACE ("ACE_Unbounded_Set_Iterator::dump"); } template ACE_Unbounded_Set_Iterator::ACE_Unbounded_Set_Iterator (ACE_Unbounded_Set &s) : current_ (s.head_) { // ACE_TRACE ("ACE_Unbounded_Set_Iterator::ACE_Unbounded_Set_Iterator"); } template int ACE_Unbounded_Set_Iterator::advance (void) { // ACE_TRACE ("ACE_Unbounded_Set_Iterator::advance"); this->current_ = this->current_->next_; return 1; } template int ACE_Unbounded_Set_Iterator::next (T *&item) { // ACE_TRACE ("ACE_Unbounded_Set_Iterator::next"); if (this->current_ == 0) return 0; else { item = &this->current_->item_; return 1; } } #endif /* ACE_SET_C */