summaryrefslogtreecommitdiff
path: root/examples/APG/Containers/Hash_Map_Hash.cpp
blob: 4971954e21297895fa30ca048e34cbe79f16467c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// $Id$

#include "ace/Hash_Map_Manager.h"
#include "ace/Synch.h" // Needed for the lock
#include "ace/Functor.h"
#include "DataElement.h"
#include "Hash_Map_Hash.h"

// Little helper class
template<class EXT_ID, class INT_ID>
class Hash_Map :
      public ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID,
      ACE_Hash<EXT_ID>, ACE_Equal_To<EXT_ID>, ACE_Null_Mutex>
{};


class Hash_Map_Example
{
public:
  ~Hash_Map_Example ()
  {
    map_.close ();
  }

  // illustrate the hash map
  int run (void);

  // use the forward iterate
  void iterate_forward (void);

  // use the reverse iterator
  void iterate_reverse (void);

  // remove all the elements from the map
  void remove_all (void);

private:
  Hash_Map<KeyType, DataElement> map_;
};

int Hash_Map_Example::run (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::run"));

  for (int i = 0; i < 100; i++)
    {
      map_.bind (i, DataElement (i));
    }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Map has \n")));
  for (int j = 0; j < 100; j++)
    {
      DataElement d;
      map_.find (j, d);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));

  // Use the forward iterator.
  this->iterate_forward ();

  // Use the reverse iterator.
  this->iterate_reverse ();

  // Remove all the elements from the map.
  this->remove_all ();

  // Iterate through the map again.
  this->iterate_forward ();

  return 0;
}

void Hash_Map_Example::iterate_forward (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::iterate_forward"));

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward Iteration \n")));
  for (Hash_Map<KeyType, DataElement>::iterator iter = map_.begin ();
       iter != map_.end (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
}

void Hash_Map_Example::iterate_reverse (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::iterate_reverse"));

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration \n")));
  for (Hash_Map<KeyType, DataElement>::reverse_iterator iter = map_.rbegin ();
       iter != map_.rend (); iter++)
    {
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
}

void Hash_Map_Example::remove_all (void)
{
  ACE_TRACE (ACE_TEXT ("Hash_Map_Example::remove_all"));
  map_.unbind_all ();
}

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  Hash_Map_Example me;
  return me.run ();
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Manager_Ex<KeyType, DataElement, ACE_Hash<KeyType>, ACE_Equal_To<KeyType>, ACE_Null_Mutex>;
template class Hash_Map <KeyType, DataElement>;
template class ACE_Hash_Map_Entry<KeyType,DataElement>;
template class ACE_Hash_Map_Iterator<KeyType,DataElement,ACE_Null_Mutex>;
template class ACE_Hash_Map_Reverse_Iterator<KeyType,DataElement,ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator_Base_Ex<KeyType, DataElement, ACE_Hash<KeyType>, ACE_Equal_To<KeyType>, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate Hash_Map <KeyType, DataElement>
#pragma instantiate ACE_Hash_Map_Manager_Ex<KeyType, DataElement,ACE_Hash<KeyType>, ACE_Equal_To<KeyType>, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Entry<KeyType,DataElement>
#pragma instantiate ACE_Hash_Map_Iterator<KeyType,DataElement,ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<KeyType,DataElement,ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator_Base_Ex<KeyType, DataElement, ACE_Hash<KeyType>, ACE_Equal_To<KeyType>, ACE_Null_Mutex>;
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION*/