sparse-map
sparse_map.h
Go to the documentation of this file.
1 /**
2  * MIT License
3  *
4  * Copyright (c) 2017 Tessil
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_SPARSE_MAP_H
25 #define TSL_SPARSE_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 "sparse_hash.h"
35 
36 
37 namespace tsl {
38 
39 
40 /**
41  * Implementation of a sparse hash map using open-addressing with quadratic probing.
42  * The goal on the hash map is to be the most memory efficient possible, even at low load factor,
43  * while keeping reasonable performances.
44  *
45  * `GrowthPolicy` defines how the map grows and consequently how a hash value is mapped to a bucket.
46  * By default the map uses `tsl::sh::power_of_two_growth_policy`. This policy keeps the number of buckets
47  * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo.
48  * Other growth policies are available and you may define your own growth policy,
49  * check `tsl::sh::power_of_two_growth_policy` for the interface.
50  *
51  * `ExceptionSafety` defines the exception guarantee provided by the class. By default only the basic
52  * exception safety is guaranteed which mean that all resources used by the hash map will be freed (no memory leaks)
53  * but the hash map may end-up in an undefined state if an exception is thrown (undefined here means that some elements
54  * may be missing). This can ONLY happen on rehash (either on insert or if `rehash` is called explicitly) and will
55  * occur if the Allocator can't allocate memory (`std::bad_alloc`) or if the copy constructor (when a nothrow
56  * move constructor is not available) throws an exception. This can be avoided by calling `reserve` beforehand.
57  * This basic guarantee is similar to the one of `google::sparse_hash_map` and `spp::sparse_hash_map`.
58  * It is possible to ask for the strong exception guarantee with `tsl::sh::exception_safety::strong`, the drawback
59  * is that the map will be slower on rehashes and will also need more memory on rehashes.
60  *
61  * `Sparsity` defines how much the hash set will compromise between insertion speed and memory usage. A high
62  * sparsity means less memory usage but longer insertion times, and vice-versa for low sparsity. The default
63  * `tsl::sh::sparsity::medium` sparsity offers a good compromise. It doesn't change the lookup speed.
64  *
65  * `Key` and `T` must be nothrow move constructible and/or copy constructible.
66  *
67  * If the destructor of `Key` or `T` throws an exception, the behaviour of the class is undefined.
68  *
69  * Iterators invalidation:
70  * - clear, operator=, reserve, rehash: always invalidate the iterators.
71  * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.
72  * - erase: always invalidate the iterators.
73  */
74 template<class Key,
75  class T,
76  class Hash = std::hash<Key>,
77  class KeyEqual = std::equal_to<Key>,
78  class Allocator = std::allocator<std::pair<Key, T>>,
79  class GrowthPolicy = tsl::sh::power_of_two_growth_policy<2>,
82 class sparse_map {
83 private:
84  template<typename U>
85  using has_is_transparent = tsl::detail_sparse_hash::has_is_transparent<U>;
86 
87  class KeySelect {
88  public:
89  using key_type = Key;
90 
91  const key_type& operator()(const std::pair<Key, T>& key_value) const noexcept {
92  return key_value.first;
93  }
94 
95  key_type& operator()(std::pair<Key, T>& key_value) noexcept {
96  return key_value.first;
97  }
98  };
99 
100  class ValueSelect {
101  public:
102  using value_type = T;
103 
104  const value_type& operator()(const std::pair<Key, T>& key_value) const noexcept {
105  return key_value.second;
106  }
107 
108  value_type& operator()(std::pair<Key, T>& key_value) noexcept {
109  return key_value.second;
110  }
111  };
112 
113  using ht = detail_sparse_hash::sparse_hash<std::pair<Key, T>, KeySelect, ValueSelect,
114  Hash, KeyEqual, Allocator, GrowthPolicy,
115  ExceptionSafety, Sparsity,
117 
118 public:
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;
131  using iterator = typename ht::iterator;
132  using const_iterator = typename ht::const_iterator;
133 
134 
135 public:
136  /*
137  * Constructors
138  */
139  sparse_map(): sparse_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {
140  }
141 
142  explicit sparse_map(size_type bucket_count,
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)
147  {
148  }
149 
150  sparse_map(size_type bucket_count,
151  const Allocator& alloc): sparse_map(bucket_count, Hash(), KeyEqual(), alloc)
152  {
153  }
154 
155  sparse_map(size_type bucket_count,
156  const Hash& hash,
157  const Allocator& alloc): sparse_map(bucket_count, hash, KeyEqual(), alloc)
158  {
159  }
160 
161  explicit sparse_map(const Allocator& alloc): sparse_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {
162  }
163 
164  template<class InputIt>
165  sparse_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()): sparse_map(bucket_count, hash, equal, alloc)
170  {
171  insert(first, last);
172  }
173 
174  template<class InputIt>
175  sparse_map(InputIt first, InputIt last,
176  size_type bucket_count,
177  const Allocator& alloc): sparse_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)
178  {
179  }
180 
181  template<class InputIt>
182  sparse_map(InputIt first, InputIt last,
183  size_type bucket_count,
184  const Hash& hash,
185  const Allocator& alloc): sparse_map(first, last, bucket_count, hash, KeyEqual(), alloc)
186  {
187  }
188 
189  sparse_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  sparse_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)
195  {
196  }
197 
198  sparse_map(std::initializer_list<value_type> init,
199  size_type bucket_count,
200  const Allocator& alloc):
201  sparse_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)
202  {
203  }
204 
205  sparse_map(std::initializer_list<value_type> init,
206  size_type bucket_count,
207  const Hash& hash,
208  const Allocator& alloc):
209  sparse_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)
210  {
211  }
212 
213  sparse_map& operator=(std::initializer_list<value_type> ilist) {
214  m_ht.clear();
215 
216  m_ht.reserve(ilist.size());
217  m_ht.insert(ilist.begin(), ilist.end());
218 
219  return *this;
220  }
221 
222  allocator_type get_allocator() const { return m_ht.get_allocator(); }
223 
224 
225  /*
226  * Iterators
227  */
228  iterator begin() noexcept { return m_ht.begin(); }
229  const_iterator begin() const noexcept { return m_ht.begin(); }
230  const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
231 
232  iterator end() noexcept { return m_ht.end(); }
233  const_iterator end() const noexcept { return m_ht.end(); }
234  const_iterator cend() const noexcept { return m_ht.cend(); }
235 
236 
237  /*
238  * Capacity
239  */
240  bool empty() const noexcept { return m_ht.empty(); }
241  size_type size() const noexcept { return m_ht.size(); }
242  size_type max_size() const noexcept { return m_ht.max_size(); }
243 
244  /*
245  * Modifiers
246  */
247  void clear() noexcept { m_ht.clear(); }
248 
249 
250 
251  std::pair<iterator, bool> insert(const value_type& value) {
252  return m_ht.insert(value);
253  }
254 
255  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
256  std::pair<iterator, bool> insert(P&& value) {
257  return m_ht.emplace(std::forward<P>(value));
258  }
259 
260  std::pair<iterator, bool> insert(value_type&& value) {
261  return m_ht.insert(std::move(value));
262  }
263 
264 
265  iterator insert(const_iterator hint, const value_type& value) {
266  return m_ht.insert(hint, value);
267  }
268 
269  template<class P, typename std::enable_if<std::is_constructible<value_type, P&&>::value>::type* = nullptr>
270  iterator insert(const_iterator hint, P&& value) {
271  return m_ht.emplace_hint(hint, std::forward<P>(value));
272  }
273 
274  iterator insert(const_iterator hint, value_type&& value) {
275  return m_ht.insert(hint, std::move(value));
276  }
277 
278 
279  template<class InputIt>
280  void insert(InputIt first, InputIt last) {
281  m_ht.insert(first, last);
282  }
283 
284  void insert(std::initializer_list<value_type> ilist) {
285  m_ht.insert(ilist.begin(), ilist.end());
286  }
287 
288 
289 
290 
291  template<class M>
292  std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
293  return m_ht.insert_or_assign(k, std::forward<M>(obj));
294  }
295 
296  template<class M>
297  std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
298  return m_ht.insert_or_assign(std::move(k), std::forward<M>(obj));
299  }
300 
301  template<class M>
302  iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {
303  return m_ht.insert_or_assign(hint, k, std::forward<M>(obj));
304  }
305 
306  template<class M>
307  iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {
308  return m_ht.insert_or_assign(hint, std::move(k), std::forward<M>(obj));
309  }
310 
311 
312 
313  /**
314  * Due to the way elements are stored, emplace will need to move or copy the key-value once.
315  * The method is equivalent to `insert(value_type(std::forward<Args>(args)...));`.
316  *
317  * Mainly here for compatibility with the `std::unordered_map` interface.
318  */
319  template<class... Args>
320  std::pair<iterator, bool> emplace(Args&&... args) {
321  return m_ht.emplace(std::forward<Args>(args)...);
322  }
323 
324 
325 
326  /**
327  * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.
328  * The method is equivalent to `insert(hint, value_type(std::forward<Args>(args)...));`.
329  *
330  * Mainly here for compatibility with the `std::unordered_map` interface.
331  */
332  template<class... Args>
333  iterator emplace_hint(const_iterator hint, Args&&... args) {
334  return m_ht.emplace_hint(hint, std::forward<Args>(args)...);
335  }
336 
337 
338 
339 
340  template<class... Args>
341  std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args) {
342  return m_ht.try_emplace(k, std::forward<Args>(args)...);
343  }
344 
345  template<class... Args>
346  std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args) {
347  return m_ht.try_emplace(std::move(k), std::forward<Args>(args)...);
348  }
349 
350  template<class... Args>
351  iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {
352  return m_ht.try_emplace(hint, k, std::forward<Args>(args)...);
353  }
354 
355  template<class... Args>
356  iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {
357  return m_ht.try_emplace(hint, std::move(k), std::forward<Args>(args)...);
358  }
359 
360 
361 
362 
363  iterator erase(iterator pos) { return m_ht.erase(pos); }
364  iterator erase(const_iterator pos) { return m_ht.erase(pos); }
365  iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
366  size_type erase(const key_type& key) { return m_ht.erase(key); }
367 
368  /**
369  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
370  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
371  * if you already have the hash.
372  */
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>
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)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
389  * if you already have the hash.
390  */
391  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
392  size_type erase(const K& key, std::size_t precalculated_hash) {
393  return m_ht.erase(key, precalculated_hash);
394  }
395 
396 
397 
398  void swap(sparse_map& other) { other.m_ht.swap(m_ht); }
399 
400 
401 
402  /*
403  * Lookup
404  */
405  T& at(const Key& key) { return m_ht.at(key); }
406 
407  /**
408  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
409  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
410  * if you already have the hash.
411  */
412  T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
413 
414 
415  const T& at(const Key& key) const { return m_ht.at(key); }
416 
417  /**
418  * @copydoc at(const Key& key, std::size_t precalculated_hash)
419  */
420  const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
421 
422 
423  /**
424  * This overload only participates in the overload resolution if the typedef `KeyEqual::is_transparent` exists.
425  * If so, `K` must be hashable and comparable to `Key`.
426  */
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); }
429 
430  /**
431  * @copydoc at(const K& key)
432  *
433  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
434  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
435  * if you already have the hash.
436  */
437  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
438  T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }
439 
440 
441  /**
442  * @copydoc at(const K& key)
443  */
444  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
445  const T& at(const K& key) const { return m_ht.at(key); }
446 
447  /**
448  * @copydoc at(const K& key, std::size_t precalculated_hash)
449  */
450  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
451  const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }
452 
453 
454 
455 
456  T& operator[](const Key& key) { return m_ht[key]; }
457  T& operator[](Key&& key) { return m_ht[std::move(key)]; }
458 
459 
460 
461 
462  size_type count(const Key& key) const { return m_ht.count(key); }
463 
464  /**
465  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
466  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
467  * if you already have the hash.
468  */
469  size_type count(const Key& key, std::size_t precalculated_hash) const {
470  return m_ht.count(key, precalculated_hash);
471  }
472 
473  /**
474  * This overload only participates in the overload resolution if the typedef `KeyEqual::is_transparent` exists.
475  * If so, `K` must be hashable and comparable to `Key`.
476  */
477  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
478  size_type count(const K& key) const { return m_ht.count(key); }
479 
480  /**
481  * @copydoc count(const K& key) const
482  *
483  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
484  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
485  * if you already have the hash.
486  */
487  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
488  size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }
489 
490 
491 
492 
493  iterator find(const Key& key) { return m_ht.find(key); }
494 
495  /**
496  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
497  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
498  * if you already have the hash.
499  */
500  iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
501 
502  const_iterator find(const Key& key) const { return m_ht.find(key); }
503 
504  /**
505  * @copydoc find(const Key& key, std::size_t precalculated_hash)
506  */
507  const_iterator find(const Key& key, std::size_t precalculated_hash) const {
508  return m_ht.find(key, precalculated_hash);
509  }
510 
511  /**
512  * This overload only participates in the overload resolution if the typedef `KeyEqual::is_transparent` exists.
513  * If so, `K` must be hashable and comparable to `Key`.
514  */
515  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
516  iterator find(const K& key) { return m_ht.find(key); }
517 
518  /**
519  * @copydoc find(const K& key)
520  *
521  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
522  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
523  * if you already have the hash.
524  */
525  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
526  iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }
527 
528  /**
529  * @copydoc find(const K& key)
530  */
531  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
532  const_iterator find(const K& key) const { return m_ht.find(key); }
533 
534  /**
535  * @copydoc find(const K& key)
536  *
537  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
538  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
539  * if you already have the hash.
540  */
541  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
542  const_iterator find(const K& key, std::size_t precalculated_hash) const {
543  return m_ht.find(key, precalculated_hash);
544  }
545 
546 
547 
548 
549  std::pair<iterator, iterator> equal_range(const Key& key) { return m_ht.equal_range(key); }
550 
551  /**
552  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
553  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
554  * if you already have the hash.
555  */
556  std::pair<iterator, iterator> equal_range(const Key& key, std::size_t precalculated_hash) {
557  return m_ht.equal_range(key, precalculated_hash);
558  }
559 
560  std::pair<const_iterator, const_iterator> equal_range(const Key& key) const { return m_ht.equal_range(key); }
561 
562  /**
563  * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)
564  */
565  std::pair<const_iterator, const_iterator> equal_range(const Key& key, std::size_t precalculated_hash) const {
566  return m_ht.equal_range(key, precalculated_hash);
567  }
568 
569  /**
570  * This overload only participates in the overload resolution if the typedef `KeyEqual::is_transparent` exists.
571  * If so, `K` must be hashable and comparable to `Key`.
572  */
573  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
574  std::pair<iterator, iterator> equal_range(const K& key) { return m_ht.equal_range(key); }
575 
576 
577  /**
578  * @copydoc equal_range(const K& key)
579  *
580  * Use the hash value `precalculated_hash` instead of hashing the key. The hash value should be the same
581  * as `hash_function()(key)`, otherwise the behaviour is undefined. Useful to speed-up the lookup
582  * if you already have the hash.
583  */
584  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
585  std::pair<iterator, iterator> equal_range(const K& key, std::size_t precalculated_hash) {
586  return m_ht.equal_range(key, precalculated_hash);
587  }
588 
589  /**
590  * @copydoc equal_range(const K& key)
591  */
592  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
593  std::pair<const_iterator, const_iterator> equal_range(const K& key) const { return m_ht.equal_range(key); }
594 
595  /**
596  * @copydoc equal_range(const K& key, std::size_t precalculated_hash)
597  */
598  template<class K, class KE = KeyEqual, typename std::enable_if<has_is_transparent<KE>::value>::type* = nullptr>
599  std::pair<const_iterator, const_iterator> equal_range(const K& key, std::size_t precalculated_hash) const {
600  return m_ht.equal_range(key, precalculated_hash);
601  }
602 
603 
604 
605 
606  /*
607  * Bucket interface
608  */
609  size_type bucket_count() const { return m_ht.bucket_count(); }
610  size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
611 
612 
613  /*
614  * Hash policy
615  */
616  float load_factor() const { return m_ht.load_factor(); }
617  float max_load_factor() const { return m_ht.max_load_factor(); }
618  void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
619 
620  void rehash(size_type count) { m_ht.rehash(count); }
621  void reserve(size_type count) { m_ht.reserve(count); }
622 
623 
624  /*
625  * Observers
626  */
627  hasher hash_function() const { return m_ht.hash_function(); }
628  key_equal key_eq() const { return m_ht.key_eq(); }
629 
630  /*
631  * Other
632  */
633 
634  /**
635  * Convert a `const_iterator` to an `iterator`.
636  */
637  iterator mutable_iterator(const_iterator pos) {
638  return m_ht.mutable_iterator(pos);
639  }
640 
641  /**
642  * Serialize the map through the `serializer` parameter.
643  *
644  * The `serializer` parameter must be a function object that supports the following call:
645  * - `void operator()(const U& value);` where the types `std::uint64_t`, `float` and `std::pair<Key, T>` must be supported for U.
646  *
647  * The implementation leaves binary compatibilty (endianness, IEEE 754 for floats, ...) of the types it serializes
648  * in the hands of the `Serializer` function object if compatibilty is required.
649  */
650  template<class Serializer>
651  void serialize(Serializer& serializer) const {
652  m_ht.serialize(serializer);
653  }
654 
655  /**
656  * Deserialize a previouly serialized map through the `deserializer` parameter.
657  *
658  * The `deserializer` parameter must be a function object that supports the following calls:
659  * - `template<typename U> U operator()();` where the types `std::uint64_t`, `float` and `std::pair<Key, T>` must be supported for U.
660  *
661  * If the deserialized hash map type is hash compatible with the serialized map, the deserialization process can be
662  * sped up by setting `hash_compatible` to true. To be hash compatible, the Hash, KeyEqual and GrowthPolicy must behave the
663  * same way than the ones used on the serialized map. The `std::size_t` must also be of the same size as the one on the platform used
664  * to serialize the map. If these criteria are not met, the behaviour is undefined with `hash_compatible` sets to true, .
665  *
666  * The behaviour is undefined if the type `Key` and `T` of the `sparse_map` are not the same as the
667  * types used during serialization.
668  *
669  * The implementation leaves binary compatibilty (endianness, IEEE 754 for floats, size of int, ...) of the types it
670  * deserializes in the hands of the `Deserializer` function object if compatibilty is required.
671  */
672  template<class Deserializer>
673  static sparse_map deserialize(Deserializer& deserializer, bool hash_compatible = false) {
674  sparse_map map(0);
675  map.m_ht.deserialize(deserializer, hash_compatible);
676 
677  return map;
678  }
679 
680  friend bool operator==(const sparse_map& lhs, const sparse_map& rhs) {
681  if(lhs.size() != rhs.size()) {
682  return false;
683  }
684 
685  for(const auto& element_lhs: lhs) {
686  const auto it_element_rhs = rhs.find(element_lhs.first);
687  if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {
688  return false;
689  }
690  }
691 
692  return true;
693  }
694 
695  friend bool operator!=(const sparse_map& lhs, const sparse_map& rhs) {
696  return !operator==(lhs, rhs);
697  }
698 
699  friend void swap(sparse_map& lhs, sparse_map& rhs) {
700  lhs.swap(rhs);
701  }
702 
703 private:
704  ht m_ht;
705 };
706 
707 
708 /**
709  * Same as `tsl::sparse_map<Key, T, Hash, KeyEqual, Allocator, tsl::sh::prime_growth_policy>`.
710  */
711 template<class Key,
712  class T,
713  class Hash = std::hash<Key>,
714  class KeyEqual = std::equal_to<Key>,
715  class Allocator = std::allocator<std::pair<Key, T>>>
716 using sparse_pg_map = sparse_map<Key, T, Hash, KeyEqual, Allocator, tsl::sh::prime_growth_policy>;
717 
718 } // end namespace tsl
719 
720 #endif
iterator insert_or_assign(const_iterator hint, const key_type &k, M &&obj)
Definition: sparse_map.h:302
sparse_map(InputIt first, InputIt last, size_type bucket_count=ht::DEFAULT_INIT_BUCKETS_SIZE, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: sparse_map.h:165
T & operator[](Key &&key)
Definition: sparse_map.h:457
iterator try_emplace(const_iterator hint, const key_type &k, Args &&... args)
Definition: sparse_map.h:351
iterator find(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:526
std::pair< iterator, bool > try_emplace(const key_type &k, Args &&... args)
Definition: sparse_map.h:341
void serialize(Serializer &serializer) const
Definition: sparse_map.h:651
sparse_map(const Allocator &alloc)
Definition: sparse_map.h:161
allocator_type get_allocator() const
Definition: sparse_map.h:222
sparse_map(std::initializer_list< value_type > init, size_type bucket_count=ht::DEFAULT_INIT_BUCKETS_SIZE, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: sparse_map.h:189
std::pair< iterator, iterator > equal_range(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:585
std::pair< iterator, bool > insert(value_type &&value)
Definition: sparse_map.h:260
std::pair< const_iterator, const_iterator > equal_range(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:565
Definition: sparse_growth_policy.h:39
iterator begin() noexcept
Definition: sparse_map.h:228
std::pair< const_iterator, const_iterator > equal_range(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:599
size_type count(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:469
T & at(const Key &key)
Definition: sparse_map.h:405
iterator find(const Key &key)
Definition: sparse_map.h:493
const T & at(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:420
iterator emplace_hint(const_iterator hint, Args &&... args)
Definition: sparse_map.h:333
friend bool operator!=(const sparse_map &lhs, const sparse_map &rhs)
Definition: sparse_map.h:695
sparse_map(InputIt first, InputIt last, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:182
const T & at(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:451
void insert(std::initializer_list< value_type > ilist)
Definition: sparse_map.h:284
probing
Definition: sparse_hash.h:63
const_iterator find(const K &key) const
Definition: sparse_map.h:532
Definition: sparse_map.h:82
hasher hash_function() const
Definition: sparse_map.h:627
iterator end() noexcept
Definition: sparse_map.h:232
size_type max_bucket_count() const
Definition: sparse_map.h:610
sparse_map(size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:155
sparse_map(size_type bucket_count, const Hash &hash=Hash(), const KeyEqual &equal=KeyEqual(), const Allocator &alloc=Allocator())
Definition: sparse_map.h:142
std::pair< iterator, iterator > equal_range(const K &key)
Definition: sparse_map.h:574
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: sparse_map.h:549
iterator try_emplace(const_iterator hint, key_type &&k, Args &&... args)
Definition: sparse_map.h:356
std::pair< iterator, bool > insert(const value_type &value)
Definition: sparse_map.h:251
float max_load_factor() const
Definition: sparse_map.h:617
const_iterator find(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:542
std::pair< const_iterator, const_iterator > equal_range(const Key &key) const
Definition: sparse_map.h:560
sparsity
Definition: sparse_hash.h:73
T & operator[](const Key &key)
Definition: sparse_map.h:456
exception_safety
Definition: sparse_hash.h:68
size_type count(const K &key) const
Definition: sparse_map.h:478
std::pair< iterator, iterator > equal_range(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:556
friend void swap(sparse_map &lhs, sparse_map &rhs)
Definition: sparse_map.h:699
T & at(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:412
size_type erase(const key_type &key)
Definition: sparse_map.h:366
sparse_map(InputIt first, InputIt last, size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:175
const_iterator find(const Key &key) const
Definition: sparse_map.h:502
void clear() noexcept
Definition: sparse_map.h:247
std::pair< iterator, bool > try_emplace(key_type &&k, Args &&... args)
Definition: sparse_map.h:346
float load_factor() const
Definition: sparse_map.h:616
const_iterator find(const Key &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:507
iterator mutable_iterator(const_iterator pos)
Definition: sparse_map.h:637
sparse_map(std::initializer_list< value_type > init, size_type bucket_count, const Hash &hash, const Allocator &alloc)
Definition: sparse_map.h:205
Definition: sparse_growth_policy.h:40
size_type erase(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:392
size_type bucket_count() const
Definition: sparse_map.h:609
size_type erase(const key_type &key, std::size_t precalculated_hash)
Definition: sparse_map.h:373
iterator insert(const_iterator hint, P &&value)
Definition: sparse_map.h:270
iterator erase(const_iterator pos)
Definition: sparse_map.h:364
void insert(InputIt first, InputIt last)
Definition: sparse_map.h:280
const_iterator cend() const noexcept
Definition: sparse_map.h:234
size_type max_size() const noexcept
Definition: sparse_map.h:242
void rehash(size_type count)
Definition: sparse_map.h:620
iterator insert(const_iterator hint, const value_type &value)
Definition: sparse_map.h:265
void reserve(size_type count)
Definition: sparse_map.h:621
void swap(sparse_map &other)
Definition: sparse_map.h:398
T & at(const K &key, std::size_t precalculated_hash)
Definition: sparse_map.h:438
size_type count(const Key &key) const
Definition: sparse_map.h:462
sparse_map(std::initializer_list< value_type > init, size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:198
iterator erase(iterator pos)
Definition: sparse_map.h:363
size_type size() const noexcept
Definition: sparse_map.h:241
iterator find(const K &key)
Definition: sparse_map.h:516
const_iterator end() const noexcept
Definition: sparse_map.h:233
std::pair< iterator, bool > insert(P &&value)
Definition: sparse_map.h:256
T & at(const K &key)
Definition: sparse_map.h:428
iterator insert_or_assign(const_iterator hint, key_type &&k, M &&obj)
Definition: sparse_map.h:307
Definition: sparse_growth_policy.h:247
const T & at(const Key &key) const
Definition: sparse_map.h:415
friend bool operator==(const sparse_map &lhs, const sparse_map &rhs)
Definition: sparse_map.h:680
sparse_map()
Definition: sparse_map.h:139
std::pair< iterator, bool > insert_or_assign(const key_type &k, M &&obj)
Definition: sparse_map.h:292
size_type count(const K &key, std::size_t precalculated_hash) const
Definition: sparse_map.h:488
size_type erase(const K &key)
Definition: sparse_map.h:382
Definition: sparse_hash.h:193
sparse_map & operator=(std::initializer_list< value_type > ilist)
Definition: sparse_map.h:213
key_equal key_eq() const
Definition: sparse_map.h:628
std::pair< iterator, bool > insert_or_assign(key_type &&k, M &&obj)
Definition: sparse_map.h:297
const_iterator begin() const noexcept
Definition: sparse_map.h:229
std::pair< const_iterator, const_iterator > equal_range(const K &key) const
Definition: sparse_map.h:593
bool empty() const noexcept
Definition: sparse_map.h:240
Definition: sparse_growth_policy.h:49
iterator erase(const_iterator first, const_iterator last)
Definition: sparse_map.h:365
const T & at(const K &key) const
Definition: sparse_map.h:445
sparse_map(size_type bucket_count, const Allocator &alloc)
Definition: sparse_map.h:150
static sparse_map deserialize(Deserializer &deserializer, bool hash_compatible=false)
Definition: sparse_map.h:673
Definition: sparse_hash.h:937
void max_load_factor(float ml)
Definition: sparse_map.h:618
const_iterator cbegin() const noexcept
Definition: sparse_map.h:230
iterator insert(const_iterator hint, value_type &&value)
Definition: sparse_map.h:274
std::pair< iterator, bool > emplace(Args &&... args)
Definition: sparse_map.h:320
iterator find(const Key &key, std::size_t precalculated_hash)
Definition: sparse_map.h:500