Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Containers / Hash_Map.cpp
blob8d2596bfa6f436e55726d01000960c59f07d5ba7
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"
6 // Listing 1 code/ch05
7 // Little helper class.
8 template<class EXT_ID, class INT_ID>
9 class Hash_Map :
10 public ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID,
11 ACE_Hash<EXT_ID>, ACE_Equal_To<EXT_ID>, ACE_Null_Mutex>
12 {};
13 // Listing 1
15 class Hash_Map_Example
17 public:
18 // Constructor
19 Hash_Map_Example ();
21 // Illustrate the hash map.
22 int run ();
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.
31 void remove_all ();
33 private:
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");
42 map_.open (100);
44 // Listing 2
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++)
58 DataElement d;
59 map_.find (j,d);
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.
71 this->remove_all ();
73 // Iterate through the map again.
74 this->iterate_forward ();
76 return 0;
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");
108 map_.unbind_all ();
111 int ACE_TMAIN (int, ACE_TCHAR *[])
113 Hash_Map_Example me;
114 return me.run ();