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) 24 #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) 26 #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) 35 return static_cast<std::size_t
>(i);
40 return reinterpret_cast<std::size_t
>(p)
41 + (reinterpret_cast<std::size_t>(p) >> 3);
44 #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) 47 return static_cast<std::size_t
>(
s);
49 #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) 52 template <
typename K,
typename V>
61 typedef typename std::list<value_type>::iterator
iterator;
83 return values_.begin();
89 return values_.begin();
99 const_iterator
end()
const 101 return values_.end();
107 return values_.empty();
116 iterator it = buckets_[bucket].first;
117 if (it == values_.end())
118 return values_.end();
119 iterator end_it = buckets_[bucket].last;
128 return values_.end();
132 const_iterator
find(
const K& k)
const 137 const_iterator it = buckets_[bucket].first;
138 if (it == values_.end())
140 const_iterator end_it = buckets_[bucket].last;
149 return values_.end();
153 std::pair<iterator, bool>
insert(
const value_type& v)
155 if (size_ + 1 >= num_buckets_)
156 rehash(hash_size(size_ + 1));
158 iterator it = buckets_[bucket].first;
159 if (it == values_.end())
161 buckets_[bucket].first = buckets_[bucket].last =
162 values_insert(values_.end(), v);
164 return std::pair<iterator, bool>(buckets_[bucket].last,
true);
166 iterator end_it = buckets_[bucket].last;
170 if (it->first == v.first)
171 return std::pair<iterator, bool>(it,
false);
174 buckets_[bucket].last = values_insert(end_it, v);
176 return std::pair<iterator, bool>(buckets_[bucket].last,
true);
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();
191 ++buckets_[bucket].first;
193 --buckets_[bucket].last;
202 iterator it =
find(k);
203 if (it != values_.end())
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;
222 static std::size_t hash_size(std::size_t num_elems)
224 static std::size_t sizes[] =
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,
232 #endif // ASIO_HASH_MAP_BUCKETS 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])
238 return sizes[nth_size];
242 void rehash(std::size_t num_buckets)
244 if (num_buckets == num_buckets_)
246 num_buckets_ = num_buckets;
249 iterator end_iter = values_.end();
252 bucket_type* tmp =
new bucket_type[num_buckets_];
255 for (std::size_t i = 0; i < num_buckets_; ++i)
256 buckets_[i].first = buckets_[i].last = end_iter;
259 iterator iter = values_.begin();
260 while (iter != end_iter)
263 if (buckets_[bucket].last == end_iter)
265 buckets_[bucket].first = buckets_[bucket].last = iter++;
267 else if (++buckets_[bucket].last == iter)
273 values_.splice(buckets_[bucket].last, values_, iter++);
274 --buckets_[bucket].last;
281 iterator values_insert(iterator it,
const value_type& v)
285 return values_.insert(it, v);
290 values_.splice(it, spares_, spares_.begin());
296 void values_erase(iterator it)
299 spares_.splice(spares_.begin(), values_, it);
306 std::list<value_type> values_;
310 std::list<value_type> spares_;
320 bucket_type* buckets_;
323 std::size_t num_buckets_;
331 #endif // ASIO_DETAIL_HASH_MAP_HPP
#define ASIO_ASSERT(expr)
std::list< value_type >::const_iterator const_iterator
std::pair< iterator, bool > insert(const value_type &v)
std::size_t calculate_hash_value(int i)
const_iterator find(const K &k) const
std::pair< K, V > value_type
const_iterator begin() const
iterator find(const K &k)
const_iterator end() const
std::list< value_type >::iterator iterator