3 //=============================================================================
9 //=============================================================================
14 #include /**/ "ace/pre.h"
16 #include "ace/Global_Macros.h"
17 #include "ace/Functor_T.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
26 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
27 class ACE_RB_Tree_Iterator_Base
;
30 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
31 class ACE_RB_Tree_Iterator
;
34 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
35 class ACE_RB_Tree_Reverse_Iterator
;
40 class ACE_RB_Tree_Node_Base
43 enum RB_Tree_Node_Color
{RED
, BLACK
};
47 * @class ACE_RB_Tree_Node
49 * @brief Implements a node in a Red-Black Tree ADT.
51 template <class EXT_ID
, class INT_ID
>
52 class ACE_RB_Tree_Node
: public ACE_RB_Tree_Node_Base
56 ACE_RB_Tree_Node (const EXT_ID
&k
, const INT_ID
&t
);
67 /// Set color of the node.
68 void color (RB_Tree_Node_Color c
);
70 /// Get color of the node.
71 RB_Tree_Node_Color
color ();
73 /// Accessor for node's parent pointer.
74 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *parent ();
76 /// Mutator for node's parent pointer.
77 void parent (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * p
);
79 /// Accessor for node's left child pointer.
80 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *left ();
82 /// Mutator for node's left child pointer.
83 void left (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *l
);
85 /// Accessor for node's right child pointer.
86 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *right ();
88 /// Mutator for node's right child pointer
89 void right (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * r
);
98 /// Color of the node.
99 RB_Tree_Node_Color color_
;
101 /// Pointer to node's parent.
102 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *parent_
;
104 /// Pointer to node's left child.
105 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *left_
;
107 /// Pointer to node's right child.
108 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *right_
;
111 class ACE_RB_Tree_Base
114 /// Search result enumeration.
115 enum RB_SearchResult
{LEFT
, EXACT
, RIGHT
};
117 /// Get the allocator;
119 * @note This method is inlined here rather than in RB_Tree.inl
120 * since that file may be included multiple times when
121 * inlining is disabled. In those platform/configuration
122 * combinations, multiple definitions of this method occurred.
123 * Placing the definition inline in the header avoids such errors.
125 ACE_Allocator
* allocator () const { return this->allocator_
; }
128 // = Protected members.
130 /// Pointer to a memory allocator.
131 ACE_Allocator
*allocator_
;
137 * @brief Implements a Red-Black Tree ADT, according to T. H. Corman,
138 * C. E. Leiserson, and R. L. Rivest, "Introduction to Algorithms"
139 * 1990, MIT, chapter 14.
141 * A number of Changes have been made to this class template
142 * in order to conform to the ACE_Hash_Map_Manager_Ex
143 * interface. All previously supported public methods are
144 * still part of this class. However, these are marked as
145 * DEPRECATED and will be removed from this class in
146 * a future version of ACE. Please migrate your code
147 * to the appropriate public methods indicated in the
148 * method deprecation comments.
149 * This class uses an ACE_Allocator to allocate memory. The
150 * user can make this a persistent class by providing an
151 * ACE_Allocator with a persistable memory pool.
153 * <b> Requirements and Performance Characteristics</b>
154 * - Internal Structure:
156 * - Duplicates allowed?
158 * - Random access allowed?
162 * - Insert/replace speed:
164 * - Iterator still valid after change to container?
165 * Yes, except if the iterated-over element is removed.
166 * - Frees memory for removed elements?
168 * - Items inserted by:
170 * - Requirements for contained type
171 * -# Default constructor
172 * -# Copy constructor
177 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
178 class ACE_RB_Tree
: public ACE_RB_Tree_Base
181 friend class ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
>;
182 friend class ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
>;
183 friend class ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
>;
186 typedef INT_ID VALUE
;
187 typedef ACE_LOCK lock_type
;
188 typedef ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> ENTRY
;
190 // = ACE-style iterator typedefs.
191 typedef ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> ITERATOR
;
192 typedef ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> REVERSE_ITERATOR
;
194 // = STL-style iterator typedefs.
195 typedef ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> iterator
;
196 typedef ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> reverse_iterator
;
199 ACE_RB_Tree (ACE_Allocator
*alloc
= nullptr);
201 /// Copy constructor.
202 ACE_RB_Tree (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &rbt
);
204 /// Initialize an RB Tree.
205 int open (ACE_Allocator
*alloc
= nullptr);
207 /// Close down an RB_Tree and release dynamically allocated
212 virtual ~ACE_RB_Tree ();
214 // = insertion, removal, and search methods.
217 * Associate @a ext_id with @a int_id. If @a ext_id is already in the
218 * tree then the <ACE_RB_Tree_Node> is not changed. Returns 0 if a
219 * new entry is bound successfully, returns 1 if an attempt is made
220 * to bind an existing entry, and returns -1 if failures occur.
222 int bind (const EXT_ID
&item
,
223 const INT_ID
&int_id
);
226 * Same as a normal bind, except the tree entry is also passed back
227 * to the caller. The entry in this case will either be the newly
228 * created entry, or the existing one.
230 int bind (const EXT_ID
&ext_id
,
231 const INT_ID
&int_id
,
232 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
236 * Associate @a ext_id with @a int_id if and only if @a ext_id is not
237 * in the tree. If @a ext_id is already in the tree then the @a int_id
238 * parameter is assigned the existing value in the tree. Returns 0
239 * if a new entry is bound successfully, returns 1 if an attempt is
240 * made to bind an existing entry, and returns -1 if failures occur.
242 int trybind (const EXT_ID
&ext_id
,
246 * Same as a normal trybind, except the tree entry is also passed
247 * back to the caller. The entry in this case will either be the
248 * newly created entry, or the existing one.
250 int trybind (const EXT_ID
&ext_id
,
252 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
255 * Reassociate @a ext_id with @a int_id. If @a ext_id is not in the
256 * tree then behaves just like <bind>. Returns 0 if a new entry is
257 * bound successfully, returns 1 if an existing entry was rebound,
258 * and returns -1 if failures occur.
260 int rebind (const EXT_ID
&ext_id
,
261 const INT_ID
&int_id
);
264 * Same as a normal rebind, except the tree entry is also passed back
265 * to the caller. The entry in this case will either be the newly
266 * created entry, or the existing one.
268 int rebind (const EXT_ID
&ext_id
,
269 const INT_ID
&int_id
,
270 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
273 * Associate @a ext_id with @a int_id. If @a ext_id is not in the tree
274 * then behaves just like <bind>. Otherwise, store the old value of
275 * @a int_id into the "out" parameter and rebind the new parameters.
276 * Returns 0 if a new entry is bound successfully, returns 1 if an
277 * existing entry was rebound, and returns -1 if failures occur.
279 int rebind (const EXT_ID
&ext_id
,
280 const INT_ID
&int_id
,
284 * Same as a normal rebind, except the tree entry is also passed back
285 * to the caller. The entry in this case will either be the newly
286 * created entry, or the existing one.
288 int rebind (const EXT_ID
&ext_id
,
289 const INT_ID
&int_id
,
291 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
294 * Associate @a ext_id with @a int_id. If @a ext_id is not in the tree
295 * then behaves just like <bind>. Otherwise, store the old values
296 * of @a ext_id and @a int_id into the "out" parameters and rebind the
297 * new parameters. This is very useful if you need to have an
298 * atomic way of updating <ACE_RB_Tree_Nodes> and you also need
299 * full control over memory allocation. Returns 0 if a new entry is
300 * bound successfully, returns 1 if an existing entry was rebound,
301 * and returns -1 if failures occur.
303 int rebind (const EXT_ID
&ext_id
,
304 const INT_ID
&int_id
,
309 * Same as a normal rebind, except the tree entry is also passed back
310 * to the caller. The entry in this case will either be the newly
311 * created entry, or the existing one.
313 int rebind (const EXT_ID
&ext_id
,
314 const INT_ID
&int_id
,
317 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
319 /// Locate @a ext_id and pass out parameter via @a int_id. If found,
320 /// return 0, returns -1 if not found.
321 int find (const EXT_ID
&ext_id
,
324 /// Locate @a ext_id and pass out parameter via @a entry. If found,
325 /// return 0, returns -1 if not found.
326 int find (const EXT_ID
&ext_id
,
327 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
330 * Unbind (remove) the @a ext_id from the tree. Don't return the
331 * @a int_id to the caller (this is useful for collections where the
332 * @c int_ids are *not* dynamically allocated...)
334 int unbind (const EXT_ID
&ext_id
);
336 /// Break any association of @a ext_id. Returns the value of @a int_id
337 /// in case the caller needs to deallocate memory.
338 int unbind (const EXT_ID
&ext_id
,
342 * Remove entry from tree. This method should be used with *extreme*
343 * caution, and only for optimization purposes. The node being passed
344 * in had better have been allocated by the tree that is unbinding it.
346 int unbind (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *entry
);
348 // = Public helper methods.
350 /// Returns the current number of nodes in the tree.
351 size_t current_size () const;
353 /// Assignment operator.
354 void operator= (const ACE_RB_Tree
<EXT_ID
,
360 * Returns a reference to the underlying <ACE_LOCK>. This makes it
361 * possible to acquire the lock explicitly, which can be useful in
362 * some cases if you instantiate the ACE_Atomic_Op with an
363 * ACE_Recursive_Mutex or ACE_Process_Mutex, or if you need to
364 * guard the state of an iterator.
365 * @note The right name would be <lock>, but HP/C++ will choke on that!
369 /// Dump the state of an object.
372 // = STL styled iterator factory functions.
374 /// Return forward iterator positioned at first node in tree.
375 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> begin ();
377 /// Return forward iterator positioned at last node in tree.
378 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> end ();
380 /// Return reverse iterator positioned at last node in tree.
381 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> rbegin ();
383 /// Return reverse iterator positioned at first node in tree.
384 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> rend ();
386 /// Recursively tests the invariant red-black properties at each
387 /// node of the tree. Returns 0 if invariant holds, else -1.
388 /// This method is computationally expensive, and should only be
389 /// called for testing purposes, and not in code that depends on the
390 /// algorithmic complexity bounds provided by the other methods.
391 int test_invariant ();
393 // = DEPRECATED methods.
394 // Please migrate your code to use the new methods instead
397 * Returns a pointer to the item corresponding to the
398 * given key, or 0 if it cannot find the key in the tree.
400 * @deprecated signature will change to become
401 * int find (const EXT_ID &ext_id); which will return
402 * 0 if the @a ext_id is in the tree, otherwise -1.
404 INT_ID
* find (const EXT_ID
&k
);
407 * Inserts a *copy* of the key and the item into the tree: both the
408 * key type EXT_ID and the item type INT_ID must have well defined semantics
409 * for copy construction. The default implementation also requires that
410 * the key type support well defined < semantics. This method returns a
411 * pointer to the inserted item copy, or 0 if an error occurred.
412 * @note If an identical key already exists in the tree, no new item
413 * is created, and the returned pointer addresses the existing item
414 * associated with the existing key.
417 INT_ID
* insert (const EXT_ID
&k
, const INT_ID
&t
);
420 * Removes the item associated with the given key from the tree and
421 * destroys it. Returns 1 if it found the item and successfully
422 * destroyed it, 0 if it did not find the item, or -1 if an error
426 int remove (const EXT_ID
&k
);
429 /// Destroys all nodes and sets the root pointer null.
432 /// Declare the dynamic allocation hooks.
433 ACE_ALLOC_HOOK_DECLARE
;
436 /// Reinitialize constructor.
438 * This constructor is used to provide a valid vtable and allocator
439 * if the tree is reconstructed from shared memory. Constructor
440 * used by the derived class that has an allocator
442 ACE_RB_Tree (void *location
,
443 ACE_Allocator
*alloc
);
445 // = Protected methods. These should only be called with locks held.
447 /// Recursively tests the invariant red-black properties at each
448 /// node of the tree. Returns 0 if invariant holds, else -1.
449 int test_invariant_recurse (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * x
,
450 int & expected_black_height
,
451 int measured_black_height
);
453 /// Method for right rotation of the tree about a given node.
454 void RB_rotate_right (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * x
);
456 /// Method for left rotation of the tree about a given node.
457 void RB_rotate_left (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * x
);
459 /// Method for restoring Red-Black properties after deletion.
460 void RB_delete_fixup (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * x
,
461 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * parent
);
463 /// Method to find the successor node of the given node in the tree.
464 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *
465 RB_tree_successor (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *x
) const;
467 /// Method to find the predecessor node of the given node in the
469 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *
470 RB_tree_predecessor (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *x
) const;
472 /// Method to find the minimum node of the subtree rooted at the
474 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *
475 RB_tree_minimum (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *x
) const;
477 /// Method to find the maximum node of the subtree rooted at the
479 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *
480 RB_tree_maximum (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *x
) const;
483 * Returns a pointer to a matching node if there is one, a pointer
484 * to the node under which to insert the item if the tree is not
485 * empty and there is no such match, or 0 if the tree is empty.
486 * It stores the result of the search in the result argument:
487 * LEFT if the node is to the left of the node to be inserted,
488 * RIGHT if the node is to the right of the node to be inserted,
489 * or EXACT if an exactly matching node already exists.
491 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *find_node (const EXT_ID
&k
,
492 ACE_RB_Tree_Base::RB_SearchResult
&result
);
494 /// Rebalance the tree after insertion of a node.
495 void RB_rebalance (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * x
);
497 /// Delete children (left and right) of the node. Must be called with
499 void delete_children_i (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *parent
);
501 /// Close down an RB_Tree. this method should
502 /// only be called with locks already held.
506 * Retrieves a pointer to the item corresponding to the
507 * given key. If find_exact==1, find the exact match node,
508 * otherwise just find a match node
509 * Returns 0 for success, or -1 if it cannot find the key in the tree.
511 int find_i (const EXT_ID
&ext_id
, ACE_RB_Tree_Node
<EXT_ID
, INT_ID
>* &entry
, int find_exact
= 1);
514 * Inserts a *copy* of the key and the item into the tree: both the
515 * key type EXT_ID and the item type INT_ID must have well defined semantics
516 * for copy construction. The default implementation also requires that
517 * the key type support well defined < semantics. This method returns a
518 * pointer to the inserted item copy, or 0 if an error occurred.
519 * @note If an identical key already exists in the tree, no new item
520 * is created, and the returned pointer addresses the existing item
521 * associated with the existing key.
523 INT_ID
* insert_i (const EXT_ID
&k
, const INT_ID
&t
);
526 * Inserts a *copy* of the key and the item into the tree: both the
527 * key type EXT_ID and the item type INT_ID must have well defined semantics
528 * for copy construction. The default implementation also requires that
529 * the key type support well defined < semantics. This method passes back
530 * a pointer to the inserted (or existing) node, and the search status. If
531 * the node already exists, the method returns 1. If the node does not
532 * exist, and a new one is successfully created, and the method returns 0.
533 * If there was an error, the method returns -1.
535 int insert_i (const EXT_ID
&k
, const INT_ID
&t
,
536 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&entry
);
539 * Removes the item associated with the given key from the tree and
540 * destroys it. Returns 1 if it found the item and successfully
541 * destroyed it, 0 if it did not find the item, or -1 if an error
542 * occurred. Returns the stored internal id in the second argument.
544 int remove_i (const EXT_ID
&k
, INT_ID
&i
);
546 /// Removes the item associated with the given key from the tree and
548 int remove_i (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *z
);
550 /// Recursive function to dump the state of an object.
551 void dump_i (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *node
) const;
553 /// Function to dump node contents. Does nothing in its
554 /// basic form, but template specialization can be used to
555 /// provide definitions for various EXT_ID and INT_ID types.
556 void dump_node_i (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> &node
) const;
558 /// Less than comparison function for keys, using comparison functor.
559 int lessthan (const EXT_ID
&k1
, const EXT_ID
&k2
);
562 // = Private members.
564 /// Synchronization variable for the MT_SAFE ACE_RB_Tree.
567 /// The root of the tree.
568 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *root_
;
570 /// Comparison functor for comparing nodes in the tree.
571 COMPARE_KEYS compare_keys_
;
573 /// The current number of nodes in the tree.
574 size_t current_size_
;
578 * @class ACE_RB_Tree_Iterator_Base
580 * @brief Implements a common base class for iterators for a Red-Black Tree ADT.
582 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
583 class ACE_RB_Tree_Iterator_Base
586 /// Copy constructor.
587 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &iter
);
589 /// Assignment operator: copies both the tree reference and the position in the tree.
590 void operator= (const ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &iter
);
592 // = Iteration methods.
594 /// Returns 1 when the iteration has completed, otherwise 0.
597 /// STL-like iterator dereference operator: returns a reference
598 /// to the node underneath the iterator.
599 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> & operator* () const;
601 /// STL-like iterator dereference operator: returns a pointer
602 /// to the node underneath the iterator.
603 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> * operator-> () const;
605 /// Returns a const reference to the tree over which we're iterating.
606 const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree ();
608 /// Comparison operator: returns 1 if both iterators point to the same position, otherwise 0.
609 bool operator== (const ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &) const;
611 /// Comparison operator: returns 1 if the iterators point to different positions, otherwise 0.
612 bool operator!= (const ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &) const;
614 /// Declare the dynamic allocation hooks.
615 ACE_ALLOC_HOOK_DECLARE
;
618 /// Create the singular iterator. No valid iterator can be equal to
619 /// it, it is illegal to dereference a singular iterator, etc. etc.
620 ACE_RB_Tree_Iterator_Base ();
623 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
624 * an integer indicating (if non-zero) to position the iterator
625 * at the first element in the tree (if this integer is 0, the
626 * iterator is positioned at the last element in the tree).
628 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
632 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
633 * a pointer to a node in the tree.
635 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
636 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
>* entry
);
639 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key.
640 * The key must come first to distinguish the case of EXT_ID == int.
642 ACE_RB_Tree_Iterator_Base (const EXT_ID
& key
,
643 ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
,ACE_LOCK
> &tree
);
646 ~ACE_RB_Tree_Iterator_Base ();
648 // = Internal methods
650 /// Move forward by one element in the tree. Returns 0 when
651 /// there are no more elements in the tree, otherwise 1.
654 /// Move back by one element in the tree. Returns 0 when
655 /// there are no more elements in the tree, otherwise 1.
658 /// Dump the state of an object.
659 void dump_i () const;
661 // = Protected members.
663 /// Reference to the ACE_RB_Tree over which we're iterating.
664 const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> *tree_
;
666 /// Pointer to the node currently under the iterator.
667 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *node_
;
671 * @class ACE_RB_Tree_Iterator
673 * @brief Implements an iterator for a Red-Black Tree ADT.
675 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
676 class ACE_RB_Tree_Iterator
: public ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
>
680 * Create the singular iterator.
681 * It is illegal to deference the iterator, no valid iterator is
682 * equal to a singular iterator, etc. etc.
684 ACE_RB_Tree_Iterator ();
687 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
688 * an integer indicating (if non-zero) to position the iterator
689 * at the first element in the tree (if this integer is 0, the
690 * iterator is positioned at the last element in the tree).
692 ACE_RB_Tree_Iterator (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
695 * Constructor. Takes an ACE_RB_Tree over which to iterate
696 * and a pointer to a node in the tree.
698 ACE_RB_Tree_Iterator (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
699 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
>* entry
);
702 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key;
703 * the key comes first in order to distinguish the case of EXT_ID == int.
705 ACE_RB_Tree_Iterator (const EXT_ID
&key
,
706 ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
);
709 ~ACE_RB_Tree_Iterator ();
711 // = ACE-style iteration methods.
713 /// Move forward by one element in the tree. Returns
714 /// 0 when all elements have been seen, else 1.
717 /// Dump the state of an object.
720 // = STL-style iteration methods.
723 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> & operator++ ();
726 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> operator++ (int);
729 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> & operator-- ();
732 ACE_RB_Tree_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> operator-- (int);
734 /// Declare the dynamic allocation hooks.
735 ACE_ALLOC_HOOK_DECLARE
;
738 * Passes back the <entry> under the iterator. Returns 0 if
739 * the iteration has completed, otherwise 1. This method must
740 * be declared and defined in both the derived forward and
741 * reverse iterator classes rather than in the base iterator
742 * class because of a method signature resolution problem
743 * caused by the existence of the deprecated next ()
744 * method in the derived forward iterator class. When that
745 * deprecated method is removed, this method should be removed
746 * from the derived classes and placed in the base class.
748 int next (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&next_entry
) const;
750 // = DEPRECATED methods. Please migrate your code to use the new methods instead
753 /// Accessor for key of node under iterator (if any).
757 /// Accessor for item of node under iterator (if any).
761 /// Move to the first item in the iteration (and in the tree).
765 /// Move to the last item in the iteration (and in the tree).
769 /// Move to the next item in the iteration (and in the tree).
773 /// Move to the previous item in the iteration (and in the tree).
777 * @deprecated: use the base class <done> method instead.
778 * Returns 0 if the iterator is positioned over a valid ACE_RB_Tree
779 * node, returns 1 if not.
785 * @class ACE_RB_Tree_Reverse_Iterator
787 * @brief Implements a reverse iterator for a Red-Black Tree ADT.
789 template <class EXT_ID
, class INT_ID
, class COMPARE_KEYS
, class ACE_LOCK
>
790 class ACE_RB_Tree_Reverse_Iterator
: public ACE_RB_Tree_Iterator_Base
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
>
794 * Create the singular iterator.
795 * It is illegal to deference the iterator, no valid iterator is
796 * equal to a singular iterator, etc. etc.
798 ACE_RB_Tree_Reverse_Iterator ();
801 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
802 * an integer indicating (if non-zero) to position the iterator
803 * at the last element in the tree (if this integer is 0, the
804 * iterator is positioned at the first element in the tree).
806 ACE_RB_Tree_Reverse_Iterator (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
810 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
811 * a point to a node in the tree.
813 ACE_RB_Tree_Reverse_Iterator (const ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
,
814 ACE_RB_Tree_Node
<EXT_ID
, INT_ID
>* entry
);
817 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key;
818 * the key comes first in order to distinguish the case of EXT_ID == int.
820 ACE_RB_Tree_Reverse_Iterator (const EXT_ID
&key
,
821 ACE_RB_Tree
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> &tree
);
824 ~ACE_RB_Tree_Reverse_Iterator ();
826 // = ACE-style iteration methods.
828 /// Move forward by one element in the tree. Returns
829 /// 0 when all elements have been seen, else 1.
832 /// Dump the state of an object.
835 // = STL-style iteration methods.
838 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> & operator++ ();
841 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> operator++ (int);
844 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> & operator-- ();
847 ACE_RB_Tree_Reverse_Iterator
<EXT_ID
, INT_ID
, COMPARE_KEYS
, ACE_LOCK
> operator-- (int);
849 /// Declare the dynamic allocation hooks.
850 ACE_ALLOC_HOOK_DECLARE
;
853 * Passes back the <entry> under the iterator. Returns 0 if
854 * the iteration has completed, otherwise 1. This method must
855 * be declared and defined in both the derived forward and
856 * reverse iterator classes rather than in the base iterator
857 * class because of a method signature resolution problem
858 * caused by the existence of the deprecated next ()
859 * method in the derived forward iterator class. When that
860 * deprecated method is removed, this method should be removed
861 * from the derived classes and placed in the base class.
863 int next (ACE_RB_Tree_Node
<EXT_ID
, INT_ID
> *&next_entry
) const;
866 ACE_END_VERSIONED_NAMESPACE_DECL
868 #if defined (__ACE_INLINE__)
869 #include "ace/RB_Tree.inl"
870 #endif /* __ACE_INLINE__ */
872 #include "ace/RB_Tree.cpp"
874 #include /**/ "ace/post.h"
875 #endif /* ! defined (ACE_RB_TREE_H) */