1 /** 2 * MIT License 3 * 4 * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com> 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 all 14 * 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_ROBIN_MAP_H 25 #define TSL_ROBIN_MAP_H 26 27 28 #include <cstddef> 29 #include <functional> 30 #include <initializer_list> 31 #include <memory> 32 #include <type_traits> 33 #include <utility> 34 #include "robin_hash.h" 35 36 37 namespace tsl { 38 39 40 /** 41 * Implementation of a hash map using open-addressing and the robin hood hashing algorithm with backward shift deletion. 42 * 43 * For operations modifying the hash map (insert, erase, rehash, ...), the strong exception guarantee 44 * is only guaranteed when the expression `std::is_nothrow_swappable<std::pair<Key, T>>::value && 45 * std::is_nothrow_move_constructible<std::pair<Key, T>>::value` is true, otherwise if an exception 46 * is thrown during the swap or the move, the hash map may end up in a undefined state. Per the standard 47 * a `Key` or `T` with a noexcept copy constructor and no move constructor also satisfies the 48 * `std::is_nothrow_move_constructible<std::pair<Key, T>>::value` criterion (and will thus guarantee the 49 * strong exception for the map). 50 * 51 * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve 52 * the performance during lookups if the `KeyEqual` function takes time (if it engenders a cache-miss for example) 53 * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used 54 * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash. 55 * When it is detected that storing the hash will not incur any memory penalty due to alignment (i.e. 56 * `sizeof(tsl::detail_robin_hash::bucket_entry<ValueType, true>) == 57 * sizeof(tsl::detail_robin_hash::bucket_entry<ValueType, false>)`) and `tsl::rh::power_of_two_growth_policy` is 58 * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will 59 * not be used on lookups unless `StoreHash` is true). 60 * 61 * `GrowthPolicy` defines how the map grows and consequently how a hash value is mapped to a bucket. 62 * By default the map uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets 63 * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo. 64 * Other growth policies are available and you may define your own growth policy, 65 * check `tsl::rh::power_of_two_growth_policy` for the interface. 66 * 67 * `std::pair<Key, T>` must be swappable. 68 * 69 * `Key` and `T` must be copy and/or move constructible. 70 * 71 * If the destructor of `Key` or `T` throws an exception, the behaviour of the class is undefined. 72 * 73 * Iterators invalidation: 74 * - clear, operator=, reserve, rehash: always invalidate the iterators. 75 * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators. 76 * - erase: always invalidate the iterators. 77 */ 78 template<class Key, 79 class T, 80 class Hash = std::hash<Key>, 81 class KeyEqual = std::equal_to<Key>, 82 class Allocator = std::allocator<std::pair<Key, T>>, 83 bool StoreHash = false, 84 class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>> 85 class robin_map { 86 private: 87 template<typename U> 88 using has_is_transparent = tsl::detail_robin_hash::has_is_transparent<U>; 89 90 class KeySelect { 91 public: 92 using key_type = Key; 93 operator()94 const key_type& operator()(const std::pair<Key, T>& key_value) const noexcept { 95 return key_value.first; 96 } 97 operator()98 key_type& operator()(std::pair<Key, T>& key_value) noexcept { 99 return key_value.first; 100 } 101 }; 102 103 class ValueSelect { 104 public: 105 using value_type = T; 106 operator()107 const value_type& operator()(const std::pair<Key, T>& key_value) const noexcept { 108 return key_value.second; 109 } 110 operator()111 value_type& operator()(std::pair<Key, T>& key_value) noexcept { 112 return key_value.second; 113 } 114 }; 115 116 using ht = detail_robin_hash::robin_hash<std::pair<Key, T>, KeySelect, ValueSelect, 117 Hash, KeyEqual, Allocator, StoreHash, GrowthPolicy>; 118 119 public: 120 using key_type = typename ht::key_type; 121 using mapped_type = T; 122 using value_type = typename ht::value_type; 123 using size_type = typename ht::size_type; 124 using difference_type = typename ht::difference_type; 125 using hasher = typename ht::hasher; 126 using key_equal = typename ht::key_equal; 127 using allocator_type = typename ht::allocator_type; 128 using reference = typename ht::reference; 129 using const_reference = typename ht::const_reference; 130 using pointer = typename ht::pointer; 131 using const_pointer = typename ht::const_pointer; 132 using iterator = typename ht::iterator; 133 using const_iterator = typename ht::const_iterator; 134 135 136 public: 137 /* 138 * Constructors 139 */ robin_map()140 robin_map(): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE) { 141 } 142 143 explicit robin_map(size_type bucket_count, 144 const Hash& hash = Hash(), 145 const KeyEqual& equal = KeyEqual(), 146 const Allocator& alloc = Allocator()): m_ht(bucket_count,hash,equal,alloc)147 m_ht(bucket_count, hash, equal, alloc) 148 { 149 } 150 robin_map(size_type bucket_count,const Allocator & alloc)151 robin_map(size_type bucket_count, 152 const Allocator& alloc): robin_map(bucket_count, Hash(), KeyEqual(), alloc) 153 { 154 } 155 robin_map(size_type bucket_count,const Hash & hash,const Allocator & alloc)156 robin_map(size_type bucket_count, 157 const Hash& hash, 158 const Allocator& alloc): robin_map(bucket_count, hash, KeyEqual(), alloc) 159 { 160 } 161 robin_map(const Allocator & alloc)162 explicit robin_map(const Allocator& alloc): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) { 163 } 164 165 template<class InputIt> 166 robin_map(InputIt first, InputIt last, 167 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, 168 const Hash& hash = Hash(), 169 const KeyEqual& equal = KeyEqual(), robin_map(bucket_count,hash,equal,alloc)170 const Allocator& alloc = Allocator()): robin_map(bucket_count, hash, equal, alloc) 171 { 172 insert(first, last); 173 } 174 175 template<class InputIt> robin_map(InputIt first,InputIt last,size_type bucket_count,const Allocator & alloc)176 robin_map(InputIt first, InputIt last, 177 size_type bucket_count, 178 const Allocator& alloc): robin_map(first, last, bucket_count, Hash(), KeyEqual(), alloc) 179 { 180 } 181 182 template<class InputIt> robin_map(InputIt first,InputIt last,size_type bucket_count,const Hash & hash,const Allocator & alloc)183 robin_map(InputIt first, InputIt last, 184 size_type bucket_count, 185 const Hash& hash, 186 const Allocator& alloc): robin_map(first, last, bucket_count, hash, KeyEqual(), alloc) 187 { 188 } 189 190 robin_map(std::initializer_list<value_type> init, 191 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE, 192 const Hash& hash = Hash(), 193 const KeyEqual& equal = KeyEqual(), 194 const Allocator& alloc = Allocator()): 195 robin_map(init.begin(), init.end(), bucket_count, hash, equal, alloc) 196 { 197 } 198 robin_map(std::initializer_list<value_type> init,size_type bucket_count,const Allocator & alloc)199 robin_map(std::initializer_list<value_type> init, 200 size_type bucket_count, 201 const Allocator& alloc): 202 robin_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc) 203 { 204 } 205 robin_map(std::initializer_list<value_type> init,size_type bucket_count,const Hash & hash,const Allocator & alloc)206 robin_map(std::initializer_list<value_type> init, 207 size_type bucket_count, 208 const Hash& hash, 209 const Allocator& alloc): 210 robin_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc) 211 { 212 } 213 214 robin_map& operator=(std::initializer_list<value_type> ilist) { 215 m_ht.clear(); 216 217 m_ht.reserve(ilist.size()); 218 m_ht.insert(ilist.begin(), ilist.end()); 219 220 return *this; 221 } 222 get_allocator()223 allocator_type get_allocator() const { return m_ht.get_allocator(); } 224 225 226 /* 227 * Iterators 228 */ begin()229 iterator begin() noexcept { return m_ht.begin(); } begin()230 const_iterator begin() const noexcept { return m_ht.begin(); } cbegin()231 const_iterator cbegin() const noexcept { return m_ht.cbegin(); } 232 end()233 iterator end() noexcept { return m_ht.end(); } end()234 const_iterator end() const noexcept { return m_ht.end(); } cend()235 const_iterator cend() const noexcept { return m_ht.cend(); } 236 237 238 /* 239 * Capacity 240 */ empty()241 bool empty() const noexcept { return m_ht.empty(); } size()242 size_type size() const noexcept { return m_ht.size(); } max_size()243 size_type max_size() const noexcept { return m_ht.max_size(); } 244 245 /* 246 * Modifiers 247 */ clear()248 void clear() noexcept { m_ht.clear(); } 249 250 251 insert(const value_type & value)252 std::pair<iterator, bool> insert(const value_type& value) { 253 return m_ht.insert(value); 254 } 255 256 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr> insert(P && value)257 std::pair<iterator, bool> insert(P&& value) { 258 return m_ht.emplace(std::forward<P>(value)); 259 } 260 insert(value_type && value)261 std::pair<iterator, bool> insert(value_type&& value) { 262 return m_ht.insert(std::move(value)); 263 } 264 265 insert(const_iterator hint,const value_type & value)266 iterator insert(const_iterator hint, const value_type& value) { 267 return m_ht.insert_hint(hint, value); 268 } 269 270 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr> insert(const_iterator hint,P && value)271 iterator insert(const_iterator hint, P&& value) { 272 return m_ht.emplace_hint(hint, std::forward<P>(value)); 273 } 274 insert(const_iterator hint,value_type && value)275 iterator insert(const_iterator hint, value_type&& value) { 276 return m_ht.insert_hint(hint, std::move(value)); 277 } 278 279 280 template<class InputIt> insert(InputIt first,InputIt last)281 void insert(InputIt first, InputIt last) { 282 m_ht.insert(first, last); 283 } 284 insert(std::initializer_list<value_type> ilist)285 void insert(std::initializer_list<value_type> ilist) { 286 m_ht.insert(ilist.begin(), ilist.end()); 287 } 288 289 290 291 292 template<class M> insert_or_assign(const key_type & k,M && obj)293 std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) { 294 return m_ht.insert_or_assign(k, std::forward<M>(obj)); 295 } 296 297 template<class M> insert_or_assign(key_type && k,M && obj)298 std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) { 299 return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj)); 300 } 301 302 template<class M> insert_or_assign(const_iterator hint,const key_type & k,M && obj)303 iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) { 304 return m_ht.insert_or_assign(hint, k, std::forward<M>(obj)); 305 } 306 307 template<class M> insert_or_assign(const_iterator hint,key_type && k,M && obj)308 iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) { 309 return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj)); 310 } 311 312 313 314 /** 315 * Due to the way elements are stored, emplace will need to move or copy the key-value once. 316 * The method is equivalent to insert(value_type(std::forward<Args>(args)...)); 317 * 318 * Mainly here for compatibility with the std::unordered_map interface. 319 */ 320 template<class... Args> emplace(Args &&...args)321 std::pair<iterator, bool> emplace(Args&&... args) { 322 return m_ht.emplace(std::forward<Args>(args)...); 323 } 324 325 326 327 /** 328 * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once. 329 * The method is equivalent to insert(hint, value_type(std::forward<Args>(args)...)); 330 * 331 * Mainly here for compatibility with the std::unordered_map interface. 332 */ 333 template<class... Args> emplace_hint(const_iterator hint,Args &&...args)334 iterator emplace_hint(const_iterator hint, Args&&... args) { 335 return m_ht.emplace_hint(hint, std::forward<Args>(args)...); 336 } 337 338 339 340 341 template<class... Args> try_emplace(const key_type & k,Args &&...args)342 std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) { 343 return m_ht.try_emplace(k, std::forward<Args>(args)...); 344 } 345 346 template<class... Args> try_emplace(key_type && k,Args &&...args)347 std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) { 348 return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...); 349 } 350 351 template<class... Args> try_emplace(const_iterator hint,const key_type & k,Args &&...args)352 iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) { 353 return m_ht.try_emplace_hint(hint, k, std::forward<Args>(args)...); 354 } 355 356 template<class... Args> try_emplace(const_iterator hint,key_type && k,Args &&...args)357 iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) { 358 return m_ht.try_emplace_hint(hint, std::move(k), std::forward<Args>(args)...); 359 } 360 361 362 363 erase(iterator pos)364 iterator erase(iterator pos) { return m_ht.erase(pos); } erase(const_iterator pos)365 iterator erase(const_iterator pos) { return m_ht.erase(pos); } erase(const_iterator first,const_iterator last)366 iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); } erase(const key_type & key)367 size_type erase(const key_type& key) { return m_ht.erase(key); } 368 369 /** 370 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 371 * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash. 372 */ erase(const key_type & key,std::size_t precalculated_hash)373 size_type erase(const key_type& key, std::size_t precalculated_hash) { 374 return m_ht.erase(key, precalculated_hash); 375 } 376 377 /** 378 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 379 * If so, K must be hashable and comparable to Key. 380 */ 381 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> erase(const K & key)382 size_type erase(const K& key) { return m_ht.erase(key); } 383 384 /** 385 * @copydoc erase(const K& key) 386 * 387 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 388 * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash. 389 */ 390 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> erase(const K & key,std::size_t precalculated_hash)391 size_type erase(const K& key, std::size_t precalculated_hash) { 392 return m_ht.erase(key, precalculated_hash); 393 } 394 395 396 swap(robin_map & other)397 void swap(robin_map& other) { other.m_ht.swap(m_ht); } 398 399 400 401 /* 402 * Lookup 403 */ at(const Key & key)404 T& at(const Key& key) { return m_ht.at(key); } 405 406 /** 407 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 408 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 409 */ at(const Key & key,std::size_t precalculated_hash)410 T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); } 411 412 at(const Key & key)413 const T& at(const Key& key) const { return m_ht.at(key); } 414 415 /** 416 * @copydoc at(const Key& key, std::size_t precalculated_hash) 417 */ at(const Key & key,std::size_t precalculated_hash)418 const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); } 419 420 421 /** 422 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 423 * If so, K must be hashable and comparable to Key. 424 */ 425 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> at(const K & key)426 T& at(const K& key) { return m_ht.at(key); } 427 428 /** 429 * @copydoc at(const K& key) 430 * 431 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 432 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 433 */ 434 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> at(const K & key,std::size_t precalculated_hash)435 T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); } 436 437 438 /** 439 * @copydoc at(const K& key) 440 */ 441 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> at(const K & key)442 const T& at(const K& key) const { return m_ht.at(key); } 443 444 /** 445 * @copydoc at(const K& key, std::size_t precalculated_hash) 446 */ 447 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> at(const K & key,std::size_t precalculated_hash)448 const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); } 449 450 451 452 453 T& operator[](const Key& key) { return m_ht[key]; } 454 T& operator[](Key&& key) { return m_ht[std::move(key)]; } 455 456 457 458 count(const Key & key)459 size_type count(const Key& key) const { return m_ht.count(key); } 460 461 /** 462 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 463 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 464 */ count(const Key & key,std::size_t precalculated_hash)465 size_type count(const Key& key, std::size_t precalculated_hash) const { 466 return m_ht.count(key, precalculated_hash); 467 } 468 469 /** 470 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 471 * If so, K must be hashable and comparable to Key. 472 */ 473 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> count(const K & key)474 size_type count(const K& key) const { return m_ht.count(key); } 475 476 /** 477 * @copydoc count(const K& key) const 478 * 479 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 480 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 481 */ 482 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> count(const K & key,std::size_t precalculated_hash)483 size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); } 484 485 486 487 find(const Key & key)488 iterator find(const Key& key) { return m_ht.find(key); } 489 490 /** 491 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 492 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 493 */ find(const Key & key,std::size_t precalculated_hash)494 iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); } 495 find(const Key & key)496 const_iterator find(const Key& key) const { return m_ht.find(key); } 497 498 /** 499 * @copydoc find(const Key& key, std::size_t precalculated_hash) 500 */ find(const Key & key,std::size_t precalculated_hash)501 const_iterator find(const Key& key, std::size_t precalculated_hash) const { 502 return m_ht.find(key, precalculated_hash); 503 } 504 505 /** 506 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 507 * If so, K must be hashable and comparable to Key. 508 */ 509 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> find(const K & key)510 iterator find(const K& key) { return m_ht.find(key); } 511 512 /** 513 * @copydoc find(const K& key) 514 * 515 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 516 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 517 */ 518 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> find(const K & key,std::size_t precalculated_hash)519 iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); } 520 521 /** 522 * @copydoc find(const K& key) 523 */ 524 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> find(const K & key)525 const_iterator find(const K& key) const { return m_ht.find(key); } 526 527 /** 528 * @copydoc find(const K& key) 529 * 530 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 531 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 532 */ 533 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> find(const K & key,std::size_t precalculated_hash)534 const_iterator find(const K& key, std::size_t precalculated_hash) const { 535 return m_ht.find(key, precalculated_hash); 536 } 537 538 539 540 contains(const Key & key)541 bool contains(const Key& key) const { return m_ht.contains(key); } 542 543 /** 544 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 545 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 546 */ contains(const Key & key,std::size_t precalculated_hash)547 bool contains(const Key& key, std::size_t precalculated_hash) const { 548 return m_ht.contains(key, precalculated_hash); 549 } 550 551 /** 552 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 553 * If so, K must be hashable and comparable to Key. 554 */ 555 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> contains(const K & key)556 bool contains(const K& key) const { return m_ht.contains(key); } 557 558 /** 559 * @copydoc contains(const K& key) const 560 * 561 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 562 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 563 */ 564 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> contains(const K & key,std::size_t precalculated_hash)565 bool contains(const K& key, std::size_t precalculated_hash) const { 566 return m_ht.contains(key, precalculated_hash); 567 } 568 569 570 571 equal_range(const Key & key)572 std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); } 573 574 /** 575 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 576 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 577 */ equal_range(const Key & key,std::size_t precalculated_hash)578 std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) { 579 return m_ht.equal_range(key, precalculated_hash); 580 } 581 equal_range(const Key & key)582 std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); } 583 584 /** 585 * @copydoc equal_range(const Key& key, std::size_t precalculated_hash) 586 */ equal_range(const Key & key,std::size_t precalculated_hash)587 std::pair<const_iterator, const_iterator> equal_range(const Key& key, std::size_t precalculated_hash) const { 588 return m_ht.equal_range(key, precalculated_hash); 589 } 590 591 /** 592 * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. 593 * If so, K must be hashable and comparable to Key. 594 */ 595 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> equal_range(const K & key)596 std::pair<iterator, iterator> equal_range(const K& key) { return m_ht.equal_range(key); } 597 598 599 /** 600 * @copydoc equal_range(const K& key) 601 * 602 * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same 603 * as hash_function()(key). Useful to speed-up the lookup if you already have the hash. 604 */ 605 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> equal_range(const K & key,std::size_t precalculated_hash)606 std::pair<iterator, iterator> equal_range(const K& key, std::size_t precalculated_hash) { 607 return m_ht.equal_range(key, precalculated_hash); 608 } 609 610 /** 611 * @copydoc equal_range(const K& key) 612 */ 613 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> equal_range(const K & key)614 std::pair<const_iterator, const_iterator> equal_range(const K& key) const { return m_ht.equal_range(key); } 615 616 /** 617 * @copydoc equal_range(const K& key, std::size_t precalculated_hash) 618 */ 619 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr> equal_range(const K & key,std::size_t precalculated_hash)620 std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t precalculated_hash) const { 621 return m_ht.equal_range(key, precalculated_hash); 622 } 623 624 625 626 627 /* 628 * Bucket interface 629 */ bucket_count()630 size_type bucket_count() const { return m_ht.bucket_count(); } max_bucket_count()631 size_type max_bucket_count() const { return m_ht.max_bucket_count(); } 632 633 634 /* 635 * Hash policy 636 */ load_factor()637 float load_factor() const { return m_ht.load_factor(); } 638 min_load_factor()639 float min_load_factor() const { return m_ht.min_load_factor(); } max_load_factor()640 float max_load_factor() const { return m_ht.max_load_factor(); } 641 642 /** 643 * Set the `min_load_factor` to `ml`. When the `load_factor` of the map goes 644 * below `min_load_factor` after some erase operations, the map will be 645 * shrunk when an insertion occurs. The erase method itself never shrinks 646 * the map. 647 * 648 * The default value of `min_load_factor` is 0.0f, the map never shrinks by default. 649 */ min_load_factor(float ml)650 void min_load_factor(float ml) { m_ht.min_load_factor(ml); } max_load_factor(float ml)651 void max_load_factor(float ml) { m_ht.max_load_factor(ml); } 652 rehash(size_type count)653 void rehash(size_type count) { m_ht.rehash(count); } reserve(size_type count)654 void reserve(size_type count) { m_ht.reserve(count); } 655 656 657 /* 658 * Observers 659 */ hash_function()660 hasher hash_function() const { return m_ht.hash_function(); } key_eq()661 key_equal key_eq() const { return m_ht.key_eq(); } 662 663 /* 664 * Other 665 */ 666 667 /** 668 * Convert a const_iterator to an iterator. 669 */ mutable_iterator(const_iterator pos)670 iterator mutable_iterator(const_iterator pos) { 671 return m_ht.mutable_iterator(pos); 672 } 673 674 friend bool operator==(const robin_map& lhs, const robin_map& rhs) { 675 if(lhs.size() != rhs.size()) { 676 return false; 677 } 678 679 for(const auto& element_lhs: lhs) { 680 const auto it_element_rhs = rhs.find(element_lhs.first); 681 if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) { 682 return false; 683 } 684 } 685 686 return true; 687 } 688 689 friend bool operator!=(const robin_map& lhs, const robin_map& rhs) { 690 return !operator==(lhs, rhs); 691 } 692 swap(robin_map & lhs,robin_map & rhs)693 friend void swap(robin_map& lhs, robin_map& rhs) { 694 lhs.swap(rhs); 695 } 696 697 private: 698 ht m_ht; 699 }; 700 701 702 /** 703 * Same as `tsl::robin_map<Key, T, Hash, KeyEqual, Allocator, StoreHash, tsl::rh::prime_growth_policy>`. 704 */ 705 template<class Key, 706 class T, 707 class Hash = std::hash<Key>, 708 class KeyEqual = std::equal_to<Key>, 709 class Allocator = std::allocator<std::pair<Key, T>>, 710 bool StoreHash = false> 711 using robin_pg_map = robin_map<Key, T, Hash, KeyEqual, Allocator, StoreHash, tsl::rh::prime_growth_policy>; 712 713 } // end namespace tsl 714 715 #endif 716