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