1 #include "ace/Log_Msg.h"
2 #include "DataElement.h"
3 #include "RB_Tree_Functors.h"
6 #include "ace/RB_Tree.h"
9 // Little helper class.
10 template<class EXT_ID
, class INT_ID
>
11 class Tree
: public ACE_RB_Tree
<EXT_ID
, INT_ID
,
12 ACE_Less_Than
<EXT_ID
>,
20 // Illustrate the tree.
24 // Use the forward iterator.
25 void iterate_forward ();
27 // Use the reverse iterator.
28 void iterate_reverse ();
30 // Remove all elements from the tree.
34 Tree
<KeyType
, DataElement
*> tree_
;
38 int Tree_Example::run ()
40 ACE_TRACE ("Tree_Example::run");
43 for (int i
= 0; i
< 100; i
++)
45 ACE_NEW_RETURN (d
, DataElement (i
), -1);
46 int result
= tree_
.bind(i
, d
);
49 ACE_ERROR_RETURN((LM_ERROR
, "%p\n", "Bind"), -1);
53 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Using find:\n")));
54 for (int j
= 0; j
< 100; j
++)
57 int result
= tree_
.find (j
, d
);
60 ACE_ERROR_RETURN((LM_ERROR
, "%p\n", "Find"), -1);
62 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), d
->getData ()));
64 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
66 // Use the forward iterator.
67 this->iterate_forward ();
69 // Use the reverse iterator.
70 this->iterate_reverse ();
72 // Remove all elements from the tree.
73 ACE_ASSERT (this->remove_all ()!= -1);
75 // Iterate through once again.
76 this->iterate_forward ();
81 void Tree_Example::iterate_forward ()
83 ACE_TRACE ("Tree_Example::iterate_forward");
85 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Forward Iteration\n")));
86 for (Tree
<KeyType
, DataElement
*>::iterator iter
= tree_
.begin ();
87 iter
!= tree_
.end (); iter
++)
89 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).item ()->getData ()));
91 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
94 void Tree_Example::iterate_reverse ()
96 ACE_TRACE ("Tree_Example::iterate_reverse");
98 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Reverse Iteration\n")));
99 for (Tree
<KeyType
, DataElement
*>::reverse_iterator iter
= tree_
.rbegin ();
100 iter
!= tree_
.rend (); iter
++)
102 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).item ()->getData ()));
104 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
107 int Tree_Example::remove_all ()
109 ACE_TRACE ("Tree_Example::remove_all");
111 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Removing elements\n")));
112 for (int i
= 0; i
< 100; i
++)
115 int result
= tree_
.unbind (i
, d
);
118 ACE_ERROR_RETURN((LM_ERROR
, "%p\n", "Unbind"), -1);
127 int ACE_TMAIN (int, ACE_TCHAR
*[])