| 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_SET_H |
| 25 |
#define TSL_BHOPSCOTCH_SET_H |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cstddef> |
| 29 |
#include <functional> |
| 30 |
#include <initializer_list> |
| 31 |
#include <memory> |
| 32 |
#include <set> |
| 33 |
#include <type_traits> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
#include "hopscotch_hash.h" |
| 37 |
|
| 38 |
namespace tsl { |
| 39 |
|
| 40 |
/** |
| 41 |
* Similar to tsl::hopscotch_set 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 set 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 set 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 |
* @copydoc hopscotch_set |
| 55 |
*/ |
| 56 |
template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, |
| 57 |
class Compare = std::less<Key>, class Allocator = std::allocator<Key>, |
| 58 |
unsigned int NeighborhoodSize = 62, bool StoreHash = false, |
| 59 |
class GrowthPolicy = tsl::hh::power_of_two_growth_policy<2>> |
| 60 |
class bhopscotch_set |
| 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 Key &key) const { return key; } |
| 72 |
|
| 73 |
key_type &operator()(Key &key) { return key; } |
| 74 |
}; |
| 75 |
|
| 76 |
using overflow_container_type = std::set<Key, Compare, Allocator>; |
| 77 |
using ht = tsl::detail_hopscotch_hash::hopscotch_hash<Key, KeySelect, void, Hash, KeyEqual, |
| 78 |
Allocator, NeighborhoodSize, StoreHash, |
| 79 |
GrowthPolicy, overflow_container_type>; |
| 80 |
|
| 81 |
public: |
| 82 |
using key_type = typename ht::key_type; |
| 83 |
using value_type = typename ht::value_type; |
| 84 |
using size_type = typename ht::size_type; |
| 85 |
using difference_type = typename ht::difference_type; |
| 86 |
using hasher = typename ht::hasher; |
| 87 |
using key_equal = typename ht::key_equal; |
| 88 |
using key_compare = Compare; |
| 89 |
using allocator_type = typename ht::allocator_type; |
| 90 |
using reference = typename ht::reference; |
| 91 |
using const_reference = typename ht::const_reference; |
| 92 |
using pointer = typename ht::pointer; |
| 93 |
using const_pointer = typename ht::const_pointer; |
| 94 |
using iterator = typename ht::iterator; |
| 95 |
using const_iterator = typename ht::const_iterator; |
| 96 |
|
| 97 |
/* |
| 98 |
* Constructors |
| 99 |
*/ |
| 100 |
bhopscotch_set() : bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {} |
| 101 |
|
| 102 |
explicit bhopscotch_set(size_type bucket_count, const Hash &hash = Hash(), |
| 103 |
const KeyEqual &equal = KeyEqual(), |
| 104 |
const Allocator &alloc = Allocator(), const Compare &comp = Compare()) |
| 105 |
: m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR, comp) |
| 106 |
{ |
| 107 |
} |
| 108 |
|
| 109 |
bhopscotch_set(size_type bucket_count, const Allocator &alloc) |
| 110 |
: bhopscotch_set(bucket_count, Hash(), KeyEqual(), alloc) |
| 111 |
{ |
| 112 |
} |
| 113 |
|
| 114 |
bhopscotch_set(size_type bucket_count, const Hash &hash, const Allocator &alloc) |
| 115 |
: bhopscotch_set(bucket_count, hash, KeyEqual(), alloc) |
| 116 |
{ |
| 117 |
} |
| 118 |
|
| 119 |
explicit bhopscotch_set(const Allocator &alloc) |
| 120 |
: bhopscotch_set(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) |
| 121 |
{ |
| 122 |
} |
| 123 |
|
| 124 |
template <class InputIt> |
| 125 |
bhopscotch_set(InputIt first, InputIt last, |
| 126 |
size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, |
| 127 |
const Hash &hash = Hash(), const KeyEqual &equal = KeyEqual(), |
| 128 |
const Allocator &alloc = Allocator()) |
| 129 |
: bhopscotch_set(bucket_count, hash, equal, alloc) |
| 130 |
{ |
| 131 |
insert(first, last); |
| 132 |
} |
| 133 |
|
| 134 |
template <class InputIt> |
| 135 |
bhopscotch_set(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc) |
| 136 |
: bhopscotch_set(first, last, bucket_count, Hash(), KeyEqual(), alloc) |
| 137 |
{ |
| 138 |
} |
| 139 |
|
| 140 |
template <class InputIt> |
| 141 |
bhopscotch_set(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, |
| 142 |
const Allocator &alloc) |
| 143 |
: bhopscotch_set(first, last, bucket_count, hash, KeyEqual(), alloc) |
| 144 |
{ |
| 145 |
} |
| 146 |
|
| 147 |
bhopscotch_set(std::initializer_list<value_type> init, |
| 148 |
size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, |
| 149 |
const Hash &hash = Hash(), const KeyEqual &equal = KeyEqual(), |
| 150 |
const Allocator &alloc = Allocator()) |
| 151 |
: bhopscotch_set(init.begin(), init.end(), bucket_count, hash, equal, alloc) |
| 152 |
{ |
| 153 |
} |
| 154 |
|
| 155 |
bhopscotch_set(std::initializer_list<value_type> init, size_type bucket_count, |
| 156 |
const Allocator &alloc) |
| 157 |
: bhopscotch_set(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc) |
| 158 |
{ |
| 159 |
} |
| 160 |
|
| 161 |
bhopscotch_set(std::initializer_list<value_type> init, size_type bucket_count, const Hash &hash, |
| 162 |
const Allocator &alloc) |
| 163 |
: bhopscotch_set(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc) |
| 164 |
{ |
| 165 |
} |
| 166 |
|
| 167 |
bhopscotch_set &operator=(std::initializer_list<value_type> ilist) |
| 168 |
{ |
| 169 |
m_ht.clear(); |
| 170 |
|
| 171 |
m_ht.reserve(ilist.size()); |
| 172 |
m_ht.insert(ilist.begin(), ilist.end()); |
| 173 |
|
| 174 |
return *this; |
| 175 |
} |
| 176 |
|
| 177 |
allocator_type get_allocator() const { return m_ht.get_allocator(); } |
| 178 |
|
| 179 |
/* |
| 180 |
* Iterators |
| 181 |
*/ |
| 182 |
iterator begin() noexcept { return m_ht.begin(); } |
| 183 |
const_iterator begin() const noexcept { return m_ht.begin(); } |
| 184 |
const_iterator cbegin() const noexcept { return m_ht.cbegin(); } |
| 185 |
|
| 186 |
iterator end() noexcept { return m_ht.end(); } |
| 187 |
const_iterator end() const noexcept { return m_ht.end(); } |
| 188 |
const_iterator cend() const noexcept { return m_ht.cend(); } |
| 189 |
|
| 190 |
/* |
| 191 |
* Capacity |
| 192 |
*/ |
| 193 |
bool empty() const noexcept { return m_ht.empty(); } |
| 194 |
size_type size() const noexcept { return m_ht.size(); } |
| 195 |
size_type max_size() const noexcept { return m_ht.max_size(); } |
| 196 |
|
| 197 |
/* |
| 198 |
* Modifiers |
| 199 |
*/ |
| 200 |
void clear() noexcept { m_ht.clear(); } |
| 201 |
|
| 202 |
std::pair<iterator, bool> insert(const value_type &value) { return m_ht.insert(value); } |
| 203 |
std::pair<iterator, bool> insert(value_type &&value) { return m_ht.insert(std::move(value)); } |
| 204 |
|
| 205 |
iterator insert(const_iterator hint, const value_type &value) |
| 206 |
{ |
| 207 |
return m_ht.insert(hint, value); |
| 208 |
} |
| 209 |
iterator insert(const_iterator hint, value_type &&value) |
| 210 |
{ |
| 211 |
return m_ht.insert(hint, std::move(value)); |
| 212 |
} |
| 213 |
|
| 214 |
template <class InputIt> void insert(InputIt first, InputIt last) { m_ht.insert(first, last); } |
| 215 |
void insert(std::initializer_list<value_type> ilist) |
| 216 |
{ |
| 217 |
m_ht.insert(ilist.begin(), ilist.end()); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Due to the way elements are stored, emplace will need to move or copy the |
| 222 |
* key-value once. The method is equivalent to |
| 223 |
* insert(value_type(std::forward<Args>(args)...)); |
| 224 |
* |
| 225 |
* Mainly here for compatibility with the std::unordered_map interface. |
| 226 |
*/ |
| 227 |
template <class... Args> std::pair<iterator, bool> emplace(Args &&...args) |
| 228 |
{ |
| 229 |
return m_ht.emplace(std::forward<Args>(args)...); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Due to the way elements are stored, emplace_hint will need to move or copy |
| 234 |
* the key-value once. The method is equivalent to insert(hint, |
| 235 |
* value_type(std::forward<Args>(args)...)); |
| 236 |
* |
| 237 |
* Mainly here for compatibility with the std::unordered_map interface. |
| 238 |
*/ |
| 239 |
template <class... Args> iterator emplace_hint(const_iterator hint, Args &&...args) |
| 240 |
{ |
| 241 |
return m_ht.emplace_hint(hint, std::forward<Args>(args)...); |
| 242 |
} |
| 243 |
|
| 244 |
iterator erase(iterator pos) { return m_ht.erase(pos); } |
| 245 |
iterator erase(const_iterator pos) { return m_ht.erase(pos); } |
| 246 |
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); } |
| 247 |
size_type erase(const key_type &key) { return m_ht.erase(key); } |
| 248 |
|
| 249 |
/** |
| 250 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 251 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 252 |
* the lookup to the value if you already have the hash. |
| 253 |
*/ |
| 254 |
size_type erase(const key_type &key, std::size_t precalculated_hash) |
| 255 |
{ |
| 256 |
return m_ht.erase(key, precalculated_hash); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* This overload only participates in the overload resolution if the typedef |
| 261 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 262 |
* be hashable and comparable to Key. |
| 263 |
*/ |
| 264 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 265 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 266 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 267 |
size_type erase(const K &key) |
| 268 |
{ |
| 269 |
return m_ht.erase(key); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* @copydoc erase(const K& key) |
| 274 |
* |
| 275 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 276 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 277 |
* the lookup to the value if you already have the hash. |
| 278 |
*/ |
| 279 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 280 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 281 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 282 |
size_type erase(const K &key, std::size_t precalculated_hash) |
| 283 |
{ |
| 284 |
return m_ht.erase(key, precalculated_hash); |
| 285 |
} |
| 286 |
|
| 287 |
void swap(bhopscotch_set &other) { other.m_ht.swap(m_ht); } |
| 288 |
|
| 289 |
/* |
| 290 |
* Lookup |
| 291 |
*/ |
| 292 |
size_type count(const Key &key) const { return m_ht.count(key); } |
| 293 |
|
| 294 |
/** |
| 295 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 296 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 297 |
* the lookup if you already have the hash. |
| 298 |
*/ |
| 299 |
size_type count(const Key &key, std::size_t precalculated_hash) const |
| 300 |
{ |
| 301 |
return m_ht.count(key, precalculated_hash); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* This overload only participates in the overload resolution if the typedef |
| 306 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 307 |
* be hashable and comparable to Key. |
| 308 |
*/ |
| 309 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 310 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 311 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 312 |
size_type count(const K &key) const |
| 313 |
{ |
| 314 |
return m_ht.count(key); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* @copydoc count(const K& key) const |
| 319 |
* |
| 320 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 321 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 322 |
* the lookup if you already have the hash. |
| 323 |
*/ |
| 324 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 325 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 326 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 327 |
size_type count(const K &key, std::size_t precalculated_hash) const |
| 328 |
{ |
| 329 |
return m_ht.count(key, precalculated_hash); |
| 330 |
} |
| 331 |
|
| 332 |
iterator find(const Key &key) { return m_ht.find(key); } |
| 333 |
|
| 334 |
/** |
| 335 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 336 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 337 |
* the lookup if you already have the hash. |
| 338 |
*/ |
| 339 |
iterator find(const Key &key, std::size_t precalculated_hash) |
| 340 |
{ |
| 341 |
return m_ht.find(key, precalculated_hash); |
| 342 |
} |
| 343 |
|
| 344 |
const_iterator find(const Key &key) const { return m_ht.find(key); } |
| 345 |
|
| 346 |
/** |
| 347 |
* @copydoc find(const Key& key, std::size_t precalculated_hash) |
| 348 |
*/ |
| 349 |
const_iterator find(const Key &key, std::size_t precalculated_hash) const |
| 350 |
{ |
| 351 |
return m_ht.find(key, precalculated_hash); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* This overload only participates in the overload resolution if the typedef |
| 356 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 357 |
* be hashable and comparable to Key. |
| 358 |
*/ |
| 359 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 360 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 361 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 362 |
iterator find(const K &key) |
| 363 |
{ |
| 364 |
return m_ht.find(key); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* @copydoc find(const K& key) |
| 369 |
* |
| 370 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 371 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 372 |
* the lookup if you already have the hash. |
| 373 |
*/ |
| 374 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 375 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 376 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 377 |
iterator find(const K &key, std::size_t precalculated_hash) |
| 378 |
{ |
| 379 |
return m_ht.find(key, precalculated_hash); |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* @copydoc find(const K& key) |
| 384 |
*/ |
| 385 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 386 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 387 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 388 |
const_iterator find(const K &key) const |
| 389 |
{ |
| 390 |
return m_ht.find(key); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* @copydoc find(const K& key) |
| 395 |
* |
| 396 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 397 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 398 |
* the lookup if you already have the hash. |
| 399 |
*/ |
| 400 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 401 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 402 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 403 |
const_iterator find(const K &key, std::size_t precalculated_hash) const |
| 404 |
{ |
| 405 |
return m_ht.find(key, precalculated_hash); |
| 406 |
} |
| 407 |
|
| 408 |
bool contains(const Key &key) const { return m_ht.contains(key); } |
| 409 |
|
| 410 |
/** |
| 411 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 412 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 413 |
* the lookup if you already have the hash. |
| 414 |
*/ |
| 415 |
bool contains(const Key &key, std::size_t precalculated_hash) const |
| 416 |
{ |
| 417 |
return m_ht.contains(key, precalculated_hash); |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* This overload only participates in the overload resolution if the typedef |
| 422 |
* KeyEqual::is_transparent exists. If so, K must be hashable and comparable |
| 423 |
* to Key. |
| 424 |
*/ |
| 425 |
template <class K, class KE = KeyEqual, |
| 426 |
typename std::enable_if<has_is_transparent<KE>::value>::type * = nullptr> |
| 427 |
bool contains(const K &key) const |
| 428 |
{ |
| 429 |
return m_ht.contains(key); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* @copydoc contains(const K& key) const |
| 434 |
* |
| 435 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 436 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 437 |
* the lookup if you already have the hash. |
| 438 |
*/ |
| 439 |
template <class K, class KE = KeyEqual, |
| 440 |
typename std::enable_if<has_is_transparent<KE>::value>::type * = nullptr> |
| 441 |
bool contains(const K &key, std::size_t precalculated_hash) const |
| 442 |
{ |
| 443 |
return m_ht.contains(key, precalculated_hash); |
| 444 |
} |
| 445 |
|
| 446 |
std::pair<iterator, iterator> equal_range(const Key &key) { return m_ht.equal_range(key); } |
| 447 |
|
| 448 |
/** |
| 449 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 450 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 451 |
* the lookup if you already have the hash. |
| 452 |
*/ |
| 453 |
std::pair<iterator, iterator> equal_range(const Key &key, std::size_t precalculated_hash) |
| 454 |
{ |
| 455 |
return m_ht.equal_range(key, precalculated_hash); |
| 456 |
} |
| 457 |
|
| 458 |
std::pair<const_iterator, const_iterator> equal_range(const Key &key) const |
| 459 |
{ |
| 460 |
return m_ht.equal_range(key); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* @copydoc equal_range(const Key& key, std::size_t precalculated_hash) |
| 465 |
*/ |
| 466 |
std::pair<const_iterator, const_iterator> equal_range(const Key &key, |
| 467 |
std::size_t precalculated_hash) const |
| 468 |
{ |
| 469 |
return m_ht.equal_range(key, precalculated_hash); |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* This overload only participates in the overload resolution if the typedef |
| 474 |
* KeyEqual::is_transparent and Compare::is_transparent exist. If so, K must |
| 475 |
* be hashable and comparable to Key. |
| 476 |
*/ |
| 477 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 478 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 479 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 480 |
std::pair<iterator, iterator> equal_range(const K &key) |
| 481 |
{ |
| 482 |
return m_ht.equal_range(key); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* @copydoc equal_range(const K& key) |
| 487 |
* |
| 488 |
* Use the hash value 'precalculated_hash' instead of hashing the key. The |
| 489 |
* hash value should be the same as hash_function()(key). Useful to speed-up |
| 490 |
* the lookup if you already have the hash. |
| 491 |
*/ |
| 492 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 493 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 494 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 495 |
std::pair<iterator, iterator> equal_range(const K &key, std::size_t precalculated_hash) |
| 496 |
{ |
| 497 |
return m_ht.equal_range(key, precalculated_hash); |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* @copydoc equal_range(const K& key) |
| 502 |
*/ |
| 503 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 504 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 505 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 506 |
std::pair<const_iterator, const_iterator> equal_range(const K &key) const |
| 507 |
{ |
| 508 |
return m_ht.equal_range(key); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* @copydoc equal_range(const K& key, std::size_t precalculated_hash) |
| 513 |
*/ |
| 514 |
template <class K, class KE = KeyEqual, class CP = Compare, |
| 515 |
typename std::enable_if<has_is_transparent<KE>::value && |
| 516 |
has_is_transparent<CP>::value>::type * = nullptr> |
| 517 |
std::pair<const_iterator, const_iterator> equal_range(const K &key, |
| 518 |
std::size_t precalculated_hash) const |
| 519 |
{ |
| 520 |
return m_ht.equal_range(key, precalculated_hash); |
| 521 |
} |
| 522 |
|
| 523 |
/* |
| 524 |
* Bucket interface |
| 525 |
*/ |
| 526 |
size_type bucket_count() const { return m_ht.bucket_count(); } |
| 527 |
size_type max_bucket_count() const { return m_ht.max_bucket_count(); } |
| 528 |
|
| 529 |
/* |
| 530 |
* Hash policy |
| 531 |
*/ |
| 532 |
float load_factor() const { return m_ht.load_factor(); } |
| 533 |
float max_load_factor() const { return m_ht.max_load_factor(); } |
| 534 |
void max_load_factor(float ml) { m_ht.max_load_factor(ml); } |
| 535 |
|
| 536 |
void rehash(size_type count_) { m_ht.rehash(count_); } |
| 537 |
void reserve(size_type count_) { m_ht.reserve(count_); } |
| 538 |
|
| 539 |
/* |
| 540 |
* Observers |
| 541 |
*/ |
| 542 |
hasher hash_function() const { return m_ht.hash_function(); } |
| 543 |
key_equal key_eq() const { return m_ht.key_eq(); } |
| 544 |
key_compare key_comp() const { return m_ht.key_comp(); } |
| 545 |
|
| 546 |
/* |
| 547 |
* Other |
| 548 |
*/ |
| 549 |
|
| 550 |
/** |
| 551 |
* Convert a const_iterator to an iterator. |
| 552 |
*/ |
| 553 |
iterator mutable_iterator(const_iterator pos) { return m_ht.mutable_iterator(pos); } |
| 554 |
|
| 555 |
size_type overflow_size() const noexcept { return m_ht.overflow_size(); } |
| 556 |
|
| 557 |
friend bool operator==(const bhopscotch_set &lhs, const bhopscotch_set &rhs) |
| 558 |
{ |
| 559 |
if (lhs.size() != rhs.size()) { |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
for (const auto &element_lhs : lhs) { |
| 564 |
const auto it_element_rhs = rhs.find(element_lhs); |
| 565 |
if (it_element_rhs == rhs.cend()) { |
| 566 |
return false; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
return true; |
| 571 |
} |
| 572 |
|
| 573 |
friend bool operator!=(const bhopscotch_set &lhs, const bhopscotch_set &rhs) |
| 574 |
{ |
| 575 |
return !operator==(lhs, rhs); |
| 576 |
} |
| 577 |
|
| 578 |
friend void swap(bhopscotch_set &lhs, bhopscotch_set &rhs) { lhs.swap(rhs); } |
| 579 |
|
| 580 |
private: |
| 581 |
ht m_ht; |
| 582 |
}; |
| 583 |
|
| 584 |
/** |
| 585 |
* Same as `tsl::bhopscotch_set<Key, Hash, KeyEqual, Compare, Allocator, |
| 586 |
* NeighborhoodSize, StoreHash, tsl::hh::prime_growth_policy>`. |
| 587 |
*/ |
| 588 |
template <class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, |
| 589 |
class Compare = std::less<Key>, class Allocator = std::allocator<Key>, |
| 590 |
unsigned int NeighborhoodSize = 62, bool StoreHash = false> |
| 591 |
using bhopscotch_pg_set = |
| 592 |
bhopscotch_set<Key, Hash, KeyEqual, Compare, Allocator, NeighborhoodSize, StoreHash, |
| 593 |
tsl::hh::prime_growth_policy>; |
| 594 |
|
| 595 |
} // end namespace tsl |
| 596 |
|
| 597 |
#endif |