24#ifndef TSL_HOPSCOTCH_MAP_H
25#define TSL_HOPSCOTCH_MAP_H
31#include <initializer_list>
36#include "hopscotch_hash.h"
73 class Hash = std::hash<Key>,
74 class KeyEqual = std::equal_to<Key>,
75 class Allocator = std::allocator<std::pair<Key, T>>,
76 unsigned int NeighborhoodSize = 62,
77 bool StoreHash =
false,
88 const key_type& operator()(
const std::pair<Key, T>& key_value)
const {
89 return key_value.first;
92 key_type& operator()(std::pair<Key, T>& key_value) {
93 return key_value.first;
101 const value_type& operator()(
const std::pair<Key, T>& key_value)
const {
102 return key_value.second;
105 value_type& operator()(std::pair<Key, T>& key_value) {
106 return key_value.second;
111 using overflow_container_type = std::list<std::pair<Key, T>, Allocator>;
114 Allocator, NeighborhoodSize,
115 StoreHash, GrowthPolicy,
116 overflow_container_type>;
119 using key_type =
typename ht::key_type;
120 using mapped_type = T;
121 using value_type =
typename ht::value_type;
122 using size_type =
typename ht::size_type;
123 using difference_type =
typename ht::difference_type;
124 using hasher =
typename ht::hasher;
125 using key_equal =
typename ht::key_equal;
126 using allocator_type =
typename ht::allocator_type;
127 using reference =
typename ht::reference;
128 using const_reference =
typename ht::const_reference;
129 using pointer =
typename ht::pointer;
130 using const_pointer =
typename ht::const_pointer;
143 const Hash& hash = Hash(),
144 const KeyEqual& equal = KeyEqual(),
145 const Allocator& alloc = Allocator()) :
146 m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR)
150 hopscotch_map(size_type bucket_count,
151 const Allocator& alloc) : hopscotch_map(bucket_count, Hash(), KeyEqual(), alloc)
155 hopscotch_map(size_type bucket_count,
157 const Allocator& alloc) : hopscotch_map(bucket_count, hash, KeyEqual(), alloc)
161 explicit hopscotch_map(
const Allocator& alloc) : hopscotch_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
164 template<
class InputIt>
165 hopscotch_map(InputIt first, InputIt last,
166 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
167 const Hash& hash = Hash(),
168 const KeyEqual& equal = KeyEqual(),
169 const Allocator& alloc = Allocator()) : hopscotch_map(bucket_count, hash, equal, alloc)
174 template<
class InputIt>
175 hopscotch_map(InputIt first, InputIt last,
176 size_type bucket_count,
177 const Allocator& alloc) : hopscotch_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
181 template<
class InputIt>
182 hopscotch_map(InputIt first, InputIt last,
183 size_type bucket_count,
185 const Allocator& alloc) : hopscotch_map(first, last, bucket_count, hash, KeyEqual(), alloc)
189 hopscotch_map(std::initializer_list<value_type> init,
190 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
191 const Hash& hash = Hash(),
192 const KeyEqual& equal = KeyEqual(),
193 const Allocator& alloc = Allocator()) :
194 hopscotch_map(
init.begin(),
init.end(), bucket_count, hash, equal, alloc)
198 hopscotch_map(std::initializer_list<value_type> init,
199 size_type bucket_count,
200 const Allocator& alloc) :
201 hopscotch_map(
init.begin(),
init.end(), bucket_count, Hash(), KeyEqual(), alloc)
205 hopscotch_map(std::initializer_list<value_type> init,
206 size_type bucket_count,
208 const Allocator& alloc) :
209 hopscotch_map(
init.begin(),
init.end(), bucket_count, hash, KeyEqual(), alloc)
214 hopscotch_map& operator=(std::initializer_list<value_type> ilist) {
217 m_ht.reserve(ilist.size());
218 m_ht.insert(ilist.begin(), ilist.end());
223 allocator_type get_allocator()
const {
return m_ht.get_allocator(); }
229 iterator begin() noexcept {
return m_ht.begin(); }
230 const_iterator begin() const noexcept {
return m_ht.begin(); }
231 const_iterator cbegin() const noexcept {
return m_ht.cbegin(); }
233 iterator end() noexcept {
return m_ht.end(); }
234 const_iterator end() const noexcept {
return m_ht.end(); }
235 const_iterator cend() const noexcept {
return m_ht.cend(); }
241 bool empty() const noexcept {
return m_ht.empty(); }
242 size_type size() const noexcept {
return m_ht.size(); }
243 size_type max_size() const noexcept {
return m_ht.max_size(); }
248 void clear() noexcept { m_ht.clear(); }
253 std::pair<iterator, bool> insert(
const value_type& value) {
254 return m_ht.insert(value);
257 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
258 std::pair<iterator, bool> insert(
P&& value) {
259 return m_ht.insert(std::forward<P>(value));
262 std::pair<iterator, bool> insert(value_type&& value) {
263 return m_ht.insert(std::move(value));
267 iterator insert(const_iterator hint,
const value_type& value) {
268 return m_ht.insert(hint, value);
271 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
272 iterator insert(const_iterator hint,
P&& value) {
273 return m_ht.insert(hint, std::forward<P>(value));
276 iterator insert(const_iterator hint, value_type&& value) {
277 return m_ht.insert(hint, std::move(value));
281 template<
class InputIt>
282 void insert(InputIt first, InputIt last) {
283 m_ht.insert(first, last);
286 void insert(std::initializer_list<value_type> ilist) {
287 m_ht.insert(ilist.begin(), ilist.end());
294 std::pair<iterator, bool> insert_or_assign(
const key_type& k, M&& obj) {
295 return m_ht.insert_or_assign(k, std::forward<M>(obj));
299 std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
300 return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
304 iterator insert_or_assign(const_iterator hint,
const key_type& k, M&& obj) {
305 return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
309 iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
310 return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
322 template<
class... Args>
323 std::pair<iterator, bool>
emplace(Args&&... args) {
324 return m_ht.emplace(std::forward<Args>(args)...);
336 template<
class... Args>
338 return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
344 template<
class... Args>
345 std::pair<iterator, bool> try_emplace(
const key_type& k, Args&&... args) {
346 return m_ht.try_emplace(k, std::forward<Args>(args)...);
349 template<
class... Args>
350 std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
351 return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
354 template<
class... Args>
355 iterator try_emplace(const_iterator hint,
const key_type& k, Args&&... args) {
356 return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
359 template<
class... Args>
360 iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
361 return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
367 iterator erase(iterator pos) {
return m_ht.erase(pos); }
368 iterator erase(const_iterator pos) {
return m_ht.erase(pos); }
369 iterator erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
370 size_type erase(
const key_type& key) {
return m_ht.erase(key); }
376 size_type
erase(
const key_type& key, std::size_t precalculated_hash) {
377 return m_ht.erase(key, precalculated_hash);
384 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
385 size_type
erase(
const K& key) {
return m_ht.erase(key); }
393 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
394 size_type
erase(
const K& key, std::size_t precalculated_hash) {
395 return m_ht.erase(key, precalculated_hash);
406 T& at(
const Key& key) {
return m_ht.at(key); }
412 T&
at(
const Key& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
415 const T& at(
const Key& key)
const {
return m_ht.at(key); }
420 const T&
at(
const Key& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
427 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
428 T&
at(
const K& key) {
return m_ht.at(key); }
436 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
437 T&
at(
const K& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
443 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
444 const T&
at(
const K& key)
const {
return m_ht.at(key); }
449 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
450 const T&
at(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
455 T& operator[](
const Key& key) {
return m_ht[key]; }
456 T& operator[](Key&& key) {
return m_ht[std::move(key)]; }
461 size_type count(
const Key& key)
const {
return m_ht.count(key); }
467 size_type
count(
const Key& key, std::size_t precalculated_hash)
const {
468 return m_ht.count(key, precalculated_hash);
475 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
476 size_type
count(
const K& key)
const {
return m_ht.count(key); }
484 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
485 size_type
count(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.count(key, precalculated_hash); }
490 iterator find(
const Key& key) {
return m_ht.find(key); }
496 iterator
find(
const Key& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
498 const_iterator find(
const Key& key)
const {
return m_ht.find(key); }
503 const_iterator
find(
const Key& key, std::size_t precalculated_hash)
const {
504 return m_ht.find(key, precalculated_hash);
511 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
512 iterator
find(
const K& key) {
return m_ht.find(key); }
520 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
521 iterator
find(
const K& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
526 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
527 const_iterator
find(
const K& key)
const {
return m_ht.find(key); }
535 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
536 const_iterator
find(
const K& key, std::size_t precalculated_hash)
const {
537 return m_ht.find(key, precalculated_hash);
543 std::pair<iterator, iterator> equal_range(
const Key& key) {
return m_ht.equal_range(key); }
549 std::pair<iterator, iterator>
equal_range(
const Key& key, std::size_t precalculated_hash) {
550 return m_ht.equal_range(key, precalculated_hash);
553 std::pair<const_iterator, const_iterator> equal_range(
const Key& key)
const {
return m_ht.equal_range(key); }
558 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key, std::size_t precalculated_hash)
const {
559 return m_ht.equal_range(key, precalculated_hash);
566 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
567 std::pair<iterator, iterator>
equal_range(
const K& key) {
return m_ht.equal_range(key); }
576 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
577 std::pair<iterator, iterator>
equal_range(
const K& key, std::size_t precalculated_hash) {
578 return m_ht.equal_range(key, precalculated_hash);
584 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
585 std::pair<const_iterator, const_iterator>
equal_range(
const K& key)
const {
return m_ht.equal_range(key); }
590 template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* =
nullptr>
591 std::pair<const_iterator, const_iterator>
equal_range(
const K& key, std::size_t precalculated_hash)
const {
592 return m_ht.equal_range(key, precalculated_hash);
601 size_type bucket_count()
const {
return m_ht.bucket_count(); }
602 size_type max_bucket_count()
const {
return m_ht.max_bucket_count(); }
608 float load_factor()
const {
return m_ht.load_factor(); }
609 float max_load_factor()
const {
return m_ht.max_load_factor(); }
610 void max_load_factor(
float ml) { m_ht.max_load_factor(ml); }
612 void rehash(size_type count) { m_ht.rehash(count); }
613 void reserve(size_type count) { m_ht.reserve(count); }
619 hasher hash_function()
const {
return m_ht.hash_function(); }
620 key_equal key_eq()
const {
return m_ht.key_eq(); }
630 return m_ht.mutable_iterator(pos);
633 size_type overflow_size() const noexcept {
return m_ht.overflow_size(); }
635 friend bool operator==(
const hopscotch_map& lhs,
const hopscotch_map& rhs) {
636 if(lhs.size() != rhs.size()) {
640 for(
const auto& element_lhs : lhs) {
641 const auto it_element_rhs = rhs.find(element_lhs.first);
642 if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
650 friend bool operator!=(
const hopscotch_map& lhs,
const hopscotch_map& rhs) {
651 return !operator==(lhs, rhs);
654 friend void swap(hopscotch_map& lhs, hopscotch_map& rhs) {
Test structure used for several test.
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
iterator find(const K &key, std::size_t precalculated_hash)
const T & at(const K &key) const
const T & at(const K &key, std::size_t precalculated_hash) const
size_type erase(const key_type &key, std::size_t precalculated_hash)
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
const_iterator find(const K &key, std::size_t precalculated_hash) const
T & at(const K &key, std::size_t precalculated_hash)
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
size_type erase(const K &key)
iterator find(const Key &key, std::size_t precalculated_hash)
size_type erase(const K &key, std::size_t precalculated_hash)
const T & at(const Key &key, std::size_t precalculated_hash) const
size_type count(const K &key, std::size_t precalculated_hash) const
std::pair< iterator, bool > emplace(Args &&... args)
T & at(const Key &key, std::size_t precalculated_hash)
std::pair< iterator, iterator > equal_range(const K &key)
size_type count(const Key &key, std::size_t precalculated_hash) const
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
iterator emplace_hint(const_iterator hint, Args &&... args)
const_iterator find(const Key &key, std::size_t precalculated_hash) const
size_type count(const K &key) const
const_iterator find(const K &key) const
iterator find(const K &key)
iterator mutable_iterator(const_iterator pos)
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
Structure required for the Sph Harmonic amplitude dictionary arguments.