Realistic 3D camera system
3D camera system components
hash_map.hpp
Go to the documentation of this file.
1 //
2 // detail/hash_map.hpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_HASH_MAP_HPP
12 #define ASIO_DETAIL_HASH_MAP_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include <list>
20 #include <utility>
21 #include "asio/detail/assert.hpp"
23 
24 #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
26 #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
27 
29 
30 namespace asio {
31 namespace detail {
32 
33 inline std::size_t calculate_hash_value(int i)
34 {
35  return static_cast<std::size_t>(i);
36 }
37 
38 inline std::size_t calculate_hash_value(void* p)
39 {
40  return reinterpret_cast<std::size_t>(p)
41  + (reinterpret_cast<std::size_t>(p) >> 3);
42 }
43 
44 #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
45 inline std::size_t calculate_hash_value(SOCKET s)
46 {
47  return static_cast<std::size_t>(s);
48 }
49 #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
50 
51 // Note: assumes K and V are POD types.
52 template <typename K, typename V>
53 class hash_map
54  : private noncopyable
55 {
56 public:
57  // The type of a value in the map.
58  typedef std::pair<K, V> value_type;
59 
60  // The type of a non-const iterator over the hash map.
61  typedef typename std::list<value_type>::iterator iterator;
62 
63  // The type of a const iterator over the hash map.
64  typedef typename std::list<value_type>::const_iterator const_iterator;
65 
66  // Constructor.
68  : size_(0),
69  buckets_(0),
70  num_buckets_(0)
71  {
72  }
73 
74  // Destructor.
76  {
77  delete[] buckets_;
78  }
79 
80  // Get an iterator for the beginning of the map.
81  iterator begin()
82  {
83  return values_.begin();
84  }
85 
86  // Get an iterator for the beginning of the map.
87  const_iterator begin() const
88  {
89  return values_.begin();
90  }
91 
92  // Get an iterator for the end of the map.
93  iterator end()
94  {
95  return values_.end();
96  }
97 
98  // Get an iterator for the end of the map.
99  const_iterator end() const
100  {
101  return values_.end();
102  }
103 
104  // Check whether the map is empty.
105  bool empty() const
106  {
107  return values_.empty();
108  }
109 
110  // Find an entry in the map.
111  iterator find(const K& k)
112  {
113  if (num_buckets_)
114  {
115  size_t bucket = calculate_hash_value(k) % num_buckets_;
116  iterator it = buckets_[bucket].first;
117  if (it == values_.end())
118  return values_.end();
119  iterator end_it = buckets_[bucket].last;
120  ++end_it;
121  while (it != end_it)
122  {
123  if (it->first == k)
124  return it;
125  ++it;
126  }
127  }
128  return values_.end();
129  }
130 
131  // Find an entry in the map.
132  const_iterator find(const K& k) const
133  {
134  if (num_buckets_)
135  {
136  size_t bucket = calculate_hash_value(k) % num_buckets_;
137  const_iterator it = buckets_[bucket].first;
138  if (it == values_.end())
139  return it;
140  const_iterator end_it = buckets_[bucket].last;
141  ++end_it;
142  while (it != end_it)
143  {
144  if (it->first == k)
145  return it;
146  ++it;
147  }
148  }
149  return values_.end();
150  }
151 
152  // Insert a new entry into the map.
153  std::pair<iterator, bool> insert(const value_type& v)
154  {
155  if (size_ + 1 >= num_buckets_)
156  rehash(hash_size(size_ + 1));
157  size_t bucket = calculate_hash_value(v.first) % num_buckets_;
158  iterator it = buckets_[bucket].first;
159  if (it == values_.end())
160  {
161  buckets_[bucket].first = buckets_[bucket].last =
162  values_insert(values_.end(), v);
163  ++size_;
164  return std::pair<iterator, bool>(buckets_[bucket].last, true);
165  }
166  iterator end_it = buckets_[bucket].last;
167  ++end_it;
168  while (it != end_it)
169  {
170  if (it->first == v.first)
171  return std::pair<iterator, bool>(it, false);
172  ++it;
173  }
174  buckets_[bucket].last = values_insert(end_it, v);
175  ++size_;
176  return std::pair<iterator, bool>(buckets_[bucket].last, true);
177  }
178 
179  // Erase an entry from the map.
180  void erase(iterator it)
181  {
182  ASIO_ASSERT(it != values_.end());
183  ASIO_ASSERT(num_buckets_ != 0);
184 
185  size_t bucket = calculate_hash_value(it->first) % num_buckets_;
186  bool is_first = (it == buckets_[bucket].first);
187  bool is_last = (it == buckets_[bucket].last);
188  if (is_first && is_last)
189  buckets_[bucket].first = buckets_[bucket].last = values_.end();
190  else if (is_first)
191  ++buckets_[bucket].first;
192  else if (is_last)
193  --buckets_[bucket].last;
194 
195  values_erase(it);
196  --size_;
197  }
198 
199  // Erase a key from the map.
200  void erase(const K& k)
201  {
202  iterator it = find(k);
203  if (it != values_.end())
204  erase(it);
205  }
206 
207  // Remove all entries from the map.
208  void clear()
209  {
210  // Clear the values.
211  values_.clear();
212  size_ = 0;
213 
214  // Initialise all buckets to empty.
215  iterator end_it = values_.end();
216  for (size_t i = 0; i < num_buckets_; ++i)
217  buckets_[i].first = buckets_[i].last = end_it;
218  }
219 
220 private:
221  // Calculate the hash size for the specified number of elements.
222  static std::size_t hash_size(std::size_t num_elems)
223  {
224  static std::size_t sizes[] =
225  {
226 #if defined(ASIO_HASH_MAP_BUCKETS)
227  ASIO_HASH_MAP_BUCKETS
228 #else // ASIO_HASH_MAP_BUCKETS
229  3, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
230  49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
231  12582917, 25165843
232 #endif // ASIO_HASH_MAP_BUCKETS
233  };
234  const std::size_t nth_size = sizeof(sizes) / sizeof(std::size_t) - 1;
235  for (std::size_t i = 0; i < nth_size; ++i)
236  if (num_elems < sizes[i])
237  return sizes[i];
238  return sizes[nth_size];
239  }
240 
241  // Re-initialise the hash from the values already contained in the list.
242  void rehash(std::size_t num_buckets)
243  {
244  if (num_buckets == num_buckets_)
245  return;
246  num_buckets_ = num_buckets;
247  ASIO_ASSERT(num_buckets_ != 0);
248 
249  iterator end_iter = values_.end();
250 
251  // Update number of buckets and initialise all buckets to empty.
252  bucket_type* tmp = new bucket_type[num_buckets_];
253  delete[] buckets_;
254  buckets_ = tmp;
255  for (std::size_t i = 0; i < num_buckets_; ++i)
256  buckets_[i].first = buckets_[i].last = end_iter;
257 
258  // Put all values back into the hash.
259  iterator iter = values_.begin();
260  while (iter != end_iter)
261  {
262  std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;
263  if (buckets_[bucket].last == end_iter)
264  {
265  buckets_[bucket].first = buckets_[bucket].last = iter++;
266  }
267  else if (++buckets_[bucket].last == iter)
268  {
269  ++iter;
270  }
271  else
272  {
273  values_.splice(buckets_[bucket].last, values_, iter++);
274  --buckets_[bucket].last;
275  }
276  }
277  }
278 
279  // Insert an element into the values list by splicing from the spares list,
280  // if a spare is available, and otherwise by inserting a new element.
281  iterator values_insert(iterator it, const value_type& v)
282  {
283  if (spares_.empty())
284  {
285  return values_.insert(it, v);
286  }
287  else
288  {
289  spares_.front() = v;
290  values_.splice(it, spares_, spares_.begin());
291  return --it;
292  }
293  }
294 
295  // Erase an element from the values list by splicing it to the spares list.
296  void values_erase(iterator it)
297  {
298  *it = value_type();
299  spares_.splice(spares_.begin(), values_, it);
300  }
301 
302  // The number of elements in the hash.
303  std::size_t size_;
304 
305  // The list of all values in the hash map.
306  std::list<value_type> values_;
307 
308  // The list of spare nodes waiting to be recycled. Assumes that POD types only
309  // are stored in the hash map.
310  std::list<value_type> spares_;
311 
312  // The type for a bucket in the hash table.
313  struct bucket_type
314  {
315  iterator first;
316  iterator last;
317  };
318 
319  // The buckets in the hash.
320  bucket_type* buckets_;
321 
322  // The number of buckets in the hash.
323  std::size_t num_buckets_;
324 };
325 
326 } // namespace detail
327 } // namespace asio
328 
330 
331 #endif // ASIO_DETAIL_HASH_MAP_HPP
#define ASIO_ASSERT(expr)
Definition: assert.hpp:29
void erase(const K &k)
Definition: hash_map.hpp:200
std::list< value_type >::const_iterator const_iterator
Definition: hash_map.hpp:64
SocketService & s
Definition: connect.hpp:521
bool empty() const
Definition: hash_map.hpp:105
std::pair< iterator, bool > insert(const value_type &v)
Definition: hash_map.hpp:153
void erase(iterator it)
Definition: hash_map.hpp:180
std::size_t calculate_hash_value(int i)
Definition: hash_map.hpp:33
const_iterator find(const K &k) const
Definition: hash_map.hpp:132
std::pair< K, V > value_type
Definition: hash_map.hpp:58
const_iterator begin() const
Definition: hash_map.hpp:87
iterator find(const K &k)
Definition: hash_map.hpp:111
const_iterator end() const
Definition: hash_map.hpp:99
std::list< value_type >::iterator iterator
Definition: hash_map.hpp:61