GitHub Actions: Try MSVC builds with /std:c++17 and 20
[ACE_TAO.git] / ACE / ace / RB_Tree.h
blobb3b08f19aab57de0a5444c95cdad9400b0d10dd8
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file RB_Tree.h
7 * @author Chris Gill
8 */
9 //=============================================================================
12 #ifndef ACE_RB_TREE_H
13 #define ACE_RB_TREE_H
14 #include /**/ "ace/pre.h"
16 #include "ace/Global_Macros.h"
17 #include "ace/Functor_T.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 # pragma once
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
25 // Forward decl.
26 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
27 class ACE_RB_Tree_Iterator_Base;
29 // Forward decl.
30 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
31 class ACE_RB_Tree_Iterator;
33 // Forward decl.
34 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
35 class ACE_RB_Tree_Reverse_Iterator;
37 // Forward decl.
38 class ACE_Allocator;
40 class ACE_RB_Tree_Node_Base
42 public:
43 enum RB_Tree_Node_Color {RED, BLACK};
46 /**
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
54 public:
55 /// Constructor.
56 ACE_RB_Tree_Node (const EXT_ID &k, const INT_ID &t);
58 /// Destructor.
59 ~ACE_RB_Tree_Node (void);
61 /// Key accessor.
62 EXT_ID &key (void);
64 /// Item accessor.
65 INT_ID &item (void);
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 (void);
73 /// Accessor for node's parent pointer.
74 ACE_RB_Tree_Node<EXT_ID, INT_ID> *parent (void);
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 (void);
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 (void);
88 /// Mutator for node's right child pointer
89 void right (ACE_RB_Tree_Node<EXT_ID, INT_ID> * r);
91 private:
93 /// The key.
94 EXT_ID k_;
96 /// The item.
97 INT_ID t_;
99 /// Color of the node.
100 RB_Tree_Node_Color color_;
102 /// Pointer to node's parent.
103 ACE_RB_Tree_Node<EXT_ID, INT_ID> *parent_;
105 /// Pointer to node's left child.
106 ACE_RB_Tree_Node<EXT_ID, INT_ID> *left_;
108 /// Pointer to node's right child.
109 ACE_RB_Tree_Node<EXT_ID, INT_ID> *right_;
112 class ACE_RB_Tree_Base
114 public:
115 /// Search result enumeration.
116 enum RB_SearchResult {LEFT, EXACT, RIGHT};
118 /// Get the allocator;
120 * @note This method is inlined here rather than in RB_Tree.inl
121 * since that file may be included multiple times when
122 * inlining is disabled and on platforms where
123 * @c ACE_TEMPLATES_REQUIRE_SOURCE is defined. In those
124 * platform/configuration combinations, multiple definitions
125 * of this method occurred. Placing the definition inline in
126 * the header avoids such errors.
128 ACE_Allocator * allocator (void) const { return this->allocator_; }
130 protected:
131 // = Protected members.
133 /// Pointer to a memory allocator.
134 ACE_Allocator *allocator_;
138 * @class ACE_RB_Tree
140 * @brief Implements a Red-Black Tree ADT, according to T. H. Corman,
141 * C. E. Leiserson, and R. L. Rivest, "Introduction to Algorithms"
142 * 1990, MIT, chapter 14.
144 * A number of Changes have been made to this class template
145 * in order to conform to the ACE_Hash_Map_Manager_Ex
146 * interface. All previously supported public methods are
147 * still part of this class. However, these are marked as
148 * DEPRECATED and will be removed from this class in
149 * a future version of ACE. Please migrate your code
150 * to the appropriate public methods indicated in the
151 * method deprecation comments.
152 * This class uses an ACE_Allocator to allocate memory. The
153 * user can make this a persistent class by providing an
154 * ACE_Allocator with a persistable memory pool.
156 * <b> Requirements and Performance Characteristics</b>
157 * - Internal Structure:
158 * Binary tree
159 * - Duplicates allowed?
160 * No
161 * - Random access allowed?
162 * No
163 * - Search speed:
164 * Log(n)
165 * - Insert/replace speed:
166 * Log(n)
167 * - Iterator still valid after change to container?
168 * Yes, except if the iterated-over element is removed.
169 * - Frees memory for removed elements?
170 * Yes
171 * - Items inserted by:
172 * Value
173 * - Requirements for contained type
174 * -# Default constructor
175 * -# Copy constructor
176 * -# operator=
177 * -# operator==
178 * -# operator<
180 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
181 class ACE_RB_Tree : public ACE_RB_Tree_Base
184 public:
185 friend class ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK>;
186 friend class ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK>;
187 friend class ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK>;
189 typedef EXT_ID KEY;
190 typedef INT_ID VALUE;
191 typedef ACE_LOCK lock_type;
192 typedef ACE_RB_Tree_Node<EXT_ID, INT_ID> ENTRY;
194 // = ACE-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;
198 // = STL-style iterator typedefs.
199 typedef ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> iterator;
200 typedef ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> reverse_iterator;
202 /// Constructor.
203 ACE_RB_Tree (ACE_Allocator *alloc = 0);
205 /// Copy constructor.
206 ACE_RB_Tree (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &rbt);
208 /// Initialize an RB Tree.
209 int open (ACE_Allocator *alloc = 0);
211 /// Close down an RB_Tree and release dynamically allocated
212 /// resources.
213 int close (void);
215 /// Destructor.
216 virtual ~ACE_RB_Tree (void);
218 // = insertion, removal, and search methods.
221 * Associate @a ext_id with @a int_id. If @a ext_id is already in the
222 * tree then the <ACE_RB_Tree_Node> is not changed. Returns 0 if a
223 * new entry is bound successfully, returns 1 if an attempt is made
224 * to bind an existing entry, and returns -1 if failures occur.
226 int bind (const EXT_ID &item,
227 const INT_ID &int_id);
230 * Same as a normal bind, except the tree entry is also passed back
231 * to the caller. The entry in this case will either be the newly
232 * created entry, or the existing one.
234 int bind (const EXT_ID &ext_id,
235 const INT_ID &int_id,
236 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
240 * Associate @a ext_id with @a int_id if and only if @a ext_id is not
241 * in the tree. If @a ext_id is already in the tree then the @a int_id
242 * parameter is assigned the existing value in the tree. Returns 0
243 * if a new entry is bound successfully, returns 1 if an attempt is
244 * made to bind an existing entry, and returns -1 if failures occur.
246 int trybind (const EXT_ID &ext_id,
247 INT_ID &int_id);
250 * Same as a normal trybind, except the tree entry is also passed
251 * back to the caller. The entry in this case will either be the
252 * newly created entry, or the existing one.
254 int trybind (const EXT_ID &ext_id,
255 INT_ID &int_id,
256 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
259 * Reassociate @a ext_id with @a int_id. If @a ext_id is not in the
260 * tree then behaves just like <bind>. Returns 0 if a new entry is
261 * bound successfully, returns 1 if an existing entry was rebound,
262 * and returns -1 if failures occur.
264 int rebind (const EXT_ID &ext_id,
265 const INT_ID &int_id);
268 * Same as a normal rebind, except the tree entry is also passed back
269 * to the caller. The entry in this case will either be the newly
270 * created entry, or the existing one.
272 int rebind (const EXT_ID &ext_id,
273 const INT_ID &int_id,
274 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
277 * Associate @a ext_id with @a int_id. If @a ext_id is not in the tree
278 * then behaves just like <bind>. Otherwise, store the old value of
279 * @a int_id into the "out" parameter and rebind the new parameters.
280 * Returns 0 if a new entry is bound successfully, returns 1 if an
281 * existing entry was rebound, and returns -1 if failures occur.
283 int rebind (const EXT_ID &ext_id,
284 const INT_ID &int_id,
285 INT_ID &old_int_id);
288 * Same as a normal rebind, except the tree entry is also passed back
289 * to the caller. The entry in this case will either be the newly
290 * created entry, or the existing one.
292 int rebind (const EXT_ID &ext_id,
293 const INT_ID &int_id,
294 INT_ID &old_int_id,
295 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
298 * Associate @a ext_id with @a int_id. If @a ext_id is not in the tree
299 * then behaves just like <bind>. Otherwise, store the old values
300 * of @a ext_id and @a int_id into the "out" parameters and rebind the
301 * new parameters. This is very useful if you need to have an
302 * atomic way of updating <ACE_RB_Tree_Nodes> and you also need
303 * full control over memory allocation. Returns 0 if a new entry is
304 * bound successfully, returns 1 if an existing entry was rebound,
305 * and returns -1 if failures occur.
307 int rebind (const EXT_ID &ext_id,
308 const INT_ID &int_id,
309 EXT_ID &old_ext_id,
310 INT_ID &old_int_id);
313 * Same as a normal rebind, except the tree entry is also passed back
314 * to the caller. The entry in this case will either be the newly
315 * created entry, or the existing one.
317 int rebind (const EXT_ID &ext_id,
318 const INT_ID &int_id,
319 EXT_ID &old_ext_id,
320 INT_ID &old_int_id,
321 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
323 /// Locate @a ext_id and pass out parameter via @a int_id. If found,
324 /// return 0, returns -1 if not found.
325 int find (const EXT_ID &ext_id,
326 INT_ID &int_id);
328 /// Locate @a ext_id and pass out parameter via @a entry. If found,
329 /// return 0, returns -1 if not found.
330 int find (const EXT_ID &ext_id,
331 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
334 * Unbind (remove) the @a ext_id from the tree. Don't return the
335 * @a int_id to the caller (this is useful for collections where the
336 * @c int_ids are *not* dynamically allocated...)
338 int unbind (const EXT_ID &ext_id);
340 /// Break any association of @a ext_id. Returns the value of @a int_id
341 /// in case the caller needs to deallocate memory.
342 int unbind (const EXT_ID &ext_id,
343 INT_ID &int_id);
346 * Remove entry from tree. This method should be used with *extreme*
347 * caution, and only for optimization purposes. The node being passed
348 * in had better have been allocated by the tree that is unbinding it.
350 int unbind (ACE_RB_Tree_Node<EXT_ID, INT_ID> *entry);
352 // = Public helper methods.
354 /// Returns the current number of nodes in the tree.
355 size_t current_size (void) const;
357 /// Assignment operator.
358 void operator= (const ACE_RB_Tree<EXT_ID,
359 INT_ID,
360 COMPARE_KEYS,
361 ACE_LOCK> &rbt);
364 * Returns a reference to the underlying <ACE_LOCK>. This makes it
365 * possible to acquire the lock explicitly, which can be useful in
366 * some cases if you instantiate the ACE_Atomic_Op with an
367 * ACE_Recursive_Mutex or ACE_Process_Mutex, or if you need to
368 * guard the state of an iterator.
369 * @note The right name would be <lock>, but HP/C++ will choke on that!
371 ACE_LOCK &mutex (void);
373 /// Dump the state of an object.
374 void dump (void) const;
376 // = STL styled iterator factory functions.
378 /// Return forward iterator positioned at first node in tree.
379 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> begin (void);
381 /// Return forward iterator positioned at last node in tree.
382 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> end (void);
384 /// Return reverse iterator positioned at last node in tree.
385 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> rbegin (void);
387 /// Return reverse iterator positioned at first node in tree.
388 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> rend (void);
390 /// Recursively tests the invariant red-black properties at each
391 /// node of the tree. Returns 0 if invariant holds, else -1.
392 /// This method is computationally expensive, and should only be
393 /// called for testing purposes, and not in code that depends on the
394 /// algorithmic complexity bounds provided by the other methods.
395 int test_invariant (void);
397 // = DEPRECATED methods.
398 // Please migrate your code to use the new methods instead
401 * Returns a pointer to the item corresponding to the
402 * given key, or 0 if it cannot find the key in the tree.
404 * @deprecated signature will change to become
405 * int find (const EXT_ID &ext_id); which will return
406 * 0 if the @a ext_id is in the tree, otherwise -1.
408 INT_ID* find (const EXT_ID &k);
411 * Inserts a *copy* of the key and the item into the tree: both the
412 * key type EXT_ID and the item type INT_ID must have well defined semantics
413 * for copy construction. The default implementation also requires that
414 * the key type support well defined < semantics. This method returns a
415 * pointer to the inserted item copy, or 0 if an error occurred.
416 * @note If an identical key already exists in the tree, no new item
417 * is created, and the returned pointer addresses the existing item
418 * associated with the existing key.
419 * @deprecated
421 INT_ID* insert (const EXT_ID &k, const INT_ID &t);
424 * Removes the item associated with the given key from the tree and
425 * destroys it. Returns 1 if it found the item and successfully
426 * destroyed it, 0 if it did not find the item, or -1 if an error
427 * occurred.
428 * @deprecated
430 int remove (const EXT_ID &k);
432 /// @deprecated
433 /// Destroys all nodes and sets the root pointer null.
434 void clear (void);
436 /// Declare the dynamic allocation hooks.
437 ACE_ALLOC_HOOK_DECLARE;
439 protected:
440 /// Reinitialize constructor.
442 * This constructor is used to provide a valid vtable and allocator
443 * if the tree is reconstructed from shared memory. Constructor
444 * used by the derived class that has an allocator
446 ACE_RB_Tree (void *location,
447 ACE_Allocator *alloc);
449 // = Protected methods. These should only be called with locks held.
451 /// Recursively tests the invariant red-black properties at each
452 /// node of the tree. Returns 0 if invariant holds, else -1.
453 int test_invariant_recurse (ACE_RB_Tree_Node<EXT_ID, INT_ID> * x,
454 int & expected_black_height,
455 int measured_black_height);
457 /// Method for right rotation of the tree about a given node.
458 void RB_rotate_right (ACE_RB_Tree_Node<EXT_ID, INT_ID> * x);
460 /// Method for left rotation of the tree about a given node.
461 void RB_rotate_left (ACE_RB_Tree_Node<EXT_ID, INT_ID> * x);
463 /// Method for restoring Red-Black properties after deletion.
464 void RB_delete_fixup (ACE_RB_Tree_Node<EXT_ID, INT_ID> * x,
465 ACE_RB_Tree_Node<EXT_ID, INT_ID> * parent);
467 /// Method to find the successor node of the given node in the tree.
468 ACE_RB_Tree_Node<EXT_ID, INT_ID> *
469 RB_tree_successor (ACE_RB_Tree_Node<EXT_ID, INT_ID> *x) const;
471 /// Method to find the predecessor node of the given node in the
472 /// tree.
473 ACE_RB_Tree_Node<EXT_ID, INT_ID> *
474 RB_tree_predecessor (ACE_RB_Tree_Node<EXT_ID, INT_ID> *x) const;
476 /// Method to find the minimum node of the subtree rooted at the
477 /// given node.
478 ACE_RB_Tree_Node<EXT_ID, INT_ID> *
479 RB_tree_minimum (ACE_RB_Tree_Node<EXT_ID, INT_ID> *x) const;
481 /// Method to find the maximum node of the subtree rooted at the
482 /// given node.
483 ACE_RB_Tree_Node<EXT_ID, INT_ID> *
484 RB_tree_maximum (ACE_RB_Tree_Node<EXT_ID, INT_ID> *x) const;
487 * Returns a pointer to a matching node if there is one, a pointer
488 * to the node under which to insert the item if the tree is not
489 * empty and there is no such match, or 0 if the tree is empty.
490 * It stores the result of the search in the result argument:
491 * LEFT if the node is to the left of the node to be inserted,
492 * RIGHT if the node is to the right of the node to be inserted,
493 * or EXACT if an exactly matching node already exists.
495 ACE_RB_Tree_Node<EXT_ID, INT_ID> *find_node (const EXT_ID &k,
496 ACE_RB_Tree_Base::RB_SearchResult &result);
498 /// Rebalance the tree after insertion of a node.
499 void RB_rebalance (ACE_RB_Tree_Node<EXT_ID, INT_ID> * x);
501 /// Delete children (left and right) of the node. Must be called with
502 /// lock held.
503 void delete_children_i (ACE_RB_Tree_Node<EXT_ID, INT_ID> *parent);
505 /// Close down an RB_Tree. this method should
506 /// only be called with locks already held.
507 int close_i (void);
510 * Retrieves a pointer to the item corresponding to the
511 * given key. If find_exact==1, find the exact match node,
512 * otherwise just find a match node
513 * Returns 0 for success, or -1 if it cannot find the key in the tree.
515 int find_i (const EXT_ID &ext_id, ACE_RB_Tree_Node<EXT_ID, INT_ID>* &entry, int find_exact = 1);
518 * Inserts a *copy* of the key and the item into the tree: both the
519 * key type EXT_ID and the item type INT_ID must have well defined semantics
520 * for copy construction. The default implementation also requires that
521 * the key type support well defined < semantics. This method returns a
522 * pointer to the inserted item copy, or 0 if an error occurred.
523 * @note If an identical key already exists in the tree, no new item
524 * is created, and the returned pointer addresses the existing item
525 * associated with the existing key.
527 INT_ID* insert_i (const EXT_ID &k, const INT_ID &t);
530 * Inserts a *copy* of the key and the item into the tree: both the
531 * key type EXT_ID and the item type INT_ID must have well defined semantics
532 * for copy construction. The default implementation also requires that
533 * the key type support well defined < semantics. This method passes back
534 * a pointer to the inserted (or existing) node, and the search status. If
535 * the node already exists, the method returns 1. If the node does not
536 * exist, and a new one is successfully created, and the method returns 0.
537 * If there was an error, the method returns -1.
539 int insert_i (const EXT_ID &k, const INT_ID &t,
540 ACE_RB_Tree_Node<EXT_ID, INT_ID> *&entry);
543 * Removes the item associated with the given key from the tree and
544 * destroys it. Returns 1 if it found the item and successfully
545 * destroyed it, 0 if it did not find the item, or -1 if an error
546 * occurred. Returns the stored internal id in the second argument.
548 int remove_i (const EXT_ID &k, INT_ID &i);
550 /// Removes the item associated with the given key from the tree and
551 /// destroys it.
552 int remove_i (ACE_RB_Tree_Node<EXT_ID, INT_ID> *z);
554 /// Recursive function to dump the state of an object.
555 void dump_i (ACE_RB_Tree_Node<EXT_ID, INT_ID> *node) const;
557 /// Function to dump node contents. Does nothing in its
558 /// basic form, but template specialization can be used to
559 /// provide definitions for various EXT_ID and INT_ID types.
560 void dump_node_i (ACE_RB_Tree_Node<EXT_ID, INT_ID> &node) const;
562 /// Less than comparison function for keys, using comparison functor.
563 int lessthan (const EXT_ID &k1, const EXT_ID &k2);
565 private:
567 // = Private members.
569 /// Synchronization variable for the MT_SAFE ACE_RB_Tree.
570 ACE_LOCK lock_;
572 /// The root of the tree.
573 ACE_RB_Tree_Node<EXT_ID, INT_ID> *root_;
575 /// Comparison functor for comparing nodes in the tree.
576 COMPARE_KEYS compare_keys_;
578 /// The current number of nodes in the tree.
579 size_t current_size_;
583 * @class ACE_RB_Tree_Iterator_Base
585 * @brief Implements a common base class for iterators for a Red-Black Tree ADT.
587 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
588 class ACE_RB_Tree_Iterator_Base
591 public:
593 /// Copy constructor.
594 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &iter);
596 /// Assignment operator: copies both the tree reference and the position in the tree.
597 void operator= (const ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &iter);
599 // = Iteration methods.
601 /// Returns 1 when the iteration has completed, otherwise 0.
602 int done (void) const;
604 /// STL-like iterator dereference operator: returns a reference
605 /// to the node underneath the iterator.
606 ACE_RB_Tree_Node<EXT_ID, INT_ID> & operator* (void) const;
608 /// STL-like iterator dereference operator: returns a pointer
609 /// to the node underneath the iterator.
610 ACE_RB_Tree_Node<EXT_ID, INT_ID> * operator-> (void) const;
612 /// Returns a const reference to the tree over which we're iterating.
613 const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree (void);
615 /// Comparison operator: returns 1 if both iterators point to the same position, otherwise 0.
616 bool operator== (const ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &) const;
618 /// Comparison operator: returns 1 if the iterators point to different positions, otherwise 0.
619 bool operator!= (const ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &) const;
621 /// Declare the dynamic allocation hooks.
622 ACE_ALLOC_HOOK_DECLARE;
624 protected:
625 /// Create the singular iterator. No valid iterator can be equal to
626 /// it, it is illegal to dereference a singular iterator, etc. etc.
627 ACE_RB_Tree_Iterator_Base (void);
630 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
631 * an integer indicating (if non-zero) to position the iterator
632 * at the first element in the tree (if this integer is 0, the
633 * iterator is positioned at the last element in the tree).
635 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
636 int set_first);
639 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
640 * a pointer to a node in the tree.
642 ACE_RB_Tree_Iterator_Base (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
643 ACE_RB_Tree_Node<EXT_ID, INT_ID>* entry);
646 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key.
647 * The key must come first to distinguish the case of EXT_ID == int.
649 ACE_RB_Tree_Iterator_Base (const EXT_ID& key,
650 ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS,ACE_LOCK> &tree);
652 /// Destructor.
653 ~ACE_RB_Tree_Iterator_Base (void);
655 // = Internal methods
657 /// Move forward by one element in the tree. Returns 0 when
658 /// there are no more elements in the tree, otherwise 1.
659 int forward_i (void);
661 /// Move back by one element in the tree. Returns 0 when
662 /// there are no more elements in the tree, otherwise 1.
663 int reverse_i (void);
665 /// Dump the state of an object.
666 void dump_i (void) const;
668 // = Protected members.
670 /// Reference to the ACE_RB_Tree over which we're iterating.
671 const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> *tree_;
673 /// Pointer to the node currently under the iterator.
674 ACE_RB_Tree_Node <EXT_ID, INT_ID> *node_;
679 * @class ACE_RB_Tree_Iterator
681 * @brief Implements an iterator for a Red-Black Tree ADT.
683 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
684 class ACE_RB_Tree_Iterator : public ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK>
686 public:
688 * Create the singular iterator.
689 * It is illegal to deference the iterator, no valid iterator is
690 * equal to a singular iterator, etc. etc.
692 ACE_RB_Tree_Iterator (void);
695 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
696 * an integer indicating (if non-zero) to position the iterator
697 * at the first element in the tree (if this integer is 0, the
698 * iterator is positioned at the last element in the tree).
700 ACE_RB_Tree_Iterator (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
701 int set_first = 1);
703 * Constructor. Takes an ACE_RB_Tree over which to iterate
704 * and a pointer to a node in the tree.
706 ACE_RB_Tree_Iterator (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
707 ACE_RB_Tree_Node<EXT_ID, INT_ID>* entry);
710 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key;
711 * the key comes first in order to distinguish the case of EXT_ID == int.
713 ACE_RB_Tree_Iterator (const EXT_ID &key,
714 ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree);
716 /// Destructor.
717 ~ACE_RB_Tree_Iterator (void);
719 // = ACE-style iteration methods.
721 /// Move forward by one element in the tree. Returns
722 /// 0 when all elements have been seen, else 1.
723 int advance (void);
725 /// Dump the state of an object.
726 void dump (void) const;
728 // = STL-style iteration methods.
730 /// Prefix advance.
731 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> & operator++ (void);
733 /// Postfix advance.
734 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> operator++ (int);
736 /// Prefix reverse.
737 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> & operator-- (void);
739 /// Postfix reverse.
740 ACE_RB_Tree_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> operator-- (int);
742 /// Declare the dynamic allocation hooks.
743 ACE_ALLOC_HOOK_DECLARE;
746 * Passes back the <entry> under the iterator. Returns 0 if
747 * the iteration has completed, otherwise 1. This method must
748 * be declared and defined in both the derived forward and
749 * reverse iterator classes rather than in the base iterator
750 * class because of a method signature resolution problem
751 * caused by the existence of the deprecated next (void)
752 * method in the derived forward iterator class. When that
753 * deprecated method is removed, this method should be removed
754 * from the derived classes and placed in the base class.
756 int next (ACE_RB_Tree_Node<EXT_ID, INT_ID> *&next_entry) const;
758 // = DEPRECATED methods. Please migrate your code to use the new methods instead
760 /// @deprecated
761 /// Accessor for key of node under iterator (if any).
762 EXT_ID *key (void);
764 /// @deprecated
765 /// Accessor for item of node under iterator (if any).
766 INT_ID *item (void);
768 /// @deprecated
769 /// Move to the first item in the iteration (and in the tree).
770 int first (void);
772 /// @deprecated
773 /// Move to the last item in the iteration (and in the tree).
774 int last (void);
776 /// @deprecated
777 /// Move to the next item in the iteration (and in the tree).
778 int next (void);
780 /// @deprecated
781 /// Move to the previous item in the iteration (and in the tree).
782 int previous (void);
785 * @deprecated: use the base class <done> method instead.
786 * Returns 0 if the iterator is positioned over a valid ACE_RB_Tree
787 * node, returns 1 if not.
789 int is_done (void);
794 * @class ACE_RB_Tree_Reverse_Iterator
796 * @brief Implements a reverse iterator for a Red-Black Tree ADT.
798 template <class EXT_ID, class INT_ID, class COMPARE_KEYS, class ACE_LOCK>
799 class ACE_RB_Tree_Reverse_Iterator : public ACE_RB_Tree_Iterator_Base<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK>
801 public:
803 * Create the singular iterator.
804 * It is illegal to deference the iterator, no valid iterator is
805 * equal to a singular iterator, etc. etc.
807 ACE_RB_Tree_Reverse_Iterator (void);
810 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
811 * an integer indicating (if non-zero) to position the iterator
812 * at the last element in the tree (if this integer is 0, the
813 * iterator is positioned at the first element in the tree).
815 ACE_RB_Tree_Reverse_Iterator (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
816 int set_last = 1);
819 * Constructor. Takes an ACE_RB_Tree over which to iterate, and
820 * a point to a node in the tree.
822 ACE_RB_Tree_Reverse_Iterator (const ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree,
823 ACE_RB_Tree_Node<EXT_ID, INT_ID>* entry);
826 * Constructor. Takes an ACE_RB_Tree over which to iterate, and a key;
827 * the key comes first in order to distinguish the case of EXT_ID == int.
829 ACE_RB_Tree_Reverse_Iterator (const EXT_ID &key,
830 ACE_RB_Tree<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> &tree);
832 /// Destructor.
833 ~ACE_RB_Tree_Reverse_Iterator (void);
835 // = ACE-style iteration methods.
837 /// Move forward by one element in the tree. Returns
838 /// 0 when all elements have been seen, else 1.
839 int advance (void);
841 /// Dump the state of an object.
842 void dump (void) const;
844 // = STL-style iteration methods.
846 /// Prefix advance.
847 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> & operator++ (void);
849 /// Postfix advance.
850 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> operator++ (int);
852 /// Prefix reverse.
853 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> & operator-- (void);
855 /// Postfix reverse.
856 ACE_RB_Tree_Reverse_Iterator<EXT_ID, INT_ID, COMPARE_KEYS, ACE_LOCK> operator-- (int);
858 /// Declare the dynamic allocation hooks.
859 ACE_ALLOC_HOOK_DECLARE;
862 * Passes back the <entry> under the iterator. Returns 0 if
863 * the iteration has completed, otherwise 1. This method must
864 * be declared and defined in both the derived forward and
865 * reverse iterator classes rather than in the base iterator
866 * class because of a method signature resolution problem
867 * caused by the existence of the deprecated next (void)
868 * method in the derived forward iterator class. When that
869 * deprecated method is removed, this method should be removed
870 * from the derived classes and placed in the base class.
872 int next (ACE_RB_Tree_Node<EXT_ID, INT_ID> *&next_entry) const;
876 ACE_END_VERSIONED_NAMESPACE_DECL
878 #if defined (__ACE_INLINE__)
879 #include "ace/RB_Tree.inl"
880 #endif /* __ACE_INLINE__ */
882 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
883 #include "ace/RB_Tree.cpp"
884 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
886 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
887 #pragma implementation ("RB_Tree.cpp")
888 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
890 #include /**/ "ace/post.h"
891 #endif /* ! defined (ACE_RB_TREE_H) */