DDD  1.9.0.20240826145154
ext_hash_map.hh
Go to the documentation of this file.
1 #ifndef _EXT_HASH_MAP_HH_
2 #define _EXT_HASH_MAP_HH_
3 
4 #ifndef USE_STD_HASH
5 #include <ddd/google/sparse_hash_map>
6 #else
7 #include <unordered_map>
8 #endif
9 
10 #include "ddd/util/hash_support.hh"
11 #include <utility>
12 
13 template
14 <
15  typename Key,
16  typename Data,
17  typename HashKey = d3::util::hash<Key>,
18  typename EqualKey = d3::util::equal<Key>
19 >
21 {
22  // Types
23 public:
24 
25 #ifndef USE_STD_HASH
26  typedef google::sparse_hash_map<Key,Data,HashKey,EqualKey> internal_hash_map;
27 #else
28  typedef std::unordered_map<Key,Data,HashKey,EqualKey> internal_hash_map;
29 #endif
30 
31  typedef typename internal_hash_map::iterator iterator;
32  typedef typename internal_hash_map::const_iterator const_iterator;
33  typedef typename internal_hash_map::size_type size_type;
34 
36 
38  {
39  // Types
40  public:
41 
42  typedef const typename std::pair<const Key, Data> value_type;
43 
44  // Attributes
45  private:
46 
47  friend class ext_hash_map;
50 
51 
52  // Methods
53  public:
54 
56  :
57  has_result_(false),
59  {
60  }
61 
62  bool
63  empty() const
64  {
65  return ! has_result_;
66  }
67 
68  const value_type&
69  operator*() const
70  {
71  return *current_bucket_;
72  }
73 
74  const value_type*
75  operator->() const
76  {
77  return &operator*();
78  }
79 
80  void
82  {
83  // nothing to do, because there are no mutexes to release
84  }
85 
86  private:
87 
88  // cannot copy or assign a const_accessor
91 
92  };
93 
95  class accessor
96  {
97  // Types
98  public:
99  typedef typename std::pair<const Key, Data> value_type;
100 
101  // Attributes
102  private:
105 
106  friend class ext_hash_map;
107  // Methods
108  public:
109 
110  value_type&
111  operator*() const
112  {
113  return *(this->current_bucket_);
114  }
115 
116  value_type*
117  operator->() const
118  {
119  return &operator*();
120  }
121 
123  :
124  has_result_(false),
126  {
127  }
128 
129  bool
130  empty() const
131  {
132  return ! has_result_;
133  }
134 
135  void
137  {
138  // nothing to do, because there are no mutexes to release
139  }
140 
141  private:
142 
143  // cannot copy or assign a const_accessor
146 
147 
148  };
149 
151 
152  // Attributes
153 private:
154 
155  friend class const_accessor;
156  friend class accessor;
158 
159  // Methods
160 public:
161 
163  :
164  map_()
165  {
166  }
167 
168  ext_hash_map(size_t s)
169  :
170  map_(s)
171  {
172  }
173 
174  iterator
176  {
177  return map_.begin();
178  }
179 
181  begin() const
182  {
183  return map_.begin();
184  }
185 
186  iterator
187  end()
188  {
189  return map_.end();
190  }
191 
193  end() const
194  {
195  return map_.end();
196  }
197 
198  size_type
199  size() const
200  {
201  return map_.size();
202  }
203 
204  bool
205  empty() const
206  {
207  return map_.empty();
208  }
209 
210  void
212  {
213  map_.clear();
214  }
215 
216  bool
217  find( accessor& result, const Key& key)
218  {
219  iterator i = map_.find(key);
220  result.current_bucket_ = i;
221  result.has_result_ = ( i != map_.end() );
222  return result.has_result_;
223  }
224 
225  bool
226  find( const_accessor& result, const Key& key) const
227  {
228  const_iterator i = map_.find(key);
229  result.current_bucket_ = i;
230  result.has_result_ = ( i != map_.end() );
231  return result.has_result_;
232  }
233 
234  bool
235  insert( accessor& result, const Key& key)
236  {
237  std::pair<const Key, Data> value_to_insert(key,Data());
238  std::pair<iterator,bool> p(map_.insert(value_to_insert));
239  result.current_bucket_ = p.first;
240  result.has_result_ = true;
241  return p.second;
242  }
243 
244  bool
245  erase( const Key& key)
246  {
247  return map_.erase(key) > 1 ? false : true;
248  }
249 
250 #ifdef HASH_STAT
251  std::map<std::string, size_t> get_hits() const { return map_.get_hits(); }
252  std::map<std::string, size_t> get_misses() const { return map_.get_misses(); }
253  std::map<std::string, size_t> get_bounces() const { return map_.get_bounces(); }
254 #endif // HASH_STAT
255 
256 };
257 
258 #endif
Definition: ext_hash_map.hh:96
accessor & operator=(const accessor &)
bool has_result_
Definition: ext_hash_map.hh:103
iterator current_bucket_
Definition: ext_hash_map.hh:104
std::pair< const Key, Data > value_type
Definition: ext_hash_map.hh:99
accessor()
Definition: ext_hash_map.hh:122
void release()
Definition: ext_hash_map.hh:136
value_type & operator*() const
Definition: ext_hash_map.hh:111
accessor(const accessor &)
bool empty() const
Definition: ext_hash_map.hh:130
value_type * operator->() const
Definition: ext_hash_map.hh:117
Definition: ext_hash_map.hh:38
bool empty() const
Definition: ext_hash_map.hh:63
const_accessor(const const_accessor &)
const_accessor()
Definition: ext_hash_map.hh:55
const value_type * operator->() const
Definition: ext_hash_map.hh:75
bool has_result_
Definition: ext_hash_map.hh:48
const_iterator current_bucket_
Definition: ext_hash_map.hh:49
const_accessor & operator=(const const_accessor &)
const std::pair< const Key, Data > value_type
Definition: ext_hash_map.hh:42
const value_type & operator*() const
Definition: ext_hash_map.hh:69
void release()
Definition: ext_hash_map.hh:81
Definition: ext_hash_map.hh:21
iterator end()
Definition: ext_hash_map.hh:187
const_iterator end() const
Definition: ext_hash_map.hh:193
size_type size() const
Definition: ext_hash_map.hh:199
ext_hash_map()
Definition: ext_hash_map.hh:162
internal_hash_map::const_iterator const_iterator
Definition: ext_hash_map.hh:32
google::sparse_hash_map< Key, Data, HashKey, EqualKey > internal_hash_map
Definition: ext_hash_map.hh:26
bool find(accessor &result, const Key &key)
Definition: ext_hash_map.hh:217
internal_hash_map::iterator iterator
Definition: ext_hash_map.hh:31
iterator begin()
Definition: ext_hash_map.hh:175
bool empty() const
Definition: ext_hash_map.hh:205
ext_hash_map(size_t s)
Definition: ext_hash_map.hh:168
bool insert(accessor &result, const Key &key)
Definition: ext_hash_map.hh:235
bool find(const_accessor &result, const Key &key) const
Definition: ext_hash_map.hh:226
internal_hash_map::size_type size_type
Definition: ext_hash_map.hh:33
void clear()
Definition: ext_hash_map.hh:211
const_iterator begin() const
Definition: ext_hash_map.hh:181
internal_hash_map map_
Definition: ext_hash_map.hh:157
bool erase(const Key &key)
Definition: ext_hash_map.hh:245
Definition: hash_support.hh:51
Definition: hash_support.hh:40

Please comment this page and report errors about it on the RefDocComments page.
Generated on Mon Aug 26 2024 14:54:00 for DDD by doxygen 1.9.1