1 #include "ace/RB_Tree.h"
2 #include "ace/Log_Msg.h"
4 #include "DataElement.h"
6 // Little helper class.
7 template<class EXT_ID
, class INT_ID
>
8 class Tree
: public ACE_RB_Tree
<EXT_ID
, INT_ID
,
16 // Illustrate the tree.
20 // Use the forward iterator.
21 void iterate_forward ();
23 // Use the reverse iterator.
24 void iterate_reverse ();
26 // Remove all elements from the tree.
30 Tree
<int, DataElement
*> tree_
;
33 // Listing 1 code/ch05
34 int Tree_Example::run ()
36 ACE_TRACE ("Tree_Example::run");
39 for (int i
= 0; i
< 100; i
++)
41 ACE_NEW_RETURN (d
, DataElement (i
), -1);
42 int result
= tree_
.bind (i
, d
);
45 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
51 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Using find:\n")));
52 for (int j
= 0; j
< 100; j
++)
55 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), d
->getData ()));
57 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
59 // Use the forward iterator.
60 this->iterate_forward ();
62 // Use the reverse iterator.
63 this->iterate_reverse ();
65 // Remove all elements from the tree.
66 ACE_ASSERT (this->remove_all ()!= -1);
68 // Iterate through once again.
69 this->iterate_forward ();
74 void Tree_Example::iterate_forward ()
76 ACE_TRACE ("Tree_Example::iterate_forward");
78 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Forward Iteration:\n")));
79 for (Tree
<int, DataElement
*>::iterator iter
= tree_
.begin ();
80 iter
!= tree_
.end (); iter
++)
82 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"),
83 (*iter
).item ()->getData ()));
85 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
88 void Tree_Example::iterate_reverse ()
90 ACE_TRACE ("Tree_Example::iterate_reverse");
92 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Reverse Iteration:\n")));
93 for (Tree
<int, DataElement
*>::reverse_iterator iter
95 iter
!= tree_
.rend (); iter
++)
97 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"),
98 (*iter
).item ()->getData ()));
100 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
103 int Tree_Example::remove_all ()
105 ACE_TRACE ("Tree_Example::remove_all");
106 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Removing elements\n")));
108 // Note that we can't use the iterators here as they are
109 // invalidated after deletions or insertions.
110 for (int i
= 0; i
< 100; i
++)
113 int result
= tree_
.unbind (i
, d
);
116 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
117 ACE_TEXT ("Unbind")),
128 int ACE_TMAIN (int, ACE_TCHAR
*[])