aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
hashTable.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_HASHTABLE_H
50#define GUM_HASHTABLE_H
51
52#include <cstddef>
53#include <iostream>
54#include <limits>
55#include <string>
56#include <utility>
57#include <vector>
58
59#include <agrum/agrum.h>
60
63
64#include <initializer_list>
65
66namespace gum {
67
68#ifndef DOXYGEN_SHOULD_SKIP_THIS
69
70 // the templates used by this file
71 template < typename Key, typename Val >
72 class HashTable;
73 template < typename Key, typename Val >
74 class HashTableList;
75 template < typename Key, typename Val >
76 class HashTableIterator;
77 template < typename Key, typename Val >
78 class HashTableConstIterator;
79 template < typename Key, typename Val >
80 class HashTableIteratorSafe;
81 template < typename Key, typename Val >
83 template < typename T1, typename T2 >
84 class Bijection;
85
86#endif /* DOXYGEN_SHOULD_SKIP_THIS */
87
101 static constexpr Size default_size{Size(4)};
102
108 static constexpr Size default_mean_val_by_slot{Size(3)};
109
115 static constexpr bool default_resize_policy{true};
116
123 static constexpr bool default_uniqueness_policy{true};
124 };
125
126 // Doxygen raises warning with the following comment bloc
127 // @brief Prints the content of a gum::HashTableList in the stream.
128 // @ingroup hashtable_group
129 // @param s The s used to print the gum::HashTableList.
130 // @param list The gum::HashTableList to print.
131 // @return Returns the std::ostream s.
132 // @tparam Key The type of keys in the gum::HashTableList.
133 // @tparam Val The type of values in the gum::HashTableList.
134
139 template < typename Key, typename Val >
140 std::ostream& operator<<(std::ostream& s, const HashTableList< Key, Val >& list);
141
142 // Doxygen raises warning with the following comment bloc
143 // @brief Prints the content of a gum::HashTableList with pointers key in the
144 // stream.
145 // @ingroup hashtable_group
146 // @param s The s used to print the gum::HashTableList.
147 // @param list The gum::HashTableList to print.
148 // @return Returns the std::ostream s.
149 // @tparam Key The type of keys in the gum::HashTableList.
150 // @tparam Val The type of values in the gum::HashTableList.
151 //
152
158 template < typename Key, typename Val >
159 std::ostream& operator<<(std::ostream& s, const HashTableList< Key*, Val >& list);
160
161 // Doxygen raises warning with the following comment bloc
162 // @brief Prints the content of a gum::HashTable in the stream.
163 // @ingroup hashtable_group
164 // @param s The stream used to print the gum::HashTable.
165 // @param table The gum::HashTable to print.
166 // @return Returns the std::ostream s.
167 // @tparam Key The type of keys in the gum::HashTable.
168 // @tparam Val The type of values in the gum::HashTable.
169
174 template < typename Key, typename Val >
175 std::ostream& operator<<(std::ostream& s, const HashTable< Key, Val >& table);
176
177 // Doxygen raises warning with the following comment bloc
178 // @brief Prints the content of a gum::HashTable with pointers key in the
179 // stream.
180 // @ingroup hashtable_group
181 // @param s The stream used to print the gum::HashTable.
182 // @param table The gum::HashTable to print.
183 // @return Returns the std::ostream s.
184 // @tparam Key The type of keys in the gum::HashTable.
185 // @tparam Val The type of values in the gum::HashTable.
186
192 template < typename Key, typename Val >
193 std::ostream& operator<<(std::ostream& s, const HashTable< Key*, Val >& table);
194
195 // ===========================================================================
196 // === LISTS SPECIFIC FOR SAVING ELEMENTS IN HASHTABLES ===
197 // ===========================================================================
198
214 template < typename Key, typename Val >
217 std::pair< const Key, Val > pair;
218
221
224
229 enum class Emplace { EMPLACE };
230
235
241
247 HashTableBucket(const Key& k, const Val& v) : pair{k, v} {}
248
254 HashTableBucket(Key&& k, Val&& v) : pair{std::move(k), std::move(v)} {}
255
260 HashTableBucket(const std::pair< const Key, Val >& p) : pair(p) {}
261
266 HashTableBucket(std::pair< const Key, Val >&& p) : pair(std::move(p)) {}
267
274 template < typename... Args >
275 HashTableBucket(Emplace e, Args&&... args) :
276 // emplace (universal) constructor
277 pair(std::forward< Args >(args)...) {}
278
283
288 std::pair< const Key, Val >& elt() { return pair; }
289
294 Key& key() { return const_cast< Key& >(pair.first); }
295
300 Val& val() { return pair.second; }
301 };
302
303 // ===========================================================================
304 // === DOUBLY CHAINED LISTS FOR STORING ELEMENTS IN HASH TABLES ===
305 // ===========================================================================
306
316 template < typename Key, typename Val >
318 public:
321 using key_type = Key;
322 using mapped_type = Val;
323 using value_type = std::pair< const Key, Val >;
327 using const_pointer = const value_type*;
331
332 // ============================================================================
334 // ============================================================================
336
342 HashTableList() noexcept;
343
354 HashTableList(const HashTableList< Key, Val >& from);
355
360 HashTableList(HashTableList< Key, Val >&& from) noexcept;
361
365 ~HashTableList();
366
368 // ============================================================================
370 // ============================================================================
372
389 HashTableList< Key, Val >& operator=(const HashTableList< Key, Val >& from);
390
396 HashTableList< Key, Val >& operator=(HashTableList< Key, Val >&& from) noexcept;
397
399 // ============================================================================
401 // ============================================================================
403
412 value_type& at(Size i);
413
422 const value_type& at(Size i) const;
423
430 mapped_type& operator[](const key_type& key);
431
438 const mapped_type& operator[](const key_type& key) const;
439
447 bool exists(const key_type& key) const;
448
455 void insert(Bucket* new_elt) noexcept;
456
461 void erase(Bucket* ptr);
462
466 void clear();
467
472 bool empty() const noexcept;
473
481 Bucket* bucket(const Key& key) const;
482
484
485 private:
488 friend class HashTable< Key, Val >;
489 friend class HashTableIterator< Key, Val >;
490 friend class HashTableConstIterator< Key, Val >;
491 friend class HashTableIteratorSafe< Key, Val >;
492 friend class HashTableConstIteratorSafe< Key, Val >;
493 friend std::ostream& operator<< <>(std::ostream&, const HashTableList< Key, Val >&);
494 friend std::ostream& operator<< <>(std::ostream&, const HashTableList< Key*, Val >&);
495 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key, Val >&);
496 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key*, Val >&);
498
500 HashTableBucket< Key, Val >* _deb_list_{nullptr};
501
504
507
517 void _copy_(const HashTableList< Key, Val >& from);
518 };
519
520 // ===========================================================================
521 // === GENERIC HASH TABLES ===
522 // ===========================================================================
636 template < typename Key, typename Val >
637 class HashTable {
638 public:
641 using key_type = Key;
642 using mapped_type = Val;
643 using value_type = std::pair< const Key, Val >;
647 using const_pointer = const value_type*;
649 using difference_type = std::ptrdiff_t;
655
658
659 // ============================================================================
661 // ============================================================================
663
683 bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy);
684
689 explicit HashTable(std::initializer_list< std::pair< Key, Val > > list);
690
704
710
715
717 // ============================================================================
719 // ============================================================================
721
732 const iterator& end() noexcept;
733
746 const const_iterator& end() const noexcept;
747
760 const const_iterator& cend() const noexcept;
761
775
789
803
813 const iterator_safe& endSafe() noexcept;
814
826 const const_iterator_safe& endSafe() const noexcept;
827
839 const const_iterator_safe& cendSafe() const noexcept;
840
853
866
879
881 // ============================================================================
883 // ============================================================================
885
898 HashTable< Key, Val >& operator=(const HashTable< Key, Val >& from);
899
906 HashTable< Key, Val >& operator=(HashTable< Key, Val >&& from);
907
919 Val& operator[](const Key& key);
920
922
925 const Val& operator[](const Key& key) const;
926
938 bool operator==(const HashTable< Key, Val >& from) const;
939
941
952 bool operator!=(const HashTable< Key, Val >& from) const;
953
955 // ============================================================================
957 // ============================================================================
959
969 Size capacity() const noexcept;
970
972
989 void resize(Size new_size);
990
1008 void setResizePolicy(const bool new_policy) noexcept;
1009
1014 bool resizePolicy() const noexcept;
1015
1031 void setKeyUniquenessPolicy(const bool new_policy) noexcept;
1032
1037 bool keyUniquenessPolicy() const noexcept;
1038
1040 // ============================================================================
1042 // ============================================================================
1044
1052 Size size() const noexcept;
1053
1064 bool exists(const Key& key) const;
1065
1086 value_type& insert(const Key& key, const Val& val);
1087
1106 value_type& insert(Key&& key, Val&& val);
1107
1127 value_type& insert(const std::pair< Key, Val >& elt);
1128
1146 value_type& insert(std::pair< Key, Val >&& elt);
1147
1164 template < typename... Args >
1165 value_type& emplace(Args&&... args);
1166
1181 mapped_type& getWithDefault(const Key& key, const Val& default_value);
1182
1197 mapped_type& getWithDefault(Key&& key, Val&& default_value);
1198
1210 void set(const Key& key, const Val& default_value);
1211
1221 void reset(const Key& key);
1222
1237 void erase(const Key& key);
1238
1249 void erase(const iterator_safe& iter);
1250
1261 void erase(const const_iterator_safe& iter);
1262
1279 void eraseByVal(const Val& val);
1280
1291 const Key& keyByVal(const Val& val) const;
1292
1305 const Key& key(const Key& key) const;
1306
1319 void eraseAllVal(const Val& val);
1320
1329 void clear();
1330
1335 bool empty() const noexcept;
1336
1357 template < typename Mount >
1358 HashTable< Key, Mount > map(Mount (*f)(Val),
1359 Size size = Size(0),
1360 bool resize_pol = HashTableConst::default_resize_policy,
1361 bool key_uniqueness_pol
1362 = HashTableConst::default_uniqueness_policy) const;
1363
1384 template < typename Mount >
1385 HashTable< Key, Mount > map(Mount (*f)(Val&),
1386 Size size = Size(0),
1387 bool resize_pol = HashTableConst::default_resize_policy,
1388 bool key_uniqueness_pol
1389 = HashTableConst::default_uniqueness_policy) const;
1390
1411 template < typename Mount >
1412 HashTable< Key, Mount > map(Mount (*f)(const Val&),
1413 Size size = Size(0),
1414 bool resize_pol = HashTableConst::default_resize_policy,
1415 bool key_uniqueness_pol
1416 = HashTableConst::default_uniqueness_policy) const;
1417
1440 template < typename Mount >
1441 HashTable< Key, Mount > map(const Mount& val,
1442 Size size = Size(0),
1443 bool resize_pol = HashTableConst::default_resize_policy,
1444 bool key_uniqueness_pol
1445 = HashTableConst::default_uniqueness_policy) const;
1446
1448
1449 private:
1452 friend class HashTableIterator< Key, Val >;
1453 friend class HashTableConstIterator< Key, Val >;
1454 friend class HashTableIteratorSafe< Key, Val >;
1455 friend class HashTableConstIteratorSafe< Key, Val >;
1456
1457 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key, Val >&);
1458 friend std::ostream& operator<< <>(std::ostream& s, const HashTable< Key*, Val >& table);
1459
1461 template < typename T1, typename T2 >
1464
1469 std::vector< HashTableList< Key, Val > > _nodes_;
1470
1473
1476
1479
1482
1485
1500 mutable Size _begin_index_{std::numeric_limits< Size >::max()};
1501
1503 mutable std::vector< HashTableConstIteratorSafe< Key, Val >* > _safe_iterators_;
1504
1505
1508
1523 void _copy_(const HashTable< Key, Val >& table);
1524
1530
1535
1552 void _insert_(Bucket* bucket);
1553 };
1554
1555 // ===========================================================================
1556 // === SAFE HASH TABLES CONST ITERATORS ===
1557 // ===========================================================================
1601 template < typename Key, typename Val >
1603 public:
1606 using iterator_category = std::forward_iterator_tag;
1607 using key_type = Key;
1608 using mapped_type = Val;
1609 using value_type = std::pair< const Key, Val >;
1614 using difference_type = std::ptrdiff_t;
1616
1617 // ============================================================================
1619 // ============================================================================
1621
1626
1627#ifndef DOXYGEN_SHOULD_SKIP_THIS
1628 // constructor for the static cendSafe/crendSafe iterator
1629 // only hashTable.cpp should use this constructor
1630 explicit consteval HashTableConstIteratorSafe(StaticInitializer init) noexcept {}
1631#endif // DOXYGEN_SHOULD_SKIP_THIS
1632
1639
1641
1653
1659
1665
1671
1676
1678 // ============================================================================
1680 // ============================================================================
1682
1689 const key_type& key() const;
1690
1692
1698 const mapped_type& val() const;
1699
1707 void clear() noexcept;
1708
1710 // ============================================================================
1712 // ============================================================================
1714
1720 HashTableConstIteratorSafe< Key, Val >&
1721 operator=(const HashTableConstIteratorSafe< Key, Val >& from);
1722
1728 HashTableConstIteratorSafe< Key, Val >&
1729 operator=(const HashTableConstIterator< Key, Val >& from);
1730
1736 HashTableConstIteratorSafe< Key, Val >&
1737 operator=(HashTableConstIteratorSafe< Key, Val >&& from) noexcept;
1738
1754 HashTableConstIteratorSafe< Key, Val >& operator++() noexcept;
1755
1762 HashTableConstIteratorSafe< Key, Val >& operator+=(Size i) noexcept;
1763
1771 HashTableConstIteratorSafe< Key, Val > operator+(Size i) const;
1772
1778 bool operator!=(const HashTableConstIteratorSafe< Key, Val >& from) const noexcept;
1779
1785 bool operator==(const HashTableConstIteratorSafe< Key, Val >& from) const noexcept;
1786
1793 const value_type& operator*() const;
1794
1796
1797 protected:
1804 friend class HashTable< Key, Val >;
1805
1807 const HashTable< Key, Val >* _table_{nullptr};
1808
1814
1817
1827
1830
1837 Size _getIndex_() const noexcept;
1838
1843
1848 };
1849
1850 // ===========================================================================
1851 // === HASH TABLES ITERATORS ===
1852 // ===========================================================================
1853
1900 template < typename Key, typename Val >
1901 class HashTableIteratorSafe: public HashTableConstIteratorSafe< Key, Val > {
1902 public:
1905 using iterator_category = std::forward_iterator_tag;
1906 using key_type = Key;
1907 using mapped_type = Val;
1908 using value_type = std::pair< const Key, Val >;
1909 using reference = value_type&;
1910 using const_reference = const value_type&;
1911 using pointer = value_type*;
1912 using const_pointer = const value_type*;
1913 using difference_type = std::ptrdiff_t;
1915
1916 // ============================================================================
1918 // ============================================================================
1920
1924 explicit HashTableIteratorSafe();
1925
1926#ifndef DOXYGEN_SHOULD_SKIP_THIS
1927 // constructor for the static endSafe/rendSafe iterator
1928 // only hashTable.cpp should use this constructor
1929 explicit consteval HashTableIteratorSafe(StaticInitializer init) noexcept :
1930 HashTableConstIteratorSafe< Key, Val >(init) {}
1931#endif // DOXYGEN_SHOULD_SKIP_THIS
1932
1938
1950 HashTableIteratorSafe(const HashTable< Key, Val >& tab, Size ind_elt);
1951
1958
1965
1972
1976 ~HashTableIteratorSafe() noexcept;
1977
1979 // ============================================================================
1981 // ============================================================================
1983
1986 using HashTableConstIteratorSafe< Key, Val >::key;
1987 using HashTableConstIteratorSafe< Key, Val >::val;
1988 using HashTableConstIteratorSafe< Key, Val >::clear;
1990
1997 mapped_type& val();
1998
2000 // ============================================================================
2002 // ============================================================================
2004
2011
2018
2025
2041
2047 HashTableIteratorSafe< Key, Val >& operator+=(Size i) noexcept;
2048
2055 HashTableIteratorSafe< Key, Val > operator+(Size i) const;
2056
2063 bool operator!=(const HashTableIteratorSafe< Key, Val >& from) const noexcept;
2064
2071 bool operator==(const HashTableIteratorSafe< Key, Val >& from) const noexcept;
2072
2079 value_type& operator*();
2080
2088 const value_type& operator*() const;
2089
2091 };
2092
2093 // ===========================================================================
2094 // === UNSAFE HASH TABLES CONST ITERATORS ===
2095 // ===========================================================================
2096
2145 template < typename Key, typename Val >
2147 public:
2150 using iterator_category = std::forward_iterator_tag;
2151 using key_type = Key;
2152 using mapped_type = Val;
2153 using value_type = std::pair< const Key, Val >;
2158 using difference_type = std::ptrdiff_t;
2160
2161 // ============================================================================
2163 // ============================================================================
2165
2169 explicit HashTableConstIterator() noexcept;
2170
2171#ifndef DOXYGEN_SHOULD_SKIP_THIS
2172 // constructor for the static cend/crend iterator
2173 // only hashTable.cpp should use this constructor
2174 explicit consteval HashTableConstIterator(StaticInitializer init) noexcept {}
2175#endif // DOXYGEN_SHOULD_SKIP_THIS
2176
2182 HashTableConstIterator(const HashTable< Key, Val >& tab) noexcept;
2183
2195 HashTableConstIterator(const HashTable< Key, Val >& tab, Size ind_elt);
2196
2202
2208
2212 ~HashTableConstIterator() noexcept;
2213
2215 // ============================================================================
2217 // ============================================================================
2219
2231 const key_type& key() const;
2232
2242 const mapped_type& val() const;
2243
2248 void clear() noexcept;
2249
2251 // ============================================================================
2253 // ============================================================================
2255
2261 HashTableConstIterator< Key, Val >&
2262 operator=(const HashTableConstIterator< Key, Val >& from) noexcept;
2263
2269 HashTableConstIterator< Key, Val >&
2270 operator=(HashTableConstIterator< Key, Val >&& from) noexcept;
2271
2273
2289 HashTableConstIterator< Key, Val >& operator++() noexcept;
2290
2296 HashTableConstIterator< Key, Val >& operator+=(Size i) noexcept;
2297
2305 HashTableConstIterator< Key, Val > operator+(Size i) const noexcept;
2306
2313 bool operator!=(const HashTableConstIterator< Key, Val >& from) const noexcept;
2314
2321 bool operator==(const HashTableConstIterator< Key, Val >& from) const noexcept;
2322
2323
2333 const value_type& operator*() const;
2334
2336
2337 protected:
2344 friend class HashTable< Key, Val >;
2345
2347 friend class HashTableConstIteratorSafe< Key, Val >;
2348
2350 const HashTable< Key, Val >* _table_{nullptr};
2351
2356 Size _index_{Size(0)};
2357
2360
2365 typename HashTable< Key, Val >::Bucket* _getBucket_() const noexcept;
2366
2373 Size _getIndex_() const noexcept;
2374 };
2375
2376 // ===========================================================================
2377 // === UNSAFE HASH TABLES ITERATORS ===
2378 // ===========================================================================
2379
2427 template < typename Key, typename Val >
2428 class HashTableIterator: public HashTableConstIterator< Key, Val > {
2429 public:
2432 using iterator_category = std::forward_iterator_tag;
2433 using key_type = Key;
2434 using mapped_type = Val;
2435 using value_type = std::pair< const Key, Val >;
2440 using difference_type = std::ptrdiff_t;
2442
2443 // ############################################################################
2445 // ############################################################################
2447
2451 explicit HashTableIterator() noexcept;
2452
2453#ifndef DOXYGEN_SHOULD_SKIP_THIS
2454 // constructor for the static end/rend iterator
2455 // only hashTable.cpp should use this constructor
2456 explicit consteval HashTableIterator(StaticInitializer init) noexcept :
2458#endif // DOXYGEN_SHOULD_SKIP_THIS
2459
2465 HashTableIterator(const HashTable< Key, Val >& tab) noexcept;
2466
2468
2479 HashTableIterator(const HashTable< Key, Val >& tab, Size ind_elt);
2480
2486
2492
2496 ~HashTableIterator() noexcept;
2497
2499 // ============================================================================
2501 // ============================================================================
2503
2506 using HashTableConstIterator< Key, Val >::key;
2507 using HashTableConstIterator< Key, Val >::val;
2508 using HashTableConstIterator< Key, Val >::clear;
2510
2520 mapped_type& val();
2521
2523 // ============================================================================
2525 // ============================================================================
2527
2533 HashTableIterator< Key, Val >& operator=(const HashTableIterator< Key, Val >& from) noexcept;
2534
2540 HashTableIterator< Key, Val >& operator=(HashTableIterator< Key, Val >&& from) noexcept;
2541
2558 HashTableIterator< Key, Val >& operator++() noexcept;
2559
2565 HashTableIterator< Key, Val >& operator+=(Size i) noexcept;
2566
2572 HashTableIterator< Key, Val > operator+(Size i) const noexcept;
2573
2580 bool operator!=(const HashTableIterator< Key, Val >& from) const noexcept;
2581
2588 bool operator==(const HashTableIterator< Key, Val >& from) const noexcept;
2589
2599 value_type& operator*();
2600
2610 const value_type& operator*() const;
2611
2613 };
2614
2615
2616#ifndef DOXYGEN_SHOULD_SKIP_THIS
2617 // _static_HashTable_end_ is a 'constant' iterator initialized at compile time
2618 // that represents the end/rend iterators for all hash tables (whatever their
2619 // type). This global variable avoids creating the same iterators within every
2620 // HashTable instance (this would be quite inefficient as end is precisely
2621 // identical for all AVL trees). The same hold for const and safe end iterators.
2622 // The type of _HashTable_end_ is a pointer to void because C++ allows
2623 // pointers to void to be cast into pointers to other types (and conversely).
2624 // This avoids the painful strict-aliasing rule warning
2625 extern const HashTableIterator< int, int > _static_HashTable_end_;
2626 extern const HashTableConstIterator< int, int > _static_HashTable_cend_;
2627 extern const HashTableIteratorSafe< int, int > _static_HashTable_end_safe_;
2628 extern const HashTableConstIteratorSafe< int, int > _static_HashTable_cend_safe_;
2629
2630 inline constexpr void* const _HashTable_end_ = (void* const)&_static_HashTable_end_;
2631 inline constexpr void* const _HashTable_cend_ = (void* const)&_static_HashTable_cend_;
2632 inline constexpr void* const _HashTable_end_safe_ = (void* const)&_static_HashTable_end_safe_;
2633 inline constexpr void* const _HashTable_cend_safe_ = (void* const)&_static_HashTable_cend_safe_;
2634#endif // DOXYGEN_SHOULD_SKIP_THIS
2635
2636} // namespace gum
2637
2638
2639#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
2640extern template class gum::HashTable< int, int >;
2641extern template class gum::HashTable< int, std::string >;
2642extern template class gum::HashTable< std::string, std::string >;
2643extern template class gum::HashTable< std::string, int >;
2644#endif
2645
2646
2647// always include the implementation of the templates
2649
2650#endif // GUM_HASHTABLE_H
Unsafe Const Iterators for hashtables.
Definition hashTable.h:2146
const value_type & const_reference
Types for STL compliance.
Definition hashTable.h:2155
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition hashTable.h:2150
Val mapped_type
Types for STL compliance.
Definition hashTable.h:2152
value_type & reference
Types for STL compliance.
Definition hashTable.h:2154
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:2153
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition hashTable.h:2350
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition hashTable.h:2359
Key key_type
Types for STL compliance.
Definition hashTable.h:2151
value_type * pointer
Types for STL compliance.
Definition hashTable.h:2156
const value_type * const_pointer
Types for STL compliance.
Definition hashTable.h:2157
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table.
Definition hashTable.h:2356
std::ptrdiff_t difference_type
Types for STL compliance.
Definition hashTable.h:2158
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
Safe Iterators for hashtables.
Unsafe Iterators for hashtables.
Definition hashTable.h:2428
Val mapped_type
types for STL compliance
Definition hashTable.h:2434
value_type * pointer
types for STL compliance
Definition hashTable.h:2438
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
std::ptrdiff_t difference_type
types for STL compliance
Definition hashTable.h:2440
const value_type & const_reference
types for STL compliance
Definition hashTable.h:2437
const value_type * const_pointer
types for STL compliance
Definition hashTable.h:2439
std::forward_iterator_tag iterator_category
types for STL compliance
Definition hashTable.h:2432
Key key_type
types for STL compliance
Definition hashTable.h:2433
std::pair< const Key, Val > value_type
types for STL compliance
Definition hashTable.h:2435
value_type & reference
types for STL compliance
Definition hashTable.h:2436
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Safe Const Iterators for hashtables.
Definition hashTable.h:1602
HashTableConstIteratorSafe(const HashTable< Key, Val > &tab, Size ind_elt)
Constructor for an iterator pointing to the nth element of a hashtable.
value_type & reference
Types for STL compliance.
Definition hashTable.h:1610
Key key_type
Types for STL compliance.
Definition hashTable.h:1607
HashTableBucket< LeafPair *, std::vector< Size > > * _next_bucket_
Definition hashTable.h:1826
HashTableConstIteratorSafe< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
HashTableConstIteratorSafe(HashTableConstIteratorSafe< Key, Val > &&from)
Move constructor.
HashTableBucket< Key, Val > * _getBucket_() const noexcept
Returns the current iterator's bucket.
std::ptrdiff_t difference_type
Types for STL compliance.
Definition hashTable.h:1614
HashTableConstIteratorSafe(const HashTable< Key, Val > &tab)
Constructor for an iterator pointing to the first element of a hashtable.
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:1609
value_type * pointer
Types for STL compliance.
Definition hashTable.h:1612
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
HashTableConstIteratorSafe(const HashTableConstIterator< Key, Val > &from)
Copy constructor.
const HashTable< LeafPair *, std::vector< Size > > * _table_
Definition hashTable.h:1807
HashTableBucket< LeafPair *, std::vector< Size > > * _bucket_
Definition hashTable.h:1816
const value_type & const_reference
Types for STL compliance.
Definition hashTable.h:1611
~HashTableConstIteratorSafe() noexcept
Destructor.
HashTableConstIteratorSafe(const HashTableConstIteratorSafe< Key, Val > &from)
Copy constructor.
Val mapped_type
Types for STL compliance.
Definition hashTable.h:1608
const value_type * const_pointer
Types for STL compliance.
Definition hashTable.h:1613
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition hashTable.h:1606
HashTableConstIteratorSafe< Key, Val > & operator=(const HashTableConstIteratorSafe< Key, Val > &from)
Copy operator.
A chained list used by gum::HashTable.
Definition hashTable.h:317
Val mapped_type
types for STL compliance
Definition hashTable.h:322
Size size_type
types for STL compliance
Definition hashTable.h:328
bool exists(const key_type &key) const
Returns true if a value with the given key exists.
Key key_type
types for STL compliance
Definition hashTable.h:321
Bucket * bucket(const Key &key) const
A method to get the bucket corresponding to a given key.
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition hashTable.h:329
void erase(Bucket *ptr)
Removes an element from this chained list.
bool empty() const noexcept
Returns true if this chained list is empty.
value_type * pointer
types for STL compliance
Definition hashTable.h:326
value_type & reference
types for STL compliance
Definition hashTable.h:324
const value_type * const_pointer
types for STL compliance
Definition hashTable.h:327
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition hashTable.h:503
const value_type & const_reference
types for STL compliance
Definition hashTable.h:325
void clear()
Removes all the elements of this chained list.
value_type & at(Size i)
Function at returns the ith element in the current chained list.
void _copy_(const HashTableList< Key, Val > &from)
A function used to perform copies of HashTableLists.
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition hashTable.h:500
HashTableList() noexcept
Basic constructor that creates an empty list.
std::pair< const Key, Val > value_type
types for STL compliance
Definition hashTable.h:323
void insert(Bucket *new_elt) noexcept
Inserts a new element in the chained list.
Size _nb_elements_
The number of elements in the chained list.
Definition hashTable.h:506
The class for generic Hash Tables.
Definition hashTable.h:637
void eraseByVal(const T2 *&val)
HashTableIterator< T1, T2 * > iterator
Definition hashTable.h:650
void eraseAllVal(const T2 *&val)
void _create_(Size size)
Used by all default constructors (general and specialized).
HashFunc< T1 > _hash_func_
Definition hashTable.h:1478
void resize(Size new_size)
HashTable(std::initializer_list< std::pair< Key, Val > > list)
Initializer list constructor.
HashTable(HashTable< Key, Val > &&from)
Move constructor.
friend class HashTableIteratorSafe< Key, Val >
Definition hashTable.h:1454
std::vector< HashTableConstIteratorSafe< T1, T2 * > * > _safe_iterators_
Definition hashTable.h:1503
void _copy_(const HashTable< Key, Val > &table)
A function used to perform copies of HashTables.
void setKeyUniquenessPolicy(const bool new_policy) noexcept
const value_type * const_pointer
Definition hashTable.h:647
void _insert_(Bucket *bucket)
Adds a new element (actually a copy of this element) in the hash table.
std::pair< const T1, T2 * > value_type
Definition hashTable.h:643
void _clearIterators_()
Clear all the safe iterators.
mapped_type & getWithDefault(const T1 &key, const T2 *&default_value)
friend class HashTableConstIteratorSafe< Key, Val >
Definition hashTable.h:1455
HashTable(const HashTable< Key, Val > &from)
Copy constructor.
const iterator_safe & endSafe() noexcept
const const_iterator_safe & cendSafe() const noexcept
void erase(const T1 &key)
bool exists(const T1 &key) const
~HashTable()
Class destructor.
const_iterator cbegin() const
const T1 & key(const T1 &key) const
bool empty() const noexcept
bool resizePolicy() const noexcept
friend class HashTableConstIterator< Key, Val >
Definition hashTable.h:1453
HashTable< T1, Mount > map(Mount(*f)(T2 *), Size size=Size(0), bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy) const
std::ptrdiff_t difference_type
Definition hashTable.h:649
const const_iterator & cend() const noexcept
std::vector< HashTableList< T1, T2 * > > _nodes_
Definition hashTable.h:1469
bool keyUniquenessPolicy() const noexcept
HashTableConstIteratorSafe< T1, T2 * > const_iterator_safe
Definition hashTable.h:653
HashTableConstIterator< T1, T2 * > const_iterator
Definition hashTable.h:651
value_type & insert(const T1 &key, const T2 *&val)
const value_type & const_reference
Definition hashTable.h:645
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
Size capacity() const noexcept
HashTableBucket< T1, T2 * > Bucket
Definition hashTable.h:657
Size size() const noexcept
iterator_safe beginSafe()
void reset(const T1 &key)
void setResizePolicy(const bool new_policy) noexcept
void set(const T1 &key, const T2 *&default_value)
const T1 & keyByVal(const T2 *&val) const
HashTable(Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
Default constructor.
const_iterator_safe cbeginSafe() const
friend class HashTableIterator< Key, Val >
Definition hashTable.h:1452
value_type & emplace(Args &&... args)
HashTableIteratorSafe< T1, T2 * > iterator_safe
Definition hashTable.h:652
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Classes providing basic hash functions for hash tables.
Implementation of the HashTable.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913
STL namespace.
Data types to enable creating static variables at compile time.
A recipient for a pair of key value in a gum::HashTableList.
Definition hashTable.h:215
HashTableBucket< Key, Val > * prev
A pointer toward the previous bucket in the gum::HashTableList.
Definition hashTable.h:220
~HashTableBucket()
Class destructor.
Definition hashTable.h:282
Key & key()
Returns the key part of the pair.
Definition hashTable.h:294
HashTableBucket(Key &&k, Val &&v)
Constructor.
Definition hashTable.h:254
Emplace
A dummy type for the emplace constructor.
Definition hashTable.h:229
HashTableBucket(const Key &k, const Val &v)
Constructor.
Definition hashTable.h:247
std::pair< const Key, Val > pair
The pair stored in this bucket.
Definition hashTable.h:217
HashTableBucket(Emplace e, Args &&... args)
The emplace constructor.
Definition hashTable.h:275
std::pair< const Key, Val > & elt()
Returns the pair stored in this bucket.
Definition hashTable.h:288
HashTableBucket< Key, Val > * next
A pointer toward the next bucket in the gum::HashTableList.
Definition hashTable.h:223
HashTableBucket(const std::pair< const Key, Val > &p)
Constructor.
Definition hashTable.h:260
Val & val()
Returns the value part of the pair.
Definition hashTable.h:300
HashTableBucket(const HashTableBucket< Key, Val > &from)
Copy constructor.
Definition hashTable.h:240
HashTableBucket()
Class constructor.
Definition hashTable.h:234
HashTableBucket(std::pair< const Key, Val > &&p)
Constructor.
Definition hashTable.h:266
Parameters specifying the default behavior of the hashtables.
Definition hashTable.h:94
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition hashTable.h:108
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:101
static constexpr bool default_uniqueness_policy
A Boolean indicating the default behavior when trying to insert more than once elements with identica...
Definition hashTable.h:123
static constexpr bool default_resize_policy
A Boolean indicating whether inserting too many values into the hashtable makes it resize itself auto...
Definition hashTable.h:115