5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_HASH_MAP_HPP
12 #define BOOST_ASIO_DETAIL_HASH_MAP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/assert.hpp>
22 #include <boost/asio/detail/noncopyable.hpp>
24 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
25 # include <boost/asio/detail/socket_types.hpp>
26 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
28 #include <boost/asio/detail/push_options.hpp>
34 inline std::size_t calculate_hash_value(int i)
36 return static_cast<std::size_t>(i);
39 inline std::size_t calculate_hash_value(void* p)
41 return reinterpret_cast<std::size_t>(p)
42 + (reinterpret_cast<std::size_t>(p) >> 3);
45 #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
46 inline std::size_t calculate_hash_value(SOCKET s)
48 return static_cast<std::size_t>(s);
50 #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
52 // Note: assumes K and V are POD types.
53 template <typename K, typename V>
58 // The type of a value in the map.
59 typedef std::pair<K, V> value_type;
61 // The type of a non-const iterator over the hash map.
62 typedef typename std::list<value_type>::iterator iterator;
64 // The type of a const iterator over the hash map.
65 typedef typename std::list<value_type>::const_iterator const_iterator;
81 // Get an iterator for the beginning of the map.
84 return values_.begin();
87 // Get an iterator for the beginning of the map.
88 const_iterator begin() const
90 return values_.begin();
93 // Get an iterator for the end of the map.
99 // Get an iterator for the end of the map.
100 const_iterator end() const
102 return values_.end();
105 // Check whether the map is empty.
108 return values_.empty();
111 // Find an entry in the map.
112 iterator find(const K& k)
116 size_t bucket = calculate_hash_value(k) % num_buckets_;
117 iterator it = buckets_[bucket].first;
118 if (it == values_.end())
119 return values_.end();
120 iterator end_it = buckets_[bucket].last;
129 return values_.end();
132 // Find an entry in the map.
133 const_iterator find(const K& k) const
137 size_t bucket = calculate_hash_value(k) % num_buckets_;
138 const_iterator it = buckets_[bucket].first;
139 if (it == values_.end())
141 const_iterator end_it = buckets_[bucket].last;
150 return values_.end();
153 // Insert a new entry into the map.
154 std::pair<iterator, bool> insert(const value_type& v)
156 if (size_ + 1 >= num_buckets_)
157 rehash(hash_size(size_ + 1));
158 size_t bucket = calculate_hash_value(v.first) % num_buckets_;
159 iterator it = buckets_[bucket].first;
160 if (it == values_.end())
162 buckets_[bucket].first = buckets_[bucket].last =
163 values_insert(values_.end(), v);
165 return std::pair<iterator, bool>(buckets_[bucket].last, true);
167 iterator end_it = buckets_[bucket].last;
171 if (it->first == v.first)
172 return std::pair<iterator, bool>(it, false);
175 buckets_[bucket].last = values_insert(end_it, v);
177 return std::pair<iterator, bool>(buckets_[bucket].last, true);
180 // Erase an entry from the map.
181 void erase(iterator it)
183 BOOST_ASIO_ASSERT(it != values_.end());
184 BOOST_ASIO_ASSERT(num_buckets_ != 0);
186 size_t bucket = calculate_hash_value(it->first) % num_buckets_;
187 bool is_first = (it == buckets_[bucket].first);
188 bool is_last = (it == buckets_[bucket].last);
189 if (is_first && is_last)
190 buckets_[bucket].first = buckets_[bucket].last = values_.end();
192 ++buckets_[bucket].first;
194 --buckets_[bucket].last;
200 // Erase a key from the map.
201 void erase(const K& k)
203 iterator it = find(k);
204 if (it != values_.end())
208 // Remove all entries from the map.
215 // Initialise all buckets to empty.
216 iterator end_it = values_.end();
217 for (size_t i = 0; i < num_buckets_; ++i)
218 buckets_[i].first = buckets_[i].last = end_it;
222 // Calculate the hash size for the specified number of elements.
223 static std::size_t hash_size(std::size_t num_elems)
225 static std::size_t sizes[] =
227 #if defined(BOOST_ASIO_HASH_MAP_BUCKETS)
228 BOOST_ASIO_HASH_MAP_BUCKETS
229 #else // BOOST_ASIO_HASH_MAP_BUCKETS
230 3, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
231 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
233 #endif // BOOST_ASIO_HASH_MAP_BUCKETS
235 const std::size_t nth_size = sizeof(sizes) / sizeof(std::size_t) - 1;
236 for (std::size_t i = 0; i < nth_size; ++i)
237 if (num_elems < sizes[i])
239 return sizes[nth_size];
242 // Re-initialise the hash from the values already contained in the list.
243 void rehash(std::size_t num_buckets)
245 if (num_buckets == num_buckets_)
247 BOOST_ASIO_ASSERT(num_buckets != 0);
249 iterator end_iter = values_.end();
251 // Update number of buckets and initialise all buckets to empty.
252 bucket_type* tmp = new bucket_type[num_buckets];
255 num_buckets_ = num_buckets;
256 for (std::size_t i = 0; i < num_buckets_; ++i)
257 buckets_[i].first = buckets_[i].last = end_iter;
259 // Put all values back into the hash.
260 iterator iter = values_.begin();
261 while (iter != end_iter)
263 std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;
264 if (buckets_[bucket].last == end_iter)
266 buckets_[bucket].first = buckets_[bucket].last = iter++;
268 else if (++buckets_[bucket].last == iter)
274 values_.splice(buckets_[bucket].last, values_, iter++);
275 --buckets_[bucket].last;
280 // Insert an element into the values list by splicing from the spares list,
281 // if a spare is available, and otherwise by inserting a new element.
282 iterator values_insert(iterator it, const value_type& v)
286 return values_.insert(it, v);
291 values_.splice(it, spares_, spares_.begin());
296 // Erase an element from the values list by splicing it to the spares list.
297 void values_erase(iterator it)
300 spares_.splice(spares_.begin(), values_, it);
303 // The number of elements in the hash.
306 // The list of all values in the hash map.
307 std::list<value_type> values_;
309 // The list of spare nodes waiting to be recycled. Assumes that POD types only
310 // are stored in the hash map.
311 std::list<value_type> spares_;
313 // The type for a bucket in the hash table.
320 // The buckets in the hash.
321 bucket_type* buckets_;
323 // The number of buckets in the hash.
324 std::size_t num_buckets_;
327 } // namespace detail
331 #include <boost/asio/detail/pop_options.hpp>
333 #endif // BOOST_ASIO_DETAIL_HASH_MAP_HPP