Fixed typos
[ACE_TAO.git] / ACE / ace / Containers_T.cpp
blob4fa39569b461fbca46f726d189bd6d552157fa74
1 #ifndef ACE_CONTAINERS_T_CPP
2 #define ACE_CONTAINERS_T_CPP
4 #include "ace/Log_Category.h"
5 #include "ace/Malloc_Base.h"
6 #include "ace/OS_Memory.h"
8 #if !defined (ACE_LACKS_PRAGMA_ONCE)
9 # pragma once
10 #endif /* ACE_LACKS_PRAGMA_ONCE */
12 #include "ace/Containers.h"
14 #if !defined (__ACE_INLINE__)
15 #include "ace/Containers_T.inl"
16 #endif /* __ACE_INLINE__ */
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Bounded_Stack)
22 template <class T> void
23 ACE_Bounded_Stack<T>::dump (void) const
25 #if defined (ACE_HAS_DUMP)
26 ACE_TRACE ("ACE_Bounded_Stack<T>::dump");
27 #endif /* ACE_HAS_DUMP */
30 template<class T>
31 ACE_Bounded_Stack<T>::ACE_Bounded_Stack (size_t size)
32 : size_ (size),
33 top_ (0)
35 ACE_NEW (this->stack_,
36 T[size]);
37 ACE_TRACE ("ACE_Bounded_Stack<T>::ACE_Bounded_Stack");
40 template<class T>
41 ACE_Bounded_Stack<T>::ACE_Bounded_Stack (const ACE_Bounded_Stack<T> &s)
42 : size_ (s.size_),
43 top_ (s.top_)
45 ACE_NEW (this->stack_,
46 T[s.size_]);
48 ACE_TRACE ("ACE_Bounded_Stack<T>::ACE_Bounded_Stack");
50 for (size_t i = 0; i < this->top_; i++)
51 this->stack_[i] = s.stack_[i];
54 template<class T> void
55 ACE_Bounded_Stack<T>::operator= (const ACE_Bounded_Stack<T> &s)
57 ACE_TRACE ("ACE_Bounded_Stack<T>::operator=");
59 if (&s != this)
61 if (this->size_ < s.size_)
63 delete [] this->stack_;
64 ACE_NEW (this->stack_,
65 T[s.size_]);
66 this->size_ = s.size_;
68 this->top_ = s.top_;
70 for (size_t i = 0; i < this->top_; i++)
71 this->stack_[i] = s.stack_[i];
75 template<class T>
76 ACE_Bounded_Stack<T>::~ACE_Bounded_Stack (void)
78 ACE_TRACE ("ACE_Bounded_Stack<T>::~ACE_Bounded_Stack");
79 delete [] this->stack_;
82 // ----------------------------------------
84 ACE_ALLOC_HOOK_DEFINE_Tcs(ACE_Fixed_Stack)
86 template <class T, size_t ACE_SIZE> void
87 ACE_Fixed_Stack<T, ACE_SIZE>::dump (void) const
89 #if defined (ACE_HAS_DUMP)
90 ACE_TRACE ("ACE_Fixed_Stack<T, ACE_SIZE>::dump");
91 #endif /* ACE_HAS_DUMP */
94 template<class T, size_t ACE_SIZE>
95 ACE_Fixed_Stack<T, ACE_SIZE>::ACE_Fixed_Stack (void)
96 : size_ (ACE_SIZE),
97 top_ (0)
99 ACE_TRACE ("ACE_Fixed_Stack<T, ACE_SIZE>::ACE_Fixed_Stack");
102 template<class T, size_t ACE_SIZE>
103 ACE_Fixed_Stack<T, ACE_SIZE>::ACE_Fixed_Stack (const ACE_Fixed_Stack<T, ACE_SIZE> &s)
104 : size_ (s.size_),
105 top_ (s.top_)
107 ACE_TRACE ("ACE_Fixed_Stack<T, ACE_SIZE>::ACE_Fixed_Stack");
108 for (size_t i = 0; i < this->top_; i++)
109 this->stack_[i] = s.stack_[i];
112 template<class T, size_t ACE_SIZE> void
113 ACE_Fixed_Stack<T, ACE_SIZE>::operator= (const ACE_Fixed_Stack<T, ACE_SIZE> &s)
115 ACE_TRACE ("ACE_Fixed_Stack<T, ACE_SIZE>::operator=");
117 if (&s != this)
119 this->top_ = s.top_;
121 for (size_t i = 0; i < this->top_; i++)
122 this->stack_[i] = s.stack_[i];
126 template<class T, size_t ACE_SIZE>
127 ACE_Fixed_Stack<T, ACE_SIZE>::~ACE_Fixed_Stack (void)
129 ACE_TRACE ("ACE_Fixed_Stack<T, ACE_SIZE>::~ACE_Fixed_Stack");
132 //----------------------------------------
134 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Unbounded_Stack)
136 template <class T> void
137 ACE_Unbounded_Stack<T>::dump (void) const
139 #if defined (ACE_HAS_DUMP)
140 // ACE_TRACE ("ACE_Unbounded_Stack<T>::dump");
141 #endif /* ACE_HAS_DUMP */
144 template<class T>
145 ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack (ACE_Allocator *alloc)
146 : head_ (0),
147 cur_size_ (0),
148 allocator_ (alloc)
150 // ACE_TRACE ("ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack");
151 if (this->allocator_ == 0)
152 this->allocator_ = ACE_Allocator::instance ();
154 ACE_NEW_MALLOC (this->head_,
155 (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
156 ACE_Node<T>);
157 this->head_->next_ = this->head_;
160 template<class T> void
161 ACE_Unbounded_Stack<T>::delete_all_nodes (void)
163 // ACE_TRACE ("ACE_Unbounded_Stack<T>::delete_all_nodes");
165 while (this->is_empty () == 0)
167 ACE_Node<T> *temp = this->head_->next_;
168 this->head_->next_ = temp->next_;
169 ACE_DES_FREE_TEMPLATE (temp, this->allocator_->free,
170 ACE_Node, <T>);
173 this->cur_size_ = 0;
175 ACE_ASSERT (this->head_ == this->head_->next_
176 && this->is_empty ());
179 template<class T> void
180 ACE_Unbounded_Stack<T>::copy_all_nodes (const ACE_Unbounded_Stack<T> &s)
182 // ACE_TRACE ("ACE_Unbounded_Stack<T>::copy_all_nodes");
184 ACE_ASSERT (this->head_ == this->head_->next_);
186 ACE_Node<T> *temp = this->head_;
188 for (ACE_Node<T> *s_temp = s.head_->next_;
189 s_temp != s.head_;
190 s_temp = s_temp->next_)
192 ACE_Node<T> *nptr = temp->next_;
193 ACE_NEW_MALLOC (temp->next_,
194 (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
195 ACE_Node<T> (s_temp->item_, nptr));
196 temp = temp->next_;
198 this->cur_size_ = s.cur_size_;
201 template<class T>
202 ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack (const ACE_Unbounded_Stack<T> &s)
203 : head_ (0),
204 cur_size_ (0),
205 allocator_ (s.allocator_)
207 if (this->allocator_ == 0)
208 this->allocator_ = ACE_Allocator::instance ();
210 ACE_NEW_MALLOC (this->head_,
211 (ACE_Node<T> *) this->allocator_->malloc (sizeof (ACE_Node<T>)),
212 ACE_Node<T>);
213 this->head_->next_ = this->head_;
215 // ACE_TRACE ("ACE_Unbounded_Stack<T>::ACE_Unbounded_Stack");
216 this->copy_all_nodes (s);
219 template<class T> void
220 ACE_Unbounded_Stack<T>::operator= (const ACE_Unbounded_Stack<T> &s)
222 // ACE_TRACE ("ACE_Unbounded_Stack<T>::operator=");
224 if (this != &s)
226 this->delete_all_nodes ();
227 this->copy_all_nodes (s);
231 template<class T>
232 ACE_Unbounded_Stack<T>::~ACE_Unbounded_Stack (void)
234 // ACE_TRACE ("ACE_Unbounded_Stack<T>::~ACE_Unbounded_Stack");
236 this->delete_all_nodes ();
237 ACE_DES_FREE_TEMPLATE (head_,
238 this->allocator_->free,
239 ACE_Node,
240 <T>);
243 template<class T> int
244 ACE_Unbounded_Stack<T>::push (const T &new_item)
246 // ACE_TRACE ("ACE_Unbounded_Stack<T>::push");
248 ACE_Node<T> *temp = 0;
250 ACE_NEW_MALLOC_RETURN (temp,
251 static_cast<ACE_Node<T> *> (this->allocator_->malloc (sizeof (ACE_Node<T>))),
252 ACE_Node<T> (new_item, this->head_->next_),
253 -1);
254 this->head_->next_ = temp;
255 ++this->cur_size_;
256 return 0;
259 template<class T> int
260 ACE_Unbounded_Stack<T>::pop (T &item)
262 // ACE_TRACE ("ACE_Unbounded_Stack<T>::pop");
264 if (this->is_empty ())
265 return -1;
266 else
268 ACE_Node<T> *temp = this->head_->next_;
269 item = temp->item_;
270 this->head_->next_ = temp->next_;
272 ACE_DES_FREE_TEMPLATE (temp,
273 this->allocator_->free,
274 ACE_Node,
275 <T>);
276 --this->cur_size_;
277 return 0;
281 template <class T> int
282 ACE_Unbounded_Stack<T>::find (const T &item) const
284 // ACE_TRACE ("ACE_Unbounded_Stack<T>::find");
285 // Set <item> into the dummy node.
286 this->head_->item_ = item;
288 ACE_Node<T> *temp = this->head_->next_;
290 // Keep looping until we find the item.
291 while (!(temp->item_ == item))
292 temp = temp->next_;
294 // If we found the dummy node then it's not really there, otherwise,
295 // it is there.
296 return temp == this->head_ ? -1 : 0;
299 template <class T> int
300 ACE_Unbounded_Stack<T>::insert (const T &item)
302 // ACE_TRACE ("ACE_Unbounded_Stack<T>::insert");
304 if (this->find (item) == 0)
305 return 1;
306 else
307 return this->push (item);
310 template <class T> int
311 ACE_Unbounded_Stack<T>::remove (const T &item)
313 // ACE_TRACE ("ACE_Unbounded_Stack<T>::remove");
315 // Insert the item to be founded into the dummy node.
316 this->head_->item_ = item;
318 ACE_Node<T> *curr = this->head_;
320 while (!(curr->next_->item_ == item))
321 curr = curr->next_;
323 if (curr->next_ == this->head_)
324 return -1; // Item was not found.
325 else
327 ACE_Node<T> *temp = curr->next_;
328 // Skip over the node that we're deleting.
329 curr->next_ = temp->next_;
330 --this->cur_size_;
331 ACE_DES_FREE_TEMPLATE (temp,
332 this->allocator_->free,
333 ACE_Node,
334 <T>);
335 return 0;
339 //--------------------------------------------------
340 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Double_Linked_List_Iterator_Base)
342 template <class T>
343 ACE_Double_Linked_List_Iterator_Base<T>::ACE_Double_Linked_List_Iterator_Base (const ACE_Double_Linked_List<T> &dll)
344 : current_ (0), dllist_ (&dll)
346 // Do nothing
349 template <class T>
350 ACE_Double_Linked_List_Iterator_Base<T>::ACE_Double_Linked_List_Iterator_Base (const ACE_Double_Linked_List_Iterator_Base<T> &iter)
351 : current_ (iter.current_),
352 dllist_ (iter.dllist_)
354 // Do nothing
358 template <class T> T *
359 ACE_Double_Linked_List_Iterator_Base<T>::next (void) const
361 return this->not_done ();
364 template <class T> int
365 ACE_Double_Linked_List_Iterator_Base<T>::next (T *&ptr) const
367 ptr = this->not_done ();
368 return ptr ? 1 : 0;
372 template <class T> int
373 ACE_Double_Linked_List_Iterator_Base<T>::done (void) const
375 return this->not_done () ? 0 : 1;
378 template <class T> T &
379 ACE_Double_Linked_List_Iterator_Base<T>::operator* (void) const
381 return *(this->not_done ());
384 // @@ Is this a valid retasking? Make sure to check with Purify and
385 // whatnot that we're not leaking memory or doing any other screwing things.
386 template <class T> void
387 ACE_Double_Linked_List_Iterator_Base<T>::reset (ACE_Double_Linked_List<T> &dll)
389 current_ = 0;
390 dllist_ = &dll;
393 template <class T> int
394 ACE_Double_Linked_List_Iterator_Base<T>::go_head (void)
396 this->current_ = static_cast<T*> (dllist_->head_->next_);
397 return this->current_ ? 1 : 0;
400 template <class T> int
401 ACE_Double_Linked_List_Iterator_Base<T>::go_tail (void)
403 this->current_ = static_cast<T*> (dllist_->head_->prev_);
404 return this->current_ ? 1 : 0;
407 template <class T> T *
408 ACE_Double_Linked_List_Iterator_Base<T>::not_done (void) const
410 if (this->current_ != this->dllist_->head_)
411 return this->current_;
412 else
413 return 0;
416 template <class T> T *
417 ACE_Double_Linked_List_Iterator_Base<T>::do_advance (void)
419 if (this->not_done ())
421 this->current_ = static_cast<T*> (this->current_->next_);
422 return this->not_done ();
424 else
425 return 0;
428 template <class T> T *
429 ACE_Double_Linked_List_Iterator_Base<T>::do_retreat (void)
431 if (this->not_done ())
433 this->current_ = static_cast<T*> (this->current_->prev_);
434 return this->not_done ();
436 else
437 return 0;
440 template <class T> void
441 ACE_Double_Linked_List_Iterator_Base<T>::dump_i (void) const
443 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
444 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("current_ = %x"), this->current_));
445 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
448 //--------------------------------------------------
449 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Double_Linked_List_Iterator)
451 template <class T>
452 ACE_Double_Linked_List_Iterator<T>::ACE_Double_Linked_List_Iterator (const ACE_Double_Linked_List<T> &dll)
453 : ACE_Double_Linked_List_Iterator_Base <T> (dll)
455 this->current_ = static_cast<T*> (dll.head_->next_);
456 // Advance current_ out of the null area and onto the first item in
457 // the list
460 template <class T> void
461 ACE_Double_Linked_List_Iterator<T>::reset (ACE_Double_Linked_List<T> &dll)
463 this->ACE_Double_Linked_List_Iterator_Base <T>::reset (dll);
464 this->current_ = static_cast<T*> (dll.head_->next_);
465 // Advance current_ out of the null area and onto the first item in
466 // the list
469 template <class T> int
470 ACE_Double_Linked_List_Iterator<T>::first (void)
472 return this->go_head ();
475 template <class T> int
476 ACE_Double_Linked_List_Iterator<T>::advance (void)
478 return this->do_advance () ? 1 : 0;
481 template <class T> T*
482 ACE_Double_Linked_List_Iterator<T>::advance_and_remove (bool dont_remove)
484 T* item = 0;
485 if (dont_remove)
486 this->do_advance ();
487 else
489 item = this->next ();
490 this->do_advance ();
491 // It seems dangerous to remove nodes in an iterator, but so it goes...
492 ACE_Double_Linked_List<T> *dllist =
493 const_cast<ACE_Double_Linked_List<T> *> (this->dllist_);
494 dllist->remove (item);
496 return item;
499 template <class T> void
500 ACE_Double_Linked_List_Iterator<T>::dump (void) const
502 #if defined (ACE_HAS_DUMP)
503 this->dump_i ();
504 #endif /* ACE_HAS_DUMP */
507 // Prefix advance.
509 template <class T>
510 ACE_Double_Linked_List_Iterator<T> &
511 ACE_Double_Linked_List_Iterator<T>::operator++ (void)
513 this->do_advance ();
514 return *this;
518 // Postfix advance.
520 template <class T>
521 ACE_Double_Linked_List_Iterator<T>
522 ACE_Double_Linked_List_Iterator<T>::operator++ (int)
524 ACE_Double_Linked_List_Iterator<T> retv (*this);
525 this->do_advance ();
526 return retv;
530 // Prefix reverse.
532 template <class T>
533 ACE_Double_Linked_List_Iterator<T> &
534 ACE_Double_Linked_List_Iterator<T>::operator-- (void)
536 this->do_retreat ();
537 return *this;
541 // Postfix reverse.
543 template <class T>
544 ACE_Double_Linked_List_Iterator<T>
545 ACE_Double_Linked_List_Iterator<T>::operator-- (int)
547 ACE_Double_Linked_List_Iterator<T> retv (*this);
548 this->do_retreat ();
549 return retv;
553 //--------------------------------------------------
554 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Double_Linked_List_Reverse_Iterator)
556 template <class T>
557 ACE_Double_Linked_List_Reverse_Iterator<T>::ACE_Double_Linked_List_Reverse_Iterator (ACE_Double_Linked_List<T> &dll)
558 : ACE_Double_Linked_List_Iterator_Base <T> (dll)
560 this->current_ = static_cast<T*> (dll.head_->prev_);
561 // Advance current_ out of the null area and onto the last item in
562 // the list
565 template <class T> void
566 ACE_Double_Linked_List_Reverse_Iterator<T>::reset (ACE_Double_Linked_List<T> &dll)
568 this->ACE_Double_Linked_List_Iterator_Base <T>::reset (dll);
569 this->current_ = static_cast<T*> (dll.head_->prev_);
570 // Advance current_ out of the null area and onto the last item in
571 // the list
574 template <class T> int
575 ACE_Double_Linked_List_Reverse_Iterator<T>::first (void)
577 return this->go_tail ();
580 template <class T> int
581 ACE_Double_Linked_List_Reverse_Iterator<T>::advance (void)
583 return this->do_retreat () ? 1 : 0;
586 template <class T> T*
587 ACE_Double_Linked_List_Reverse_Iterator<T>::advance_and_remove (bool dont_remove)
589 T* item = 0;
590 if (dont_remove)
592 this->do_retreat ();
594 else
596 item = this->next ();
597 this->do_retreat ();
598 // It seems dangerous to remove nodes in an iterator, but so it goes...
599 ACE_Double_Linked_List<T> *dllist =
600 const_cast<ACE_Double_Linked_List<T> *> (this->dllist_);
601 dllist->remove (item);
603 return item;
606 template <class T> void
607 ACE_Double_Linked_List_Reverse_Iterator<T>::dump (void) const
609 #if defined (ACE_HAS_DUMP)
610 this->dump_i ();
611 #endif /* ACE_HAS_DUMP */
614 // Prefix advance.
616 template <class T>
617 ACE_Double_Linked_List_Reverse_Iterator<T> &
618 ACE_Double_Linked_List_Reverse_Iterator<T>::operator++ (void)
620 this->do_retreat ();
621 return *this;
625 // Postfix advance.
627 template <class T>
628 ACE_Double_Linked_List_Reverse_Iterator<T>
629 ACE_Double_Linked_List_Reverse_Iterator<T>::operator++ (int)
631 ACE_Double_Linked_List_Reverse_Iterator<T> retv (*this);
632 this->do_retreat ();
633 return retv;
637 // Prefix reverse.
639 template <class T>
640 ACE_Double_Linked_List_Reverse_Iterator<T> &
641 ACE_Double_Linked_List_Reverse_Iterator<T>::operator-- (void)
643 this->do_advance ();
644 return *this;
648 // Postfix reverse.
650 template <class T>
651 ACE_Double_Linked_List_Reverse_Iterator<T>
652 ACE_Double_Linked_List_Reverse_Iterator<T>::operator-- (int)
654 ACE_Double_Linked_List_Reverse_Iterator<T> retv (*this);
655 this->do_advance ();
656 return retv;
660 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Double_Linked_List)
662 template <class T>
663 ACE_Double_Linked_List<T>:: ACE_Double_Linked_List (ACE_Allocator *alloc)
664 : size_ (0), allocator_ (alloc)
666 if (this->allocator_ == 0)
667 this->allocator_ = ACE_Allocator::instance ();
669 ACE_NEW_MALLOC (this->head_,
670 (T *) this->allocator_->malloc (sizeof (T)),
672 this->init_head ();
675 template <class T>
676 ACE_Double_Linked_List<T>::ACE_Double_Linked_List (const ACE_Double_Linked_List<T> &cx)
677 : allocator_ (cx.allocator_)
679 if (this->allocator_ == 0)
680 this->allocator_ = ACE_Allocator::instance ();
682 ACE_NEW_MALLOC (this->head_,
683 (T *) this->allocator_->malloc (sizeof (T)),
685 this->init_head ();
686 this->copy_nodes (cx);
687 this->size_ = cx.size_;
690 template <class T> void
691 ACE_Double_Linked_List<T>::operator= (const ACE_Double_Linked_List<T> &cx)
693 if (this != &cx)
695 this->delete_nodes ();
696 this->copy_nodes (cx);
700 template <class T>
701 ACE_Double_Linked_List<T>::~ACE_Double_Linked_List (void)
703 this->delete_nodes ();
705 ACE_DES_FREE (head_,
706 this->allocator_->free,
709 this->head_ = 0;
712 template <class T> int
713 ACE_Double_Linked_List<T>::is_empty (void) const
715 return this->size () ? 0 : 1;
718 template <class T> int
719 ACE_Double_Linked_List<T>::is_full (void) const
721 return 0; // We have no bound.
724 template <class T> T *
725 ACE_Double_Linked_List<T>::insert_tail (T *new_item)
727 // Insert it before <head_>, i.e., at tail.
728 this->insert_element (new_item, 1);
729 return new_item;
732 template <class T> T *
733 ACE_Double_Linked_List<T>::insert_head (T *new_item)
735 this->insert_element (new_item); // Insert it after <head_>, i.e., at head.
736 return new_item;
739 template <class T> T *
740 ACE_Double_Linked_List<T>::delete_head (void)
742 if (this->is_empty ())
743 return 0;
745 T *temp = static_cast<T *> (this->head_->next_);
746 // Detach it from the list.
747 this->remove_element (temp);
748 return temp;
751 template <class T> T *
752 ACE_Double_Linked_List<T>::delete_tail (void)
754 if (this->is_empty ())
755 return 0;
757 T *temp = static_cast <T *> (this->head_->prev_);
758 // Detach it from the list.
759 this->remove_element (temp);
760 return temp;
763 template <class T> void
764 ACE_Double_Linked_List<T>::reset (void)
766 this->delete_nodes ();
769 template <class T> int
770 ACE_Double_Linked_List<T>::get (T *&item, size_t slot)
772 ACE_Double_Linked_List_Iterator<T> iter (*this);
774 for (size_t i = 0;
775 i < slot && !iter.done ();
776 i++)
777 iter.advance ();
779 item = iter.next ();
780 return item ? 0 : -1;
783 template <class T> size_t
784 ACE_Double_Linked_List<T>::size (void) const
786 return this->size_;
789 template <class T> void
790 ACE_Double_Linked_List<T>::dump (void) const
792 #if defined (ACE_HAS_DUMP)
793 // Dump the state of an object.
794 #endif /* ACE_HAS_DUMP */
797 template <class T> int
798 ACE_Double_Linked_List<T>::remove (T *n)
800 return this->remove_element (n);
803 template <class T> void
804 ACE_Double_Linked_List<T>::delete_nodes (void)
806 while (! this->is_empty ())
808 T * temp = static_cast<T*> (this->head_->next_);
809 this->remove_element (temp);
810 ACE_DES_FREE (temp,
811 this->allocator_->free,
816 template <class T> void
817 ACE_Double_Linked_List<T>::copy_nodes (const ACE_Double_Linked_List<T> &c)
819 for (ACE_Double_Linked_List_Iterator<T> iter (c);
820 !iter.done ();
821 iter.advance ())
823 T* temp = 0;
824 ACE_NEW_MALLOC (temp,
825 (T *)this->allocator_->malloc (sizeof (T)),
826 T (*iter.next ()));
827 this->insert_tail (temp);
831 template <class T> void
832 ACE_Double_Linked_List<T>::init_head (void)
834 this->head_->next_ = this->head_;
835 this->head_->prev_ = this->head_;
838 template <class T> int
839 ACE_Double_Linked_List<T>::insert_element (T *new_item,
840 int before,
841 T *old_item)
843 if (old_item == 0)
844 old_item = this->head_;
846 if (before)
847 old_item = static_cast<T *> (old_item->prev_);
849 new_item->next_ = old_item->next_;
850 new_item->next_->prev_ = new_item;
851 new_item->prev_ = old_item;
852 old_item->next_ = new_item;
853 ++this->size_;
854 return 0; // Well, what will cause errors here?
857 template <class T> int
858 ACE_Double_Linked_List<T>::remove_element (T *item)
860 // Notice that you have to ensure that item is an element of this
861 // list. We can't do much checking here.
863 if (item == this->head_ || item->next_ == 0
864 || item->prev_ == 0 || this->size () == 0) // Can't remove head
865 return -1;
867 item->prev_->next_ = item->next_;
868 item->next_->prev_ = item->prev_;
869 item->next_ = item->prev_ = 0; // reset pointers to prevent double removal.
870 --this->size_;
871 return 0;
874 //--------------------------------------------------
875 ACE_ALLOC_HOOK_DEFINE_Tcs(ACE_Fixed_Set)
877 template <class T, size_t ACE_SIZE> size_t
878 ACE_Fixed_Set<T, ACE_SIZE>::size (void) const
880 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::size");
881 return this->cur_size_;
884 template <class T, size_t ACE_SIZE> void
885 ACE_Fixed_Set<T, ACE_SIZE>::dump (void) const
887 #if defined (ACE_HAS_DUMP)
888 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::dump");
889 #endif /* ACE_HAS_DUMP */
892 template <class T, size_t ACE_SIZE>
893 ACE_Fixed_Set<T, ACE_SIZE>::~ACE_Fixed_Set (void)
895 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::~ACE_Fixed_Set");
896 this->cur_size_ = 0;
899 template <class T, size_t ACE_SIZE>
900 ACE_Fixed_Set<T, ACE_SIZE>::ACE_Fixed_Set (const ACE_Fixed_Set<T, ACE_SIZE> &fs)
901 : cur_size_ (fs.cur_size_)
903 ACE_TRACE ("ACE_Fixed_Set<T>::ACE_Fixed_Set");
905 for (size_t i = 0, j = 0; i < fs.max_size_ && j < this->cur_size_; ++i)
906 if (fs.search_structure_[i].is_free_ == 0)
907 this->search_structure_[j++] = fs.search_structure_[i];
910 template <class T, size_t ACE_SIZE> void
911 ACE_Fixed_Set<T, ACE_SIZE>::operator= (const ACE_Fixed_Set<T, ACE_SIZE> &fs)
913 ACE_TRACE ("ACE_Fixed_Set<T>::operator=");
915 if (this != &fs)
917 this->cur_size_ = fs.cur_size_;
919 for (size_t i = 0, j = 0; i < fs.max_size_ && j < this->cur_size_; ++i)
920 if (fs.search_structure_[i].is_free_ == 0)
921 this->search_structure_[j++] = fs.search_structure_[i];
925 template <class T, size_t ACE_SIZE>
926 ACE_Fixed_Set<T, ACE_SIZE>::ACE_Fixed_Set (void)
927 : cur_size_ (0),
928 max_size_ (ACE_SIZE)
930 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::ACE_Fixed_Set");
931 for (size_t i = 0; i < this->max_size_; i++)
932 this->search_structure_[i].is_free_ = 1;
935 template <class T, size_t ACE_SIZE> int
936 ACE_Fixed_Set<T, ACE_SIZE>::find (const T &item) const
938 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::find");
940 for (size_t i = 0, j = 0; i < this->max_size_ && j < this->cur_size_; ++i)
941 if (this->search_structure_[i].is_free_ == 0)
943 if (this->search_structure_[i].item_ == item)
944 return 0;
945 ++j;
948 return -1;
951 template <class T, size_t ACE_SIZE> int
952 ACE_Fixed_Set<T, ACE_SIZE>::insert (const T &item)
954 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::insert");
955 ssize_t first_free = -1; // Keep track of first free slot.
956 size_t i;
958 for (i = 0;
959 i < this->max_size_ && first_free == -1;
960 ++i)
962 // First, make sure we don't allow duplicates.
964 if (this->search_structure_[i].is_free_ == 0)
966 if (this->search_structure_[i].item_ == item)
967 return 1;
969 else
970 first_free = static_cast<ssize_t> (i);
972 // If we found a free spot let's reuse it.
974 if (first_free > -1)
976 this->search_structure_[first_free].item_ = item;
977 this->search_structure_[first_free].is_free_ = 0;
978 this->cur_size_++;
979 return 0;
981 else /* No more room! */
983 errno = ENOMEM;
984 return -1;
988 template <class T, size_t ACE_SIZE> int
989 ACE_Fixed_Set<T, ACE_SIZE>::remove (const T &item)
991 ACE_TRACE ("ACE_Fixed_Set<T, ACE_SIZE>::remove");
993 for (size_t i = 0, j = 0;
994 i < this->max_size_ && j < this->cur_size_;
995 ++i)
996 if (this->search_structure_[i].is_free_ == 0)
998 if (this->search_structure_[i].item_ == item)
1000 // Mark this entry as being free.
1001 this->search_structure_[i].is_free_ = 1;
1003 --this->cur_size_;
1004 return 0;
1006 else
1007 ++j;
1010 return -1;
1013 //--------------------------------------------------
1014 ACE_ALLOC_HOOK_DEFINE_Tcs(ACE_Fixed_Set_Iterator_Base)
1016 template <class T, size_t ACE_SIZE> void
1017 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::dump_i (void) const
1019 #if defined (ACE_HAS_DUMP)
1020 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::dump_i");
1021 #endif /* ACE_HAS_DUMP */
1024 template <class T, size_t ACE_SIZE>
1025 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::ACE_Fixed_Set_Iterator_Base (ACE_Fixed_Set<T, ACE_SIZE> &s)
1026 : s_ (s),
1027 next_ (-1),
1028 iterated_items_ (0)
1030 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::ACE_Fixed_Set_Iterator_Base");
1031 this->advance ();
1034 template <class T, size_t ACE_SIZE> int
1035 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::advance (void)
1037 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::advance");
1039 if (this->iterated_items_ < this->s_.cur_size_)
1041 for (++this->next_;
1042 static_cast<size_t> (this->next_) < this->s_.max_size_;
1043 ++this->next_)
1044 if (this->s_.search_structure_[this->next_].is_free_ == 0)
1046 ++this->iterated_items_;
1047 return 1;
1050 else
1051 ++this->next_;
1053 return 0;
1056 template <class T, size_t ACE_SIZE> int
1057 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::first (void)
1059 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::first");
1061 next_ = -1;
1062 iterated_items_ = 0;
1063 return this->advance ();
1066 template <class T, size_t ACE_SIZE> int
1067 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::done (void) const
1069 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::done");
1071 return ! (this->iterated_items_ < this->s_.cur_size_);
1074 template <class T, size_t ACE_SIZE> int
1075 ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::next_i (T *&item)
1077 ACE_TRACE ("ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE>::next_i");
1079 if (static_cast<size_t> (this->next_) < this->s_.max_size_)
1082 if (this->s_.search_structure_[this->next_].is_free_ == 0)
1084 item = &this->s_.search_structure_[this->next_].item_;
1085 this->advance ();
1086 return 1;
1089 while (this->advance () == 1);
1091 return 0;
1094 //--------------------------------------------------
1095 ACE_ALLOC_HOOK_DEFINE_Tcs(ACE_Fixed_Set_Iterator)
1097 template <class T, size_t ACE_SIZE> void
1098 ACE_Fixed_Set_Iterator<T, ACE_SIZE>::dump (void) const
1100 #if defined (ACE_HAS_DUMP)
1101 this->dump_i ();
1102 #endif /* ACE_HAS_DUMP */
1105 template <class T, size_t ACE_SIZE>
1106 ACE_Fixed_Set_Iterator<T, ACE_SIZE>::ACE_Fixed_Set_Iterator (ACE_Fixed_Set<T, ACE_SIZE> &s)
1107 : ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE> (s)
1109 ACE_TRACE ("ACE_Fixed_Set_Iterator<T, ACE_SIZE>::ACE_Fixed_Set_Iterator");
1112 template <class T, size_t ACE_SIZE> int
1113 ACE_Fixed_Set_Iterator<T, ACE_SIZE>::next (T *&item)
1115 ACE_TRACE ("ACE_Fixed_Set_Iterator<T, ACE_SIZE>::next");
1116 return this->next_i (item);
1119 template <class T, size_t ACE_SIZE> int
1120 ACE_Fixed_Set_Iterator<T, ACE_SIZE>::remove (T *&item)
1122 ACE_TRACE ("ACE_Fixed_Set_Iterator<T, ACE_SIZE>::remove");
1124 if (this->s_.search_structure_[this->next_].is_free_ == 0)
1126 item = &this->s_.search_structure_[this->next_].item_;
1127 this->s_.remove (*item);
1128 --(this->iterated_items_);
1129 return 1;
1132 return 0;
1135 template <class T, size_t ACE_SIZE> T&
1136 ACE_Fixed_Set_Iterator<T, ACE_SIZE>::operator* (void)
1138 T *retv = 0;
1140 if (this->s_.search_structure_[this->next_].is_free_ == 0)
1141 retv = &this->s_.search_structure_[this->next_].item_;
1143 ACE_ASSERT (retv != 0);
1145 return *retv;
1148 //--------------------------------------------------
1149 ACE_ALLOC_HOOK_DEFINE_Tcs(ACE_Fixed_Set_Const_Iterator)
1151 template <class T, size_t ACE_SIZE> void
1152 ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::dump (void) const
1154 #if defined (ACE_HAS_DUMP)
1155 this->dump_i ();
1156 #endif /* ACE_HAS_DUMP */
1159 template <class T, size_t ACE_SIZE>
1160 ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::ACE_Fixed_Set_Const_Iterator (const ACE_Fixed_Set<T, ACE_SIZE> &s)
1161 : ACE_Fixed_Set_Iterator_Base<T, ACE_SIZE> (s)
1163 ACE_TRACE ("ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::ACE_Fixed_Set_Const_Iterator");
1166 template <class T, size_t ACE_SIZE> int
1167 ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::next (const T *&item)
1169 ACE_TRACE ("ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::next");
1171 return this->next_i (item);
1174 template <class T, size_t ACE_SIZE> const T&
1175 ACE_Fixed_Set_Const_Iterator<T, ACE_SIZE>::operator* (void) const
1177 const T *retv = 0;
1179 if (this->s_.search_structure_[this->next_].is_free_ == 0)
1180 retv = &this->s_.search_structure_[this->next_].item_;
1182 ACE_ASSERT (retv != 0);
1184 return *retv;
1187 //--------------------------------------------------
1188 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Bounded_Set)
1190 template <class T> void
1191 ACE_Bounded_Set<T>::dump (void) const
1193 #if defined (ACE_HAS_DUMP)
1194 ACE_TRACE ("ACE_Bounded_Set<T>::dump");
1195 #endif /* ACE_HAS_DUMP */
1198 template <class T>
1199 ACE_Bounded_Set<T>::~ACE_Bounded_Set (void)
1201 ACE_TRACE ("ACE_Bounded_Set<T>::~ACE_Bounded_Set");
1202 delete [] this->search_structure_;
1205 template <class T>
1206 ACE_Bounded_Set<T>::ACE_Bounded_Set (void)
1207 : cur_size_ (0),
1208 max_size_ (static_cast<size_t> (ACE_Bounded_Set<T>::DEFAULT_SIZE))
1210 ACE_TRACE ("ACE_Bounded_Set<T>::ACE_Bounded_Set");
1212 ACE_NEW (this->search_structure_,
1213 typename ACE_Bounded_Set<T>::Search_Structure[this->max_size_]);
1215 for (size_t i = 0; i < this->max_size_; ++i)
1216 this->search_structure_[i].is_free_ = 1;
1219 template <class T> size_t
1220 ACE_Bounded_Set<T>::size (void) const
1222 ACE_TRACE ("ACE_Bounded_Set<T>::size");
1223 return this->cur_size_;
1226 template <class T>
1227 ACE_Bounded_Set<T>::ACE_Bounded_Set (const ACE_Bounded_Set<T> &bs)
1228 : cur_size_ (bs.cur_size_),
1229 max_size_ (bs.max_size_)
1231 ACE_TRACE ("ACE_Bounded_Set<T>::ACE_Bounded_Set");
1233 ACE_NEW (this->search_structure_,
1234 typename ACE_Bounded_Set<T>::Search_Structure[this->max_size_]);
1236 for (size_t i = 0; i < this->cur_size_; i++)
1237 this->search_structure_[i] = bs.search_structure_[i];
1240 template <class T> void
1241 ACE_Bounded_Set<T>::operator= (const ACE_Bounded_Set<T> &bs)
1243 ACE_TRACE ("ACE_Bounded_Set<T>::operator=");
1245 if (this != &bs)
1247 if (this->max_size_ < bs.cur_size_)
1249 delete [] this->search_structure_;
1250 ACE_NEW (this->search_structure_,
1251 typename ACE_Bounded_Set<T>::Search_Structure[bs.cur_size_]);
1252 this->max_size_ = bs.cur_size_;
1255 this->cur_size_ = bs.cur_size_;
1257 for (size_t i = 0; i < this->cur_size_; i++)
1258 this->search_structure_[i] = bs.search_structure_[i];
1262 template <class T>
1263 ACE_Bounded_Set<T>::ACE_Bounded_Set (size_t size)
1264 : cur_size_ (0),
1265 max_size_ (size)
1267 ACE_TRACE ("ACE_Bounded_Set<T>::ACE_Bounded_Set");
1268 ACE_NEW (this->search_structure_,
1269 typename ACE_Bounded_Set<T>::Search_Structure[size]);
1271 for (size_t i = 0; i < this->max_size_; i++)
1272 this->search_structure_[i].is_free_ = 1;
1275 template <class T> int
1276 ACE_Bounded_Set<T>::find (const T &item) const
1278 ACE_TRACE ("ACE_Bounded_Set<T>::find");
1280 for (size_t i = 0; i < this->cur_size_; i++)
1281 if (this->search_structure_[i].item_ == item
1282 && this->search_structure_[i].is_free_ == 0)
1283 return 0;
1285 return -1;
1288 template <class T> int
1289 ACE_Bounded_Set<T>::insert (const T &item)
1291 ACE_TRACE ("ACE_Bounded_Set<T>::insert");
1292 int first_free = -1; // Keep track of first free slot.
1293 size_t i;
1295 for (i = 0; i < this->cur_size_; i++)
1296 // First, make sure we don't allow duplicates.
1298 if (this->search_structure_[i].item_ == item
1299 && this->search_structure_[i].is_free_ == 0)
1300 return 1;
1301 else if (this->search_structure_[i].is_free_ && first_free == -1)
1302 first_free = static_cast<int> (i);
1304 if (first_free > -1) // If we found a free spot let's reuse it.
1306 this->search_structure_[first_free].item_ = item;
1307 this->search_structure_[first_free].is_free_ = 0;
1308 return 0;
1310 else if (i < this->max_size_) // Insert at the end of the active portion.
1312 this->search_structure_[i].item_ = item;
1313 this->search_structure_[i].is_free_ = 0;
1314 this->cur_size_++;
1315 return 0;
1317 else /* No more room! */
1319 errno = ENOMEM;
1320 return -1;
1324 template <class T> int
1325 ACE_Bounded_Set<T>::remove (const T &item)
1327 ACE_TRACE ("ACE_Bounded_Set<T>::remove");
1328 for (size_t i = 0; i < this->cur_size_; i++)
1329 if (this->search_structure_[i].item_ == item)
1331 // Mark this entry as being free.
1332 this->search_structure_[i].is_free_ = 1;
1334 // If we just unbound the highest entry, then we need to
1335 // figure out where the next highest active entry is.
1336 if (i + 1 == this->cur_size_)
1338 while (i > 0 && this->search_structure_[--i].is_free_)
1339 continue;
1341 if (i == 0 && this->search_structure_[i].is_free_)
1342 this->cur_size_ = 0;
1343 else
1344 this->cur_size_ = i + 1;
1346 return 0;
1349 return -1;
1352 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Bounded_Set_Iterator)
1354 template <class T> void
1355 ACE_Bounded_Set_Iterator<T>::dump (void) const
1357 #if defined (ACE_HAS_DUMP)
1358 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::dump");
1359 #endif /* ACE_HAS_DUMP */
1362 template <class T>
1363 ACE_Bounded_Set_Iterator<T>::ACE_Bounded_Set_Iterator (ACE_Bounded_Set<T> &s)
1364 : s_ (s),
1365 next_ (-1)
1367 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::ACE_Bounded_Set_Iterator");
1368 this->advance ();
1371 template <class T> int
1372 ACE_Bounded_Set_Iterator<T>::advance (void)
1374 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::advance");
1376 for (++this->next_;
1377 static_cast<size_t> (this->next_) < this->s_.cur_size_
1378 && this->s_.search_structure_[this->next_].is_free_;
1379 ++this->next_)
1380 continue;
1382 return static_cast<size_t> (this->next_) < this->s_.cur_size_;
1385 template <class T> int
1386 ACE_Bounded_Set_Iterator<T>::first (void)
1388 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::first");
1390 next_ = -1;
1391 return this->advance ();
1394 template <class T> int
1395 ACE_Bounded_Set_Iterator<T>::done (void) const
1397 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::done");
1399 return static_cast<ACE_CAST_CONST size_t> (this->next_) >=
1400 this->s_.cur_size_;
1403 template <class T> int
1404 ACE_Bounded_Set_Iterator<T>::next (T *&item)
1406 ACE_TRACE ("ACE_Bounded_Set_Iterator<T>::next");
1407 if (static_cast<size_t> (this->next_) < this->s_.cur_size_)
1409 item = &this->s_.search_structure_[this->next_].item_;
1410 return 1;
1412 else
1413 return 0;
1416 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_DNode)
1418 template <class T>
1419 ACE_DNode<T>::ACE_DNode (const T &i, ACE_DNode<T> *n, ACE_DNode<T> *p)
1420 : next_ (n), prev_ (p), item_ (i)
1424 template <class T>
1425 ACE_DNode<T>::~ACE_DNode (void)
1429 // ****************************************************************
1431 template <class T> void
1432 ACE_Unbounded_Stack_Iterator<T>::dump (void) const
1434 #if defined (ACE_HAS_DUMP)
1435 // ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::dump");
1436 #endif /* ACE_HAS_DUMP */
1439 template <class T>
1440 ACE_Unbounded_Stack_Iterator<T>::ACE_Unbounded_Stack_Iterator (ACE_Unbounded_Stack<T> &q)
1441 : current_ (q.head_->next_),
1442 stack_ (q)
1444 // ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::ACE_Unbounded_Stack_Iterator");
1447 template <class T> int
1448 ACE_Unbounded_Stack_Iterator<T>::advance (void)
1450 // ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::advance");
1451 this->current_ = this->current_->next_;
1452 return this->current_ != this->stack_.head_;
1455 template <class T> int
1456 ACE_Unbounded_Stack_Iterator<T>::first (void)
1458 // ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::first");
1459 this->current_ = this->stack_.head_->next_;
1460 return this->current_ != this->stack_.head_;
1463 template <class T> int
1464 ACE_Unbounded_Stack_Iterator<T>::done (void) const
1466 ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::done");
1468 return this->current_ == this->stack_.head_;
1471 template <class T> int
1472 ACE_Unbounded_Stack_Iterator<T>::next (T *&item)
1474 // ACE_TRACE ("ACE_Unbounded_Stack_Iterator<T>::next");
1475 if (this->current_ == this->stack_.head_)
1476 return 0;
1477 else
1479 item = &this->current_->item_;
1480 return 1;
1485 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Ordered_MultiSet)
1487 template <class T>
1488 ACE_Ordered_MultiSet<T>::ACE_Ordered_MultiSet (ACE_Allocator *alloc)
1489 : head_ (0)
1490 , tail_ (0)
1491 , cur_size_ (0)
1492 , allocator_ (alloc)
1494 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::ACE_Ordered_MultiSet");
1496 if (this->allocator_ == 0)
1497 this->allocator_ = ACE_Allocator::instance ();
1500 template <class T>
1501 ACE_Ordered_MultiSet<T>::ACE_Ordered_MultiSet (const ACE_Ordered_MultiSet<T> &us)
1502 : head_ (0)
1503 , tail_ (0)
1504 , cur_size_ (0)
1505 , allocator_ (us.allocator_)
1507 ACE_TRACE ("ACE_Ordered_MultiSet<T>::ACE_Ordered_MultiSet");
1509 if (this->allocator_ == 0)
1510 this->allocator_ = ACE_Allocator::instance ();
1512 this->copy_nodes (us);
1515 template <class T>
1516 ACE_Ordered_MultiSet<T>::~ACE_Ordered_MultiSet (void)
1518 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::~ACE_Ordered_MultiSet");
1520 this->delete_nodes ();
1524 template <class T> void
1525 ACE_Ordered_MultiSet<T>::operator= (const ACE_Ordered_MultiSet<T> &us)
1527 ACE_TRACE ("ACE_Ordered_MultiSet<T>::operator=");
1529 if (this != &us)
1531 this->delete_nodes ();
1532 this->copy_nodes (us);
1537 template <class T> int
1538 ACE_Ordered_MultiSet<T>::insert (const T &item)
1540 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::insert");
1542 return this->insert_from (item, this->head_, 0);
1545 template <class T> int
1546 ACE_Ordered_MultiSet<T>::insert (const T &new_item,
1547 ITERATOR &iter)
1549 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::insert using iterator");
1551 return this->insert_from (new_item, iter.current_, &iter.current_);
1554 template <class T> int
1555 ACE_Ordered_MultiSet<T>::remove (const T &item)
1557 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::remove");
1559 ACE_DNode<T> *node = 0;
1561 int result = locate (item, 0, node);
1563 // if we found the node, remove from list and free it
1564 if (node && (result == 0))
1566 if (node->prev_)
1567 node->prev_->next_ = node->next_;
1568 else
1569 head_ = node->next_;
1571 if (node->next_)
1572 node->next_->prev_ = node->prev_;
1573 else
1574 tail_ = node->prev_;
1576 --this->cur_size_;
1578 ACE_DES_FREE_TEMPLATE (node,
1579 this->allocator_->free,
1580 ACE_DNode,
1581 <T>);
1582 return 0;
1585 return -1;
1588 template <class T> int
1589 ACE_Ordered_MultiSet<T>::find (const T &item,
1590 ITERATOR &iter) const
1592 // search an occurrence of item, using iterator's current position as a hint
1593 ACE_DNode<T> *node = iter.current_;
1594 int const result = locate (item, node, node);
1596 // if we found the node, update the iterator and indicate success
1597 if (node && (result == 0))
1599 iter.current_ = node;
1600 return 0;
1603 return -1;
1608 template <class T> void
1609 ACE_Ordered_MultiSet<T>::reset (void)
1611 ACE_TRACE ("reset");
1613 this->delete_nodes ();
1616 template <class T> void
1617 ACE_Ordered_MultiSet<T>::dump (void) const
1619 #if defined (ACE_HAS_DUMP)
1620 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::dump");
1622 // ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
1623 // ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_ = %u"), this->head_));
1624 // ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhead_->next_ = %u"), this->head_->next_));
1625 // ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncur_size_ = %d\n"), this->cur_size_));
1627 // T *item = 0;
1628 // size_t count = 1;
1630 // for (ACE_Ordered_MultiSet_Iterator<T> iter (*(ACE_Ordered_MultiSet<T> *) this);
1631 // iter.next (item) != 0;
1632 // iter.advance ())
1633 // ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("count = %d\n"), count++));
1635 // ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
1636 #endif /* ACE_HAS_DUMP */
1639 template <class T> int
1640 ACE_Ordered_MultiSet<T>::insert_from (const T &item, ACE_DNode<T> *position,
1641 ACE_DNode<T> **new_position)
1643 // ACE_TRACE ("ACE_Ordered_MultiSet<T>::insert_from");
1645 // create a new node
1646 ACE_DNode<T> *temp = 0;
1647 ACE_NEW_MALLOC_RETURN (temp,
1648 static_cast<ACE_DNode<T>*> (this->allocator_->malloc (sizeof (ACE_DNode<T>))),
1649 ACE_DNode<T> (item),
1650 -1);
1651 // obtain approximate location of the node
1652 int result = locate (item, position, position);
1654 // if there are nodes in the multiset
1655 if (position)
1657 switch (result)
1659 // insert after the approximate position
1660 case -1:
1662 // if there is a following node
1663 if (position->next_)
1665 // link up with the following node
1666 position->next_->prev_ = temp;
1667 temp->next_ = position->next_;
1669 else
1670 // appending to the end of the set
1671 tail_ = temp;
1673 // link up with the preceeding node
1674 temp->prev_ = position;
1675 position->next_ = temp;
1677 break;
1679 // insert before the position
1680 case 0:
1681 case 1:
1683 // if there is a preceeding node
1684 if (position->prev_)
1686 // link up with the preceeding node
1687 position->prev_->next_ = temp;
1688 temp->prev_ = position->prev_;
1690 else
1691 // prepending to the start of the set
1692 head_ = temp;
1694 // link up with the preceeding node
1695 temp->next_ = position;
1696 position->prev_ = temp;
1698 break;
1700 default:
1701 return -1;
1704 else
1706 // point the head and tail to the new node.
1707 this->head_ = temp;
1708 this->tail_ = temp;
1711 ++this->cur_size_;
1712 if (new_position)
1713 *new_position = temp;
1715 return 0;
1718 template <class T> int
1719 ACE_Ordered_MultiSet<T>::locate (const T &item, ACE_DNode<T> *start_position,
1720 ACE_DNode<T> *&new_position) const
1722 if (! start_position)
1723 start_position = this->head_;
1725 // If starting before the item, move forward until at or just before
1726 // item.
1727 while (start_position && start_position->item_ < item &&
1728 start_position->next_)
1729 start_position = start_position->next_;
1731 // If starting after the item, move back until at or just after item
1732 while (start_position && item < start_position->item_ &&
1733 start_position->prev_)
1734 start_position = start_position->prev_;
1736 // Save the (approximate) location in the passed pointer.
1737 new_position = start_position;
1739 // Show the location is after (1), before (-1) , or at (0) the item
1740 if (!new_position)
1741 return 1;
1742 else if (item < new_position->item_)
1743 return 1;
1744 else if (new_position->item_ < item)
1745 return -1;
1746 else
1747 return 0;
1750 // Looks for first occurrence of <item> in the ordered set, using the
1751 // passed starting position as a hint: if there is such an instance,
1752 // it updates the new_position pointer to point to one such node and
1753 // returns 0; if there is no such node, then if there is a node before
1754 // where the item would have been, it updates the new_position pointer
1755 // to point to this node and returns -1; if there is no such node,
1756 // then if there is a node after where the item would have been, it
1757 // updates the new_position pointer to point to this node (or 0 if
1758 // there is no such node) and returns 1;
1760 template <class T> void
1761 ACE_Ordered_MultiSet<T>::copy_nodes (const ACE_Ordered_MultiSet<T> &us)
1763 ACE_DNode<T> *insertion_point = this->head_;
1765 for (ACE_DNode<T> *curr = us.head_;
1766 curr != 0;
1767 curr = curr->next_)
1768 this->insert_from (curr->item_, insertion_point, &insertion_point);
1771 template <class T> void
1772 ACE_Ordered_MultiSet<T>::delete_nodes (void)
1774 // iterate through list, deleting nodes
1775 for (ACE_DNode<T> *curr = this->head_;
1776 curr != 0;
1779 ACE_DNode<T> *temp = curr;
1780 curr = curr->next_;
1781 ACE_DES_FREE_TEMPLATE (temp,
1782 this->allocator_->free,
1783 ACE_DNode,
1784 <T>);
1787 this->head_ = 0;
1788 this->tail_ = 0;
1789 this->cur_size_ = 0;
1792 ACE_ALLOC_HOOK_DEFINE_Tc(ACE_Ordered_MultiSet_Iterator)
1794 template <class T>
1795 ACE_Ordered_MultiSet_Iterator<T>::ACE_Ordered_MultiSet_Iterator (ACE_Ordered_MultiSet<T> &s)
1796 : current_ (s.head_),
1797 set_ (s)
1799 // ACE_TRACE ("ACE_Ordered_MultiSet_Iterator<T>::ACE_Ordered_MultiSet_Iterator");
1802 template <class T> int
1803 ACE_Ordered_MultiSet_Iterator<T>::next (T *&item) const
1805 // ACE_TRACE ("ACE_Ordered_MultiSet_Iterator<T>::next");
1806 if (this->current_)
1808 item = &this->current_->item_;
1809 return 1;
1812 return 0;
1815 template <class T> T&
1816 ACE_Ordered_MultiSet_Iterator<T>::operator* (void)
1818 //ACE_TRACE ("ACE_Ordered_MultiSet_Iterator<T>::operator*");
1819 T *retv = 0;
1821 int const result = this->next (retv);
1822 ACE_ASSERT (result != 0);
1823 ACE_UNUSED_ARG (result);
1825 return *retv;
1828 template <class T> T *
1829 ACE_DLList<T>::insert_tail (T *new_item)
1831 ACE_DLList_Node *temp1 = 0;
1832 ACE_NEW_MALLOC_RETURN (temp1,
1833 static_cast<ACE_DLList_Node *> (this->allocator_->malloc (sizeof (ACE_DLList_Node))),
1834 ACE_DLList_Node (new_item),
1836 ACE_DLList_Node *temp2 = ACE_DLList_Base::insert_tail (temp1);
1837 return (T *) (temp2 ? temp2->item_ : 0);
1840 template <class T> T *
1841 ACE_DLList<T>::insert_head (T *new_item)
1843 ACE_DLList_Node *temp1 = 0;
1844 ACE_NEW_MALLOC_RETURN (temp1,
1845 (ACE_DLList_Node *) this->allocator_->malloc (sizeof (ACE_DLList_Node)),
1846 ACE_DLList_Node (new_item), 0);
1847 ACE_DLList_Node *temp2 = ACE_DLList_Base::insert_head (temp1);
1848 return (T *) (temp2 ? temp2->item_ : 0);
1851 template <class T> T *
1852 ACE_DLList<T>::delete_head (void)
1854 ACE_DLList_Node *temp1 = ACE_DLList_Base::delete_head ();
1855 T *temp2 = (T *) (temp1 ? temp1->item_ : 0);
1856 ACE_DES_FREE (temp1,
1857 this->allocator_->free,
1858 ACE_DLList_Node);
1860 return temp2;
1863 template <class T> T *
1864 ACE_DLList<T>::delete_tail (void)
1866 ACE_DLList_Node *temp1 = ACE_DLList_Base::delete_tail ();
1867 T *temp2 = (T *) (temp1 ? temp1->item_ : 0);
1868 ACE_DES_FREE (temp1,
1869 this->allocator_->free,
1870 ACE_DLList_Node);
1871 return temp2;
1874 // ****************************************************************
1876 // Compare this array with <s> for equality.
1878 template <class T> bool
1879 ACE_Array<T>::operator== (const ACE_Array<T> &s) const
1881 if (this == &s)
1882 return true;
1883 else if (this->size () != s.size ())
1884 return false;
1886 const size_t len = s.size ();
1887 for (size_t slot = 0; slot < len; ++slot)
1888 if ((*this)[slot] != s[slot])
1889 return false;
1891 return true;
1894 // ****************************************************************
1896 ACE_END_VERSIONED_NAMESPACE_DECL
1898 #endif /* ACE_CONTAINERS_T_CPP */