| 1 |
/** |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <[email protected]> |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
#ifndef TSL_BHOPSCOTCH_MAP_H |
| 25 |
#define TSL_BHOPSCOTCH_MAP_H |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cstddef> |
| 29 |
#include <functional> |
| 30 |
#include <initializer_list> |
| 31 |
#include <map> |
| 32 |
#include <memory> |
| 33 |
#include <type_traits> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
#include "hopscotch_hash.h" |
| 37 |
|
| 38 |
namespace tsl { |
| 39 |
|
| 40 |
/** |
| 41 |
* Similar to tsl::hopscotch_map but instead of using a list for overflowing |
| 42 |
* elements it uses a binary search tree. It thus needs an additional template |
| 43 |
* parameter Compare. Compare should be arithmetically coherent with KeyEqual. |
| 44 |
* |
| 45 |
* The binary search tree allows the map to have a worst-case scenario of O(log |
| 46 |
* n) for search and delete, even if the hash function maps all the elements to |
| 47 |
* the same bucket. For insert, the amortized worst case is O(log n), but the |
| 48 |
* worst case is O(n) in case of rehash. |
| 49 |
* |
| 50 |
* This makes the map resistant to DoS attacks (but doesn't preclude you to have |
| 51 |
* a good hash function, as an element in the bucket array is faster to retrieve |
| 52 |
* than in the tree). |
| 53 |
* |
| 54 |
*/ |
| 55 |
template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, |
| 56 |
class Compare = std::less<Key>, |
| 57 |
class Allocator = std::allocator<std::pair<const Key, T>>, |
| 58 |
unsigned int NeighborhoodSize = 62, bool StoreHash = false, |
| 59 |
class GrowthPolicy = tsl::hh::power_of_two_growth_policy<2>> |
| 60 |
class bhopscotch_map |
| 61 |
{ |
| 62 |
private: |
| 63 |
template <typename U> |
| 64 |
using has_is_transparent = tsl::detail_hopscotch_hash::has_is_transparent<U>; |
| 65 |
|
| 66 |
class KeySelect |
| 67 |
{ |
| 68 |
public: |
| 69 |
using key_type = Key; |
| 70 |
|
| 71 |
const key_type &operator()(const std::pair<const Key, T> &key_value) const |
| 72 |
{ |
| 73 |
return key_value.first; |
| 74 |
} |
| 75 |
|
| 76 |
const key_type &operator()(std::pair<const Key, T> &key_value) { return key_value.first; } |
| 77 |
}; |
| 78 |
|
| 79 |
class ValueSelect |
| 80 |
{ |
| 81 |
public: |
| 82 |
using value_type = T; |
| 83 |
|
| 84 |
const value_type &operator()(const std::pair<const Key, T> &key_value) const |
| 85 |
{ |
| 86 |
return key_value.second; |
| 87 |
} |
| 88 |
|
| 89 |
value_type &operator()(std::pair<const Key, T> &key_value) { return key_value.second; } |
| 90 |
}; |
| 91 |
|
| 92 |
// TODO Not optimal as we have to use std::pair<const Key, T> as ValueType |
| 93 |
// which forbid us to move the key in the bucket array, we have to use copy. |
| 94 |
// Optimize. |
| 95 |
using overflow_container_type = std::map<Key, T, Compare, Allocator>; |
| 96 |
using ht = |
| 97 |
detail_hopscotch_hash::hopscotch_hash<std::pair<const Key, T>, KeySelect, ValueSelect, Hash, |
| 98 |
KeyEqual, Allocator, NeighborhoodSize, StoreHash, |
| 99 |
GrowthPolicy, overflow_container_type>; |
| 100 |
|
| 101 |
public: |
| 102 |
using key_type = typename ht::key_type; |
| 103 |
using mapped_type = T; |
| 104 |
using value_type = typename ht::value_type; |
| 105 |
using size_type = typename ht::size_type; |
| 106 |
using difference_type = typename ht::difference_type; |
| 107 |
using hasher = typename ht::hasher; |
| 108 |
using key_equal = typename ht::key_equal; |
| 109 |
using key_compare = Compare; |
| 110 |
using allocator_type = typename ht::allocator_type; |
| 111 |
using reference = typename ht::reference; |
| 112 |
using const_reference = typename ht::const_reference; |
| 113 |
using pointer = typename ht::pointer; |
| 114 |
using const_pointer = typename ht::const_pointer; |
| 115 |
using iterator = typename ht::iterator; |
| 116 |
using const_iterator = typename ht::const_iterator; |
| 117 |
|
| 118 |
/* |
| 119 |
* Constructors |
| 120 |
*/ |
| 121 |
bhopscotch_map() : bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {} |
| 122 |
|
| 123 |
explicit bhopscotch_map(size_type bucket_count, const Hash &hash = Hash(), |
| 124 |
const KeyEqual &equal = KeyEqual(), |
| 125 |
const Allocator &alloc = Allocator(), const Compare &comp = Compare()) |
| 126 |
: m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR, comp) |
| 127 |
{ |
| 128 |
} |
| 129 |
|
| 130 |
bhopscotch_map(size_type bucket_count, const Allocator &alloc) |
| 131 |
: bhopscotch_map(bucket_count, Hash(), KeyEqual(), alloc) |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
bhopscotch_map(size_type bucket_count, const Hash &hash, const Allocator &alloc) |
| 136 |
: bhopscotch_map(bucket_count, hash, KeyEqual(), alloc) |
| 137 |
{ |
| 138 |
} |
| 139 |
|
| 140 |
explicit bhopscotch_map(const Allocator &alloc) |
| 141 |
: bhopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) |
| 142 |
{ |
| 143 |
} |
| 144 |
|
| 145 |
template <class InputIt> |
| 146 |
bhopscotch_map(InputIt first, InputIt last, |
| 147 |
size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, |
| 148 |
const Hash &hash = Hash(), const KeyEqual &equal = KeyEqual(), |
| 149 |
const Allocator &alloc = Allocator()) |
| 150 |
: bhopscotch_map(bucket_count, hash, equal, alloc) |
| 151 |
{ |
| 152 |
insert(first, last); |
| 153 |
} |
| 154 |
|
| 155 |
template <class InputIt> |
| 156 |
bhopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc) |
| 157 |
: bhopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc) |
| 158 |
{ |
| 159 |
} |
| 160 |
|
| 161 |
template <class InputIt> |
| 162 |
bhopscotch_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, |
| 163 |
const Allocator &alloc) |
| 164 |
: bhopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc) |
| 165 |
{ |
| 166 |
} |
| 167 |
|
| 168 |
bhopscotch_map(std::initializer_list<value_type> init, |
| 169 |
size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, |
| 170 |
const Hash &hash = Hash(), const KeyEqual &equal = KeyEqual(), |
| 171 |
const Allocator &alloc = Allocator()) |
| 172 |
: bhopscotch_map(init.begin(), init.end(), bucket_count, hash, equal, alloc) |
| 173 |
{ |
| 174 |
} |
| 175 |
|
| 176 |
bhopscotch_map(std::initializer_list<value_type> init, size_type bucket_count, |
| 177 |
const Allocator &alloc) |
| 178 |
: bhopscotch_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc) |
| 179 |
{ |
| 180 |
} |
| 181 |
|
| 182 |
bhopscotch_map(std::initializer_list<value_type> init, size_type bucket_count, const Hash &hash, |
| 183 |
const Allocator &alloc) |
| 184 |
: bhopscotch_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc) |
| 185 |
{ |
| 186 |
} |
| 187 |
|
| 188 |
bhopscotch_map &operator=(std::initializer_list<value_type> ilist) |
| 189 |
{ |
| 190 |
m_ht.clear(); |
| 191 |
|
| 192 |
m_ht.reserve(ilist.size()); |
| 193 |
m_ht.insert(ilist.begin(), ilist.end()); |
| 194 |
|
| 195 |
return *this; |
| 196 |
} |
| 197 |
|
| 198 |
allocator_type get_allocator() const { return m_ht.get_allocator(); } |
| 199 |
|
| 200 |
/* |
| 201 |
* Iterators |
| 202 |
*/ |
| 203 |
iterator begin() noexcept { return m_ht.begin(); } |
| 204 |
const_iterator begin() const noexcept { return m_ht.begin(); } |
| 205 |
const_iterator cbegin() const noexcept { return m_ht.cbegin(); } |
| 206 |
|
| 207 |
iterator end() noexcept { return m_ht.end(); } |
| 208 |
const_iterator end() const noexcept { return m_ht.end(); } |
| 209 |
const_iterator cend() const noexcept { return m_ht.cend(); } |
| 210 |
|
| 211 |
/* |
| 212 |
* Capacity |
| 213 |
*/ |
| 214 |
bool empty() const noexcept { return m_ht.empty(); } |
| 215 |
size_type size() const noexcept { return m_ht.size(); } |
| 216 |
size_type max_size() const noexcept { return m_ht.max_size(); } |
| 217 |
|
| 218 |
/* |
| 219 |
* Modifiers |
| 220 |
*/ |
| 221 |
void clear() noexcept { m_ht.clear(); } |
| 222 |
|
| 223 |
std::pair<iterator, bool> insert(const value_type &value) { return m_ht.insert(value); } |
| 224 |
|
| 225 |
template <class P, typename std::enable_if<std::is_constructible<value_type, P &&>::value>::type |
| 226 |
* = nullptr> |
| 227 |
std::pair<iterator, bool> insert(P &&value) |
| 228 |
{ |
| 229 |
return m_ht.insert(std::forward<P>(value)); |
| 230 |
} |
| 231 |
|
| 232 |
std::pair<iterator, bool> insert(value_type &&value) { return m_ht.insert(std::move(value)); } |
| 233 |
|
| 234 |
iterator insert(const_iterator hint, const value_type &value) |
| 235 |
{ |
| 236 |
return m_ht.insert(hint, value); |
| 237 |
} |
| 238 |
|
| 239 |
template <class P, typename std::enable_if<std::is_constructible<value_type, P &&>::value>::type |
| 240 |
* = nullptr> |
| 241 |
iterator insert(const_iterator hint, P &&value) |
| 242 |
{ |
| 243 |
return m_ht.insert(hint, std::forward<P>(value)); |
| 244 |
} |
| 245 |
|
| 246 |
iterator insert(const_iterator hint, value_type &&value) |
| 247 |
{ |
| 248 |
return m_ht.insert(hint, std::move(value)); |
| 249 |
} |
| 250 |
|
| 251 |
template <class InputIt> void insert(InputIt first, InputIt last) { m_ht.insert(first, last); } |
| 252 |
|
| 253 |
void insert(std::initializer_list<value_type> ilist) |
| 254 |
{ |
| 255 |
m_ht.insert(ilist.begin(), ilist.end()); |
| 256 |
} |
| 257 |
|
| 258 |
template <class M> std::pair<iterator, bool> insert_or_assign(const key_type &k, M &&obj) |
| 259 |
{ |
| 260 |
return m_ht.insert_or_assign(k, std::forward<M>(obj)); |
| 261 |
} |
| 262 |
|
| 263 |
template <class M> std::pair<iterator, bool> insert_or_assign(key_type &&k, M &&obj) |
| 264 |
{ |
| 265 |
return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj)); |
| 266 |
} |
| 267 |
|
| 268 |
template <class M> iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj) |
| 269 |
{ |
| 270 |
return m_ht.insert_or_assign(hint, k, std::forward<M>(obj)); |
| 271 |
} |
| 272 |
|
| 273 |
template <class M> iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj) |
| 274 |
{ |
| 275 |
return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj)); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Due to the way elements are stored, emplace will need to move or copy the |
| 280 |
* key-value once. The method is equivalent to |
| 281 |
* insert(value_type(std::forward<Args>(args)...)); |
| 282 |
* |
| 283 |
* Mainly here for compatibility with the std::unordered_map interface. |
| 284 |
*/ |
| 285 |
template <class... Args> std::pair<iterator, bool> emplace(Args &&...args) |
| 286 |
{ |
| 287 |
return m_ht.emplace(std::forward<Args>(args)...); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Due to the way elements are stored, emplace_hint will need to move or copy |
| 292 |
* the key-value once. The method is equivalent to insert(hint, |
| 293 |
* value_type(std::forward<Args>(args)...)); |
| 294 |
* |
| 295 |
* Mainly here for compatibility with the std::unordered_map interface. |
| 296 |
*/ |
| 297 |
template <class... Args> iterator emplace_hint(const_iterator hint, Args &&...args) |
| 298 |
{ |
| 299 |
return m_ht.emplace_hint(hint, std::forward<Args>(args)...); |
| 300 |
} |
| 301 |
|
| 302 |
template <class... Args> |
| 303 |
std::pair<iterator, bool> try_emplace(const key_type &k, Args &&...args) |
| 304 |
{ |
| 305 |
return m_ht.try_emplace(k, std::forward<Args>(args)...); |
| 306 |
} |
| 307 |
|
| 308 |
template <class... Args> std::pair<iterator, bool> try_emplace(key_type &&k, Args &&...args) |
| 309 |
{ |
| 310 |
return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...); |
| 311 |
} |
| 312 |
|
| 313 |
template <class... Args> |
| 314 |
iterator try_emplace(const_iterator hint, const key_type &k, Args &&...args) |
| 315 |
{ |
| 316 |
return m_ht.try_emplace(hint, k, std::forward<Args>(args)...); |
| 317 |
} |
| 318 |
|
| 319 |
template <class... Args> iterator try_emplace(const_iterator hint, key_type &&k, Args &&...args) |
| 320 |
{ |
| 321 |
return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...); |
| 322 |
} |
| 323 |
|
| 324 |
iterator erase(iterator pos) { return m_ht.erase(pos); } |
| 325 |
iterator erase(const_iterator pos) { return m_ht.erase(pos); } |
| 326 |
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); } |
| 327 |
size_type erase(const key_type &key) { return m_ht.erase(key); } |
| 328 |
|
| 329 |
/** |
| 330 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 331 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 332 |
* the lookup to the value if you already have the hash. |
| 333 |
*/ |
| 334 |
size_type erase(const key_type &key, std::size_t precalculated_hash) |
| 335 |
{ |
| 336 |
return m_ht.erase(key, precalculated_hash); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* This overload only participates in the overload resolution if the typedef |
| 341 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 342 |
* be hashable and comparable to Key. |
| 343 |
*/ |
| 344 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 345 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 346 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 347 |
size_type erase(const K &key) |
| 348 |
{ |
| 349 |
return m_ht.erase(key); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 354 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 355 |
* the lookup to the value if you already have the hash. |
| 356 |
*/ |
| 357 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 358 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 359 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 360 |
size_type erase(const K &key, std::size_t precalculated_hash) |
| 361 |
{ |
| 362 |
return m_ht.erase(key, precalculated_hash); |
| 363 |
} |
| 364 |
|
| 365 |
void swap(bhopscotch_map &other) { other.m_ht.swap(m_ht); } |
| 366 |
|
| 367 |
/* |
| 368 |
* Lookup |
| 369 |
*/ |
| 370 |
T &at(const Key &key) { return m_ht.at(key); } |
| 371 |
|
| 372 |
/** |
| 373 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 374 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 375 |
* the lookup if you already have the hash. |
| 376 |
*/ |
| 377 |
T &at(const Key &key, std::size_t precalculated_hash) |
| 378 |
{ |
| 379 |
return m_ht.at(key, precalculated_hash); |
| 380 |
} |
| 381 |
|
| 382 |
const T &at(const Key &key) const { return m_ht.at(key); } |
| 383 |
|
| 384 |
/** |
| 385 |
*/ |
| 386 |
const T &at(const Key &key, std::size_t precalculated_hash) const |
| 387 |
{ |
| 388 |
return m_ht.at(key, precalculated_hash); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* This overload only participates in the overload resolution if the typedef |
| 393 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 394 |
* be hashable and comparable to Key. |
| 395 |
*/ |
| 396 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 397 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 398 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 399 |
T &at(const K &key) |
| 400 |
{ |
| 401 |
return m_ht.at(key); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 406 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 407 |
* the lookup if you already have the hash. |
| 408 |
*/ |
| 409 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 410 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 411 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 412 |
T &at(const K &key, std::size_t precalculated_hash) |
| 413 |
{ |
| 414 |
return m_ht.at(key, precalculated_hash); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
*/ |
| 419 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 420 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 421 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 422 |
const T &at(const K &key) const |
| 423 |
{ |
| 424 |
return m_ht.at(key); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
*/ |
| 429 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 430 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 431 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 432 |
const T &at(const K &key, std::size_t precalculated_hash) const |
| 433 |
{ |
| 434 |
return m_ht.at(key, precalculated_hash); |
| 435 |
} |
| 436 |
|
| 437 |
T &operator[](const Key &key) { return m_ht[key]; } |
| 438 |
T &operator[](Key &&key) { return m_ht[std::move(key)]; } |
| 439 |
|
| 440 |
size_type count(const Key &key) const { return m_ht.count(key); } |
| 441 |
|
| 442 |
/** |
| 443 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 444 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 445 |
* the lookup if you already have the hash. |
| 446 |
*/ |
| 447 |
size_type count(const Key &key, std::size_t precalculated_hash) const |
| 448 |
{ |
| 449 |
return m_ht.count(key, precalculated_hash); |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* This overload only participates in the overload resolution if the typedef |
| 454 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 455 |
* be hashable and comparable to Key. |
| 456 |
*/ |
| 457 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 458 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 459 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 460 |
size_type count(const K &key) const |
| 461 |
{ |
| 462 |
return m_ht.count(key); |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 467 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 468 |
* the lookup if you already have the hash. |
| 469 |
*/ |
| 470 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 471 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 472 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 473 |
size_type count(const K &key, std::size_t precalculated_hash) const |
| 474 |
{ |
| 475 |
return m_ht.count(key, precalculated_hash); |
| 476 |
} |
| 477 |
|
| 478 |
iterator find(const Key &key) { return m_ht.find(key); } |
| 479 |
|
| 480 |
/** |
| 481 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 482 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 483 |
* the lookup if you already have the hash. |
| 484 |
*/ |
| 485 |
iterator find(const Key &key, std::size_t precalculated_hash) |
| 486 |
{ |
| 487 |
return m_ht.find(key, precalculated_hash); |
| 488 |
} |
| 489 |
|
| 490 |
const_iterator find(const Key &key) const { return m_ht.find(key); } |
| 491 |
|
| 492 |
/** |
| 493 |
*/ |
| 494 |
const_iterator find(const Key &key, std::size_t precalculated_hash) const |
| 495 |
{ |
| 496 |
return m_ht.find(key, precalculated_hash); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* This overload only participates in the overload resolution if the typedef |
| 501 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 502 |
* be hashable and comparable to Key. |
| 503 |
*/ |
| 504 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 505 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 506 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 507 |
iterator find(const K &key) |
| 508 |
{ |
| 509 |
return m_ht.find(key); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 514 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 515 |
* the lookup if you already have the hash. |
| 516 |
*/ |
| 517 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 518 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 519 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 520 |
iterator find(const K &key, std::size_t precalculated_hash) |
| 521 |
{ |
| 522 |
return m_ht.find(key, precalculated_hash); |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
*/ |
| 527 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 528 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 529 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 530 |
const_iterator find(const K &key) const |
| 531 |
{ |
| 532 |
return m_ht.find(key); |
| 533 |
} |
| 534 |
|
| 535 |
/** |
| 536 |
*/ |
| 537 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 538 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 539 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 540 |
const_iterator find(const K &key, std::size_t precalculated_hash) const |
| 541 |
{ |
| 542 |
return m_ht.find(key, precalculated_hash); |
| 543 |
} |
| 544 |
|
| 545 |
bool contains(const Key &key) const { return m_ht.contains(key); } |
| 546 |
|
| 547 |
/** |
| 548 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 549 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 550 |
* the lookup if you already have the hash. |
| 551 |
*/ |
| 552 |
bool contains(const Key &key, std::size_t precalculated_hash) const |
| 553 |
{ |
| 554 |
return m_ht.contains(key, precalculated_hash); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* This overload only participates in the overload resolution if the typedef |
| 559 |
* KeyEqual::is_transparent exists. If so, K must be hashable and comparable |
| 560 |
* to Key. |
| 561 |
*/ |
| 562 |
template <class K, class KE = KeyEqual, |
| 563 |
typename std::enable_if<has_is_transparent<KE>::value>::type * = nullptr> |
| 564 |
bool contains(const K &key) const |
| 565 |
{ |
| 566 |
return m_ht.contains(key); |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 571 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 572 |
* the lookup if you already have the hash. |
| 573 |
*/ |
| 574 |
template <class K, class KE = KeyEqual, |
| 575 |
typename std::enable_if<has_is_transparent<KE>::value>::type * = nullptr> |
| 576 |
bool contains(const K &key, std::size_t precalculated_hash) const |
| 577 |
{ |
| 578 |
return m_ht.contains(key, precalculated_hash); |
| 579 |
} |
| 580 |
|
| 581 |
std::pair<iterator, iterator> equal_range(const Key &key) { return m_ht.equal_range(key); } |
| 582 |
|
| 583 |
/** |
| 584 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 585 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 586 |
* the lookup if you already have the hash. |
| 587 |
*/ |
| 588 |
std::pair<iterator, iterator> equal_range(const Key &key, std::size_t precalculated_hash) |
| 589 |
{ |
| 590 |
return m_ht.equal_range(key, precalculated_hash); |
| 591 |
} |
| 592 |
|
| 593 |
std::pair<const_iterator, const_iterator> equal_range(const Key &key) const |
| 594 |
{ |
| 595 |
return m_ht.equal_range(key); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
*/ |
| 600 |
std::pair<const_iterator, const_iterator> equal_range(const Key &key, |
| 601 |
std::size_t precalculated_hash) const |
| 602 |
{ |
| 603 |
return m_ht.equal_range(key, precalculated_hash); |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* This overload only participates in the overload resolution if the typedef |
| 608 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 609 |
* be hashable and comparable to Key. |
| 610 |
*/ |
| 611 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 612 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 613 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 614 |
std::pair<iterator, iterator> equal_range(const K &key) |
| 615 |
{ |
| 616 |
return m_ht.equal_range(key); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 621 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 622 |
* the lookup if you already have the hash. |
| 623 |
*/ |
| 624 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 625 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 626 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 627 |
std::pair<iterator, iterator> equal_range(const K &key, std::size_t precalculated_hash) |
| 628 |
{ |
| 629 |
return m_ht.equal_range(key, precalculated_hash); |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
*/ |
| 634 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 635 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 636 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 637 |
std::pair<const_iterator, const_iterator> equal_range(const K &key) const |
| 638 |
{ |
| 639 |
return m_ht.equal_range(key); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
*/ |
| 644 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 645 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 646 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 647 |
std::pair<const_iterator, const_iterator> equal_range(const K &key, |
| 648 |
std::size_t precalculated_hash) const |
| 649 |
{ |
| 650 |
return m_ht.equal_range(key, precalculated_hash); |
| 651 |
} |
| 652 |
|
| 653 |
/* |
| 654 |
* Bucket interface |
| 655 |
*/ |
| 656 |
size_type bucket_count() const { return m_ht.bucket_count(); } |
| 657 |
size_type max_bucket_count() const { return m_ht.max_bucket_count(); } |
| 658 |
|
| 659 |
/* |
| 660 |
* Hash policy |
| 661 |
*/ |
| 662 |
float load_factor() const { return m_ht.load_factor(); } |
| 663 |
float max_load_factor() const { return m_ht.max_load_factor(); } |
| 664 |
void max_load_factor(float ml) { m_ht.max_load_factor(ml); } |
| 665 |
|
| 666 |
void rehash(size_type count_) { m_ht.rehash(count_); } |
| 667 |
void reserve(size_type count_) { m_ht.reserve(count_); } |
| 668 |
|
| 669 |
/* |
| 670 |
* Observers |
| 671 |
*/ |
| 672 |
hasher hash_function() const { return m_ht.hash_function(); } |
| 673 |
key_equal key_eq() const { return m_ht.key_eq(); } |
| 674 |
key_compare key_comp() const { return m_ht.key_comp(); } |
| 675 |
|
| 676 |
/* |
| 677 |
* Other |
| 678 |
*/ |
| 679 |
|
| 680 |
/** |
| 681 |
* Convert a const_iterator to an iterator. |
| 682 |
*/ |
| 683 |
iterator mutable_iterator(const_iterator pos) { return m_ht.mutable_iterator(pos); } |
| 684 |
|
| 685 |
size_type overflow_size() const noexcept { return m_ht.overflow_size(); } |
| 686 |
|
| 687 |
friend bool operator==(const bhopscotch_map &lhs, const bhopscotch_map &rhs) |
| 688 |
{ |
| 689 |
if (lhs.size() != rhs.size()) { |
| 690 |
return false; |
| 691 |
} |
| 692 |
|
| 693 |
for (const auto &element_lhs : lhs) { |
| 694 |
const auto it_element_rhs = rhs.find(element_lhs.first); |
| 695 |
if (it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) { |
| 696 |
return false; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
return true; |
| 701 |
} |
| 702 |
|
| 703 |
friend bool operator!=(const bhopscotch_map &lhs, const bhopscotch_map &rhs) |
| 704 |
{ |
| 705 |
return !operator==(lhs, rhs); |
| 706 |
} |
| 707 |
|
| 708 |
friend void swap(bhopscotch_map &lhs, bhopscotch_map &rhs) { lhs.swap(rhs); } |
| 709 |
|
| 710 |
private: |
| 711 |
ht m_ht; |
| 712 |
}; |
| 713 |
|
| 714 |
/** |
| 715 |
* Same as `tsl::bhopscotch_map<Key, T, Hash, KeyEqual, Compare, Allocator, |
| 716 |
* NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>`. |
| 717 |
*/ |
| 718 |
template <class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, |
| 719 |
class Compare = std::less<Key>, |
| 720 |
class Allocator = std::allocator<std::pair<const Key, T>>, |
| 721 |
unsigned int NeighborhoodSize = 62, bool StoreHash = false> |
| 722 |
using bhopscotch_pg_map = |
| 723 |
bhopscotch_map<Key, T, Hash, KeyEqual, Compare, Allocator, NeighborhoodSize, StoreHash, |
| 724 |
tsl::hh::prime_growth_policy>; |
| 725 |
|
| 726 |
} // end namespace tsl |
| 727 |
|
| 728 |
#endif |