OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
hopscotch_sc_map.h
1
24#ifndef TSL_HOPSCOTCH_SC_MAP_H
25#define TSL_HOPSCOTCH_SC_MAP_H
26
27
28#include <algorithm>
29#include <cstddef>
30#include <functional>
31#include <initializer_list>
32#include <map>
33#include <memory>
34#include <type_traits>
35#include <utility>
36#include "hopscotch_hash.h"
37
38
39namespace tsl {
40
41
56template<class Key,
57 class T,
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,
64 class GrowthPolicy = tsl::power_of_two_growth_policy>
66private:
67 template<typename U>
69
70 class KeySelect {
71 public:
72 using key_type = Key;
73
74 const key_type& operator()(const std::pair<const Key, T>& key_value) const {
75 return key_value.first;
76 }
77
78 const key_type& operator()(std::pair<const Key, T>& key_value) {
79 return key_value.first;
80 }
81 };
82
84 public:
85 using value_type = T;
86
87 const value_type& operator()(const std::pair<const Key, T>& key_value) const {
88 return key_value.second;
89 }
90
91 value_type& operator()(std::pair<Key, T>& key_value) {
92 return key_value.second;
93 }
94 };
95
96
97 // TODO Not optimal as we have to use std::pair<const Key, T> as ValueType which forbid
98 // us to move the key in the bucket array, we have to use copy. Optimize.
99 using overflow_container_type = std::map<Key, T, Compare, Allocator>;
101 Hash, KeyEqual,
102 Allocator, NeighborhoodSize,
103 StoreHash, GrowthPolicy,
104 overflow_container_type>;
105
106public:
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;
120 using iterator = typename ht::iterator;
121 using const_iterator = typename ht::const_iterator;
122
123
124 /*
125 * Constructors
126 */
127 hopscotch_sc_map() : hopscotch_sc_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {
128 }
129
130 explicit hopscotch_sc_map(size_type bucket_count,
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)
136 {
137 }
138
139 hopscotch_sc_map(size_type bucket_count,
140 const Allocator& alloc) : hopscotch_sc_map(bucket_count, Hash(), KeyEqual(), alloc)
141 {
142 }
143
144 hopscotch_sc_map(size_type bucket_count,
145 const Hash& hash,
146 const Allocator& alloc) : hopscotch_sc_map(bucket_count, hash, KeyEqual(), alloc)
147 {
148 }
149
150 explicit hopscotch_sc_map(const Allocator& alloc) : hopscotch_sc_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
151 }
152
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)
159 {
160 insert(first, last);
161 }
162
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)
167 {
168 }
169
170 template<class InputIt>
171 hopscotch_sc_map(InputIt first, InputIt last,
172 size_type bucket_count,
173 const Hash& hash,
174 const Allocator& alloc) : hopscotch_sc_map(first, last, bucket_count, hash, KeyEqual(), alloc)
175 {
176 }
177
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)
184 {
185 }
186
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)
191 {
192 }
193
194 hopscotch_sc_map(std::initializer_list<value_type> init,
195 size_type bucket_count,
196 const Hash& hash,
197 const Allocator& alloc) :
198 hopscotch_sc_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
199 {
200 }
201
202
203 hopscotch_sc_map& operator=(std::initializer_list<value_type> ilist) {
204 m_ht.clear();
205
206 m_ht.reserve(ilist.size());
207 m_ht.insert(ilist.begin(), ilist.end());
208
209 return *this;
210 }
211
212 allocator_type get_allocator() const { return m_ht.get_allocator(); }
213
214
215 /*
216 * Iterators
217 */
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(); }
221
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(); }
225
226
227 /*
228 * Capacity
229 */
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(); }
233
234 /*
235 * Modifiers
236 */
237 void clear() noexcept { m_ht.clear(); }
238
239
240
241
242 std::pair<iterator, bool> insert(const value_type& value) {
243 return m_ht.insert(value);
244 }
245
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));
249 }
250
251 std::pair<iterator, bool> insert(value_type&& value) {
252 return m_ht.insert(std::move(value));
253 }
254
255
256 iterator insert(const_iterator hint, const value_type& value) {
257 return m_ht.insert(hint, value);
258 }
259
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));
263 }
264
265 iterator insert(const_iterator hint, value_type&& value) {
266 return m_ht.insert(hint, std::move(value));
267 }
268
269
270 template<class InputIt>
271 void insert(InputIt first, InputIt last) {
272 m_ht.insert(first, last);
273 }
274
275 void insert(std::initializer_list<value_type> ilist) {
276 m_ht.insert(ilist.begin(), ilist.end());
277 }
278
279
280
281
282 template<class M>
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));
285 }
286
287 template<class M>
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));
290 }
291
292 template<class M>
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));
295 }
296
297 template<class M>
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));
300 }
301
302
303
310 template<class... Args>
311 std::pair<iterator, bool> emplace(Args&&... args) {
312 return m_ht.emplace(std::forward<Args>(args)...);
313 }
314
315
316
317
324 template<class... Args>
325 iterator emplace_hint(const_iterator hint, Args&&... args) {
326 return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
327 }
328
329
330
331
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)...);
335 }
336
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)...);
340 }
341
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)...);
345 }
346
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)...);
350 }
351
352
353
354
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); }
359
364 size_type erase(const key_type& key, std::size_t precalculated_hash) {
365 return m_ht.erase(key, precalculated_hash);
366 }
367
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); }
376
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); }
386
387
388
389
390 void swap(hopscotch_sc_map& other) { other.m_ht.swap(m_ht); }
391
392 /*
393 * Lookup
394 */
395 T& at(const Key& key) { return m_ht.at(key); }
396
401 T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
402
403 const T& at(const Key& key) const { return m_ht.at(key); }
404
408 const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
409
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); }
418
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); }
428
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); }
435
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); }
442
443
444
445
446 T& operator[](const Key& key) { return m_ht[key]; }
447 T& operator[](Key&& key) { return m_ht[std::move(key)]; }
448
449
450
451
452 size_type count(const Key& key) const { return m_ht.count(key); }
453
458 size_type count(const Key& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
459
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); }
468
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); }
478
479
480
481
482 iterator find(const Key& key) { return m_ht.find(key); }
483
488 iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
489
490 const_iterator find(const Key& key) const { return m_ht.find(key); }
491
495 const_iterator find(const Key& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }
496
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); }
505
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); }
515
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); }
522
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); }
529
530
531
532
533 std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
534
539 std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) {
540 return m_ht.equal_range(key, precalculated_hash);
541 }
542
543 std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); }
544
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);
550 }
551
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); }
560
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);
571 }
572
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); }
579
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);
587 }
588
589
590
591
592 /*
593 * Bucket interface
594 */
595 size_type bucket_count() const { return m_ht.bucket_count(); }
596 size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
597
598
599 /*
600 * Hash policy
601 */
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); }
605
606 void rehash(size_type count) { m_ht.rehash(count); }
607 void reserve(size_type count) { m_ht.reserve(count); }
608
609
610 /*
611 * Observers
612 */
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(); }
616
617 /*
618 * Other
619 */
620
624 iterator mutable_iterator(const_iterator pos) {
625 return m_ht.mutable_iterator(pos);
626 }
627
628 size_type overflow_size() const noexcept { return m_ht.overflow_size(); }
629
630 friend bool operator==(const hopscotch_sc_map& lhs, const hopscotch_sc_map& rhs) {
631 if(lhs.size() != rhs.size()) {
632 return false;
633 }
634
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) {
638 return false;
639 }
640 }
641
642 return true;
643 }
644
645 friend bool operator!=(const hopscotch_sc_map& lhs, const hopscotch_sc_map& rhs) {
646 return !operator==(lhs, rhs);
647 }
648
649 friend void swap(hopscotch_sc_map& lhs, hopscotch_sc_map& rhs) {
650 lhs.swap(rhs);
651 }
652
653
654
655private:
656 ht m_ht;
657};
658
659
660} // end namespace tsl
661
662
663#endif
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.