1 #include "ace/Hash_Map_Manager.h"
2 #include "ace/Synch.h" // needed for the lock
3 #include "ace/Functor.h"
4 #include "DataElement.h"
7 // Little helper class.
8 template<class EXT_ID
, class INT_ID
>
10 public ACE_Hash_Map_Manager_Ex
<EXT_ID
, INT_ID
,
11 ACE_Hash
<EXT_ID
>, ACE_Equal_To
<EXT_ID
>, ACE_Null_Mutex
>
15 class Hash_Map_Example
21 // Illustrate the hash map.
24 // Use the forward iterator.
25 void iterate_forward ();
27 // Use the reverse iterator.
28 void iterate_reverse ();
30 // Remove all the elements from the map.
34 Hash_Map
<int, DataElement
> map_
;
37 // Listing 2 code/ch05
38 Hash_Map_Example::Hash_Map_Example()
40 ACE_TRACE ("Hash_Map_Example::Hash_Map_Example");
46 int Hash_Map_Example::run ()
48 ACE_TRACE ("Hash_Map_Example::run");
50 for (int i
= 0; i
< 100; i
++)
52 map_
.bind (i
, DataElement(i
));
55 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Map has\n")));
56 for (int j
= 0; j
< 100; j
++)
60 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), d
.getData ()));
62 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
64 // Use the forward iterator.
65 this->iterate_forward ();
67 // Use the reverse iterator.
68 this->iterate_reverse ();
70 // Remove all the elements from the map.
73 // Iterate through the map again.
74 this->iterate_forward ();
79 void Hash_Map_Example::iterate_forward ()
81 ACE_TRACE ("Hash_Map_Example::iterate_forward");
83 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Forward Iteration\n")));
84 for (Hash_Map
<int, DataElement
>::iterator iter
= map_
.begin ();
85 iter
!= map_
.end (); iter
++)
87 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).int_id_
.getData ()));
89 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
92 void Hash_Map_Example::iterate_reverse ()
94 ACE_TRACE ("Hash_Map_Example::iterate_reverse");
96 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Reverse Iteration\n")));
97 for (Hash_Map
<int, DataElement
>::reverse_iterator iter
= map_
.rbegin ();
98 iter
!= map_
.rend (); iter
++)
100 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).int_id_
.getData ()));
102 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
105 void Hash_Map_Example::remove_all ()
107 ACE_TRACE ("Hash_Map_Example::remove_all");
111 int ACE_TMAIN (int, ACE_TCHAR
*[])