5
// Copyright (c) 2003-2008 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 ASIO_DETAIL_HASH_MAP_HPP
12
#define ASIO_DETAIL_HASH_MAP_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
24
#include <boost/functional/hash.hpp>
25
#include "asio/detail/pop_options.hpp"
27
#include "asio/detail/noncopyable.hpp"
28
#include "asio/detail/socket_types.hpp"
34
inline std::size_t calculate_hash_value(const T& t)
36
return boost::hash_value(t);
40
inline std::size_t calculate_hash_value(SOCKET s)
42
return static_cast<std::size_t>(s);
44
#endif // defined(_WIN64)
46
// Note: assumes K and V are POD types.
47
template <typename K, typename V>
52
// The type of a value in the map.
53
typedef std::pair<K, V> value_type;
55
// The type of a non-const iterator over the hash map.
56
typedef typename std::list<value_type>::iterator iterator;
58
// The type of a const iterator over the hash map.
59
typedef typename std::list<value_type>::const_iterator const_iterator;
64
// Initialise all buckets to empty.
65
for (size_t i = 0; i < num_buckets; ++i)
66
buckets_[i].first = buckets_[i].last = values_.end();
69
// Get an iterator for the beginning of the map.
72
return values_.begin();
75
// Get an iterator for the beginning of the map.
76
const_iterator begin() const
78
return values_.begin();
81
// Get an iterator for the end of the map.
87
// Get an iterator for the end of the map.
88
const_iterator end() const
93
// Check whether the map is empty.
96
return values_.empty();
99
// Find an entry in the map.
100
iterator find(const K& k)
102
size_t bucket = calculate_hash_value(k) % num_buckets;
103
iterator it = buckets_[bucket].first;
104
if (it == values_.end())
105
return values_.end();
106
iterator end = buckets_[bucket].last;
114
return values_.end();
117
// Find an entry in the map.
118
const_iterator find(const K& k) const
120
size_t bucket = calculate_hash_value(k) % num_buckets;
121
const_iterator it = buckets_[bucket].first;
122
if (it == values_.end())
124
const_iterator end = buckets_[bucket].last;
132
return values_.end();
135
// Insert a new entry into the map.
136
std::pair<iterator, bool> insert(const value_type& v)
138
size_t bucket = calculate_hash_value(v.first) % num_buckets;
139
iterator it = buckets_[bucket].first;
140
if (it == values_.end())
142
buckets_[bucket].first = buckets_[bucket].last =
143
values_insert(values_.end(), v);
144
return std::pair<iterator, bool>(buckets_[bucket].last, true);
146
iterator end = buckets_[bucket].last;
150
if (it->first == v.first)
151
return std::pair<iterator, bool>(it, false);
154
buckets_[bucket].last = values_insert(end, v);
155
return std::pair<iterator, bool>(buckets_[bucket].last, true);
158
// Erase an entry from the map.
159
void erase(iterator it)
161
assert(it != values_.end());
163
size_t bucket = calculate_hash_value(it->first) % num_buckets;
164
bool is_first = (it == buckets_[bucket].first);
165
bool is_last = (it == buckets_[bucket].last);
166
if (is_first && is_last)
167
buckets_[bucket].first = buckets_[bucket].last = values_.end();
169
++buckets_[bucket].first;
171
--buckets_[bucket].last;
176
// Remove all entries from the map.
182
// Initialise all buckets to empty.
183
for (size_t i = 0; i < num_buckets; ++i)
184
buckets_[i].first = buckets_[i].last = values_.end();
188
// Insert an element into the values list by splicing from the spares list,
189
// if a spare is available, and otherwise by inserting a new element.
190
iterator values_insert(iterator it, const value_type& v)
194
return values_.insert(it, v);
199
values_.splice(it, spares_, spares_.begin());
204
// Erase an element from the values list by splicing it to the spares list.
205
void values_erase(iterator it)
208
spares_.splice(spares_.begin(), values_, it);
211
// The list of all values in the hash map.
212
std::list<value_type> values_;
214
// The list of spare nodes waiting to be recycled. Assumes that POD types only
215
// are stored in the hash map.
216
std::list<value_type> spares_;
218
// The type for a bucket in the hash table.
225
// The number of buckets in the hash.
226
enum { num_buckets = 1021 };
228
// The buckets in the hash.
229
bucket_type buckets_[num_buckets];
232
} // namespace detail
235
#include "asio/detail/pop_options.hpp"
237
#endif // ASIO_DETAIL_HASH_MAP_HPP