24#ifndef TSL_HOPSCOTCH_SC_MAP_H
25#define TSL_HOPSCOTCH_SC_MAP_H
31#include <initializer_list>
36#include "hopscotch_hash.h"
58 class Hash = std::hash<Key>,
59 class KeyEqual = std::equal_to<Key>,
60 class Compare = std::less<Key>,
61 class Allocator = std::allocator<std::pair<const Key, T>>,
62 unsigned int NeighborhoodSize = 62,
63 bool StoreHash =
false,
74 const key_type& operator()(
const std::pair<const Key, T>& key_value)
const {
75 return key_value.first;
78 const key_type& operator()(std::pair<const Key, T>& key_value) {
79 return key_value.first;
87 const value_type& operator()(
const std::pair<const Key, T>& key_value)
const {
88 return key_value.second;
91 value_type& operator()(std::pair<Key, T>& key_value) {
92 return key_value.second;
99 using overflow_container_type = std::map<Key, T, Compare, Allocator>;
102 Allocator, NeighborhoodSize,
103 StoreHash, GrowthPolicy,
104 overflow_container_type>;
107 using key_type =
typename ht::key_type;
108 using mapped_type = T;
109 using value_type =
typename ht::value_type;
110 using size_type =
typename ht::size_type;
111 using difference_type =
typename ht::difference_type;
112 using hasher =
typename ht::hasher;
113 using key_equal =
typename ht::key_equal;
114 using key_compare = Compare;
115 using allocator_type =
typename ht::allocator_type;
116 using reference =
typename ht::reference;
117 using const_reference =
typename ht::const_reference;
118 using pointer =
typename ht::pointer;
119 using const_pointer =
typename ht::const_pointer;
131 const Hash& hash = Hash(),
132 const KeyEqual& equal = KeyEqual(),
133 const Allocator& alloc = Allocator(),
134 const Compare& comp = Compare()) :
135 m_ht(bucket_count, hash, equal, alloc, ht::DEFAULT_MAX_LOAD_FACTOR, comp)
139 hopscotch_sc_map(size_type bucket_count,
140 const Allocator& alloc) : hopscotch_sc_map(bucket_count, Hash(), KeyEqual(), alloc)
144 hopscotch_sc_map(size_type bucket_count,
146 const Allocator& alloc) : hopscotch_sc_map(bucket_count, hash, KeyEqual(), alloc)
150 explicit hopscotch_sc_map(
const Allocator& alloc) : hopscotch_sc_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
153 template<
class InputIt>
154 hopscotch_sc_map(InputIt first, InputIt last,
155 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
156 const Hash& hash = Hash(),
157 const KeyEqual& equal = KeyEqual(),
158 const Allocator& alloc = Allocator()) : hopscotch_sc_map(bucket_count, hash, equal, alloc)
163 template<
class InputIt>
164 hopscotch_sc_map(InputIt first, InputIt last,
165 size_type bucket_count,
166 const Allocator& alloc) : hopscotch_sc_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
170 template<
class InputIt>
171 hopscotch_sc_map(InputIt first, InputIt last,
172 size_type bucket_count,
174 const Allocator& alloc) : hopscotch_sc_map(first, last, bucket_count, hash, KeyEqual(), alloc)
178 hopscotch_sc_map(std::initializer_list<value_type> init,
179 size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,
180 const Hash& hash = Hash(),
181 const KeyEqual& equal = KeyEqual(),
182 const Allocator& alloc = Allocator()) :
183 hopscotch_sc_map(
init.begin(),
init.end(), bucket_count, hash, equal, alloc)
187 hopscotch_sc_map(std::initializer_list<value_type> init,
188 size_type bucket_count,
189 const Allocator& alloc) :
190 hopscotch_sc_map(
init.begin(),
init.end(), bucket_count, Hash(), KeyEqual(), alloc)
194 hopscotch_sc_map(std::initializer_list<value_type> init,
195 size_type bucket_count,
197 const Allocator& alloc) :
198 hopscotch_sc_map(
init.begin(),
init.end(), bucket_count, hash, KeyEqual(), alloc)
203 hopscotch_sc_map& operator=(std::initializer_list<value_type> ilist) {
206 m_ht.reserve(ilist.size());
207 m_ht.insert(ilist.begin(), ilist.end());
212 allocator_type get_allocator()
const {
return m_ht.get_allocator(); }
218 iterator begin() noexcept {
return m_ht.begin(); }
219 const_iterator begin() const noexcept {
return m_ht.begin(); }
220 const_iterator cbegin() const noexcept {
return m_ht.cbegin(); }
222 iterator end() noexcept {
return m_ht.end(); }
223 const_iterator end() const noexcept {
return m_ht.end(); }
224 const_iterator cend() const noexcept {
return m_ht.cend(); }
230 bool empty() const noexcept {
return m_ht.empty(); }
231 size_type size() const noexcept {
return m_ht.size(); }
232 size_type max_size() const noexcept {
return m_ht.max_size(); }
237 void clear() noexcept { m_ht.clear(); }
242 std::pair<iterator, bool> insert(
const value_type& value) {
243 return m_ht.insert(value);
246 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
247 std::pair<iterator, bool> insert(
P&& value) {
248 return m_ht.insert(std::forward<P>(value));
251 std::pair<iterator, bool> insert(value_type&& value) {
252 return m_ht.insert(std::move(value));
256 iterator insert(const_iterator hint,
const value_type& value) {
257 return m_ht.insert(hint, value);
260 template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* =
nullptr>
261 iterator insert(const_iterator hint,
P&& value) {
262 return m_ht.insert(hint, std::forward<P>(value));
265 iterator insert(const_iterator hint, value_type&& value) {
266 return m_ht.insert(hint, std::move(value));
270 template<
class InputIt>
271 void insert(InputIt first, InputIt last) {
272 m_ht.insert(first, last);
275 void insert(std::initializer_list<value_type> ilist) {
276 m_ht.insert(ilist.begin(), ilist.end());
283 std::pair<iterator, bool> insert_or_assign(
const key_type& k, M&& obj) {
284 return m_ht.insert_or_assign(k, std::forward<M>(obj));
288 std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
289 return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
293 iterator insert_or_assign(const_iterator hint,
const key_type& k, M&& obj) {
294 return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
298 iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
299 return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
310 template<
class... Args>
311 std::pair<iterator, bool>
emplace(Args&&... args) {
312 return m_ht.emplace(std::forward<Args>(args)...);
324 template<
class... Args>
326 return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
332 template<
class... Args>
333 std::pair<iterator, bool> try_emplace(
const key_type& k, Args&&... args) {
334 return m_ht.try_emplace(k, std::forward<Args>(args)...);
337 template<
class... Args>
338 std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
339 return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
342 template<
class... Args>
343 iterator try_emplace(const_iterator hint,
const key_type& k, Args&&... args) {
344 return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
347 template<
class... Args>
348 iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
349 return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
355 iterator erase(iterator pos) {
return m_ht.erase(pos); }
356 iterator erase(const_iterator pos) {
return m_ht.erase(pos); }
357 iterator erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
358 size_type erase(
const key_type& key) {
return m_ht.erase(key); }
364 size_type
erase(
const key_type& key, std::size_t precalculated_hash) {
365 return m_ht.erase(key, precalculated_hash);
373 template<
class K,
class KE = KeyEqual,
class CP = Compare,
374 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
375 size_type
erase(
const K& key) {
return m_ht.erase(key); }
383 template<
class K,
class KE = KeyEqual,
class CP = Compare,
384 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
385 size_type
erase(
const K& key, std::size_t precalculated_hash) {
return m_ht.erase(key, precalculated_hash); }
395 T& at(
const Key& key) {
return m_ht.at(key); }
401 T&
at(
const Key& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
403 const T& at(
const Key& key)
const {
return m_ht.at(key); }
408 const T&
at(
const Key& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
415 template<
class K,
class KE = KeyEqual,
class CP = Compare,
416 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
417 T&
at(
const K& key) {
return m_ht.at(key); }
425 template<
class K,
class KE = KeyEqual,
class CP = Compare,
426 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
427 T&
at(
const K& key, std::size_t precalculated_hash) {
return m_ht.at(key, precalculated_hash); }
432 template<
class K,
class KE = KeyEqual,
class CP = Compare,
433 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
434 const T&
at(
const K& key)
const {
return m_ht.at(key); }
439 template<
class K,
class KE = KeyEqual,
class CP = Compare,
440 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
441 const T&
at(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.at(key, precalculated_hash); }
446 T& operator[](
const Key& key) {
return m_ht[key]; }
447 T& operator[](Key&& key) {
return m_ht[std::move(key)]; }
452 size_type count(
const Key& key)
const {
return m_ht.count(key); }
458 size_type
count(
const Key& key, std::size_t precalculated_hash)
const {
return m_ht.count(key, precalculated_hash); }
465 template<
class K,
class KE = KeyEqual,
class CP = Compare,
466 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
467 size_type
count(
const K& key)
const {
return m_ht.count(key); }
475 template<
class K,
class KE = KeyEqual,
class CP = Compare,
476 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
477 size_type
count(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.count(key, precalculated_hash); }
482 iterator find(
const Key& key) {
return m_ht.find(key); }
488 iterator
find(
const Key& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
490 const_iterator find(
const Key& key)
const {
return m_ht.find(key); }
495 const_iterator
find(
const Key& key, std::size_t precalculated_hash)
const {
return m_ht.find(key, precalculated_hash); }
502 template<
class K,
class KE = KeyEqual,
class CP = Compare,
503 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
504 iterator
find(
const K& key) {
return m_ht.find(key); }
512 template<
class K,
class KE = KeyEqual,
class CP = Compare,
513 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
514 iterator
find(
const K& key, std::size_t precalculated_hash) {
return m_ht.find(key, precalculated_hash); }
519 template<
class K,
class KE = KeyEqual,
class CP = Compare,
520 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
521 const_iterator
find(
const K& key)
const {
return m_ht.find(key); }
526 template<
class K,
class KE = KeyEqual,
class CP = Compare,
527 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
528 const_iterator
find(
const K& key, std::size_t precalculated_hash)
const {
return m_ht.find(key, precalculated_hash); }
533 std::pair<iterator, iterator> equal_range(
const Key& key) {
return m_ht.equal_range(key); }
539 std::pair<iterator, iterator>
equal_range(
const Key& key, std::size_t precalculated_hash) {
540 return m_ht.equal_range(key, precalculated_hash);
543 std::pair<const_iterator, const_iterator> equal_range(
const Key& key)
const {
return m_ht.equal_range(key); }
548 std::pair<const_iterator, const_iterator>
equal_range(
const Key& key, std::size_t precalculated_hash)
const {
549 return m_ht.equal_range(key, precalculated_hash);
557 template<
class K,
class KE = KeyEqual,
class CP = Compare,
558 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
559 std::pair<iterator, iterator>
equal_range(
const K& key) {
return m_ht.equal_range(key); }
567 template<
class K,
class KE = KeyEqual,
class CP = Compare,
568 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
569 std::pair<iterator, iterator>
equal_range(
const K& key, std::size_t precalculated_hash) {
570 return m_ht.equal_range(key, precalculated_hash);
576 template<
class K,
class KE = KeyEqual,
class CP = Compare,
577 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
578 std::pair<const_iterator, const_iterator>
equal_range(
const K& key)
const {
return m_ht.equal_range(key); }
583 template<
class K,
class KE = KeyEqual,
class CP = Compare,
584 typename std::enable_if<has_is_transparent<KE>::value && has_is_transparent<CP>::value>::type* =
nullptr>
585 std::pair<const_iterator, const_iterator>
equal_range(
const K& key, std::size_t precalculated_hash)
const {
586 return m_ht.equal_range(key, precalculated_hash);
595 size_type bucket_count()
const {
return m_ht.bucket_count(); }
596 size_type max_bucket_count()
const {
return m_ht.max_bucket_count(); }
602 float load_factor()
const {
return m_ht.load_factor(); }
603 float max_load_factor()
const {
return m_ht.max_load_factor(); }
604 void max_load_factor(
float ml) { m_ht.max_load_factor(ml); }
606 void rehash(size_type count) { m_ht.rehash(count); }
607 void reserve(size_type count) { m_ht.reserve(count); }
613 hasher hash_function()
const {
return m_ht.hash_function(); }
614 key_equal key_eq()
const {
return m_ht.key_eq(); }
615 key_compare key_comp()
const {
return m_ht.key_comp(); }
625 return m_ht.mutable_iterator(pos);
628 size_type overflow_size() const noexcept {
return m_ht.overflow_size(); }
630 friend bool operator==(
const hopscotch_sc_map& lhs,
const hopscotch_sc_map& rhs) {
631 if(lhs.size() != rhs.size()) {
635 for(
const auto& element_lhs : lhs) {
636 const auto it_element_rhs = rhs.find(element_lhs.first);
637 if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
645 friend bool operator!=(
const hopscotch_sc_map& lhs,
const hopscotch_sc_map& rhs) {
646 return !operator==(lhs, rhs);
649 friend void swap(hopscotch_sc_map& lhs, hopscotch_sc_map& rhs) {
Test structure used for several test.
size_type count(const K &key) const
iterator find(const K &key, std::size_t precalculated_hash)
size_type count(const K &key, std::size_t precalculated_hash) const
size_type erase(const K &key)
iterator find(const Key &key, std::size_t precalculated_hash)
iterator emplace_hint(const_iterator hint, Args &&... args)
const T & at(const K &key, std::size_t precalculated_hash) const
T & at(const K &key, std::size_t precalculated_hash)
const T & at(const Key &key, std::size_t precalculated_hash) const
size_type count(const Key &key, std::size_t precalculated_hash) const
const_iterator find(const K &key) const
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
std::pair< iterator, bool > emplace(Args &&... args)
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
T & at(const Key &key, std::size_t precalculated_hash)
size_type erase(const key_type &key, std::size_t precalculated_hash)
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
iterator mutable_iterator(const_iterator pos)
const_iterator find(const K &key, std::size_t precalculated_hash) const
const_iterator find(const Key &key, std::size_t precalculated_hash) const
const T & at(const K &key) const
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
std::pair< iterator, iterator > equal_range(const K &key)
size_type erase(const K &key, std::size_t precalculated_hash)
iterator find(const K &key)
OutputIteratorT OffsetT ReductionOpT OuputT init
< [in] The initial value of the reduction
Structure required for the Sph Harmonic amplitude dictionary arguments.