aGrUM 3.1.1
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-2026 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-2026 *
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
65#include <initializer_list>
66#include <string_view>
67
68namespace gum {
69#ifndef DOXYGEN_SHOULD_SKIP_THIS
70
71 // the templates used by this file
72 template < typename Key, typename Val >
73 class HashTable;
74 template < typename Key, typename Val >
75 class HashTableList;
76 template < typename Key, typename Val >
77 class HashTableIterator;
78 template < typename Key, typename Val >
79 class HashTableConstIterator;
80 template < typename Key, typename Val >
81 class HashTableIteratorSafe;
82 template < typename Key, typename Val >
84 template < typename T1, typename T2 >
85 class Bijection;
86
87#endif /* DOXYGEN_SHOULD_SKIP_THIS */
88
102 static constexpr Size default_size{Size(4)};
103
109 static constexpr Size default_mean_val_by_slot{Size(3)};
110
116 static constexpr bool default_resize_policy{true};
117
124 static constexpr bool default_uniqueness_policy{true};
125 };
126
127 // Doxygen raises warning with the following comment bloc
128 // @brief Prints the content of a gum::HashTableList in the stream.
129 // @ingroup hashtable_group
130 // @param s The s used to print the gum::HashTableList.
131 // @param list The gum::HashTableList to print.
132 // @return Returns the std::ostream s.
133 // @tparam Key The type of keys in the gum::HashTableList.
134 // @tparam Val The type of values in the gum::HashTableList.
135
140 template < typename Key, typename Val >
141 std::ostream& operator<<(std::ostream& s, const HashTableList< Key, Val >& list);
142
143 // Doxygen raises warning with the following comment bloc
144 // @brief Prints the content of a gum::HashTableList with pointers key in the
145 // stream.
146 // @ingroup hashtable_group
147 // @param s The s used to print the gum::HashTableList.
148 // @param list The gum::HashTableList to print.
149 // @return Returns the std::ostream s.
150 // @tparam Key The type of keys in the gum::HashTableList.
151 // @tparam Val The type of values in the gum::HashTableList.
152 //
153
159 template < typename Key, typename Val >
160 std::ostream& operator<<(std::ostream& s, const HashTableList< Key*, Val >& list);
161
162 // Doxygen raises warning with the following comment bloc
163 // @brief Prints the content of a gum::HashTable in the stream.
164 // @ingroup hashtable_group
165 // @param s The stream used to print the gum::HashTable.
166 // @param table The gum::HashTable to print.
167 // @return Returns the std::ostream s.
168 // @tparam Key The type of keys in the gum::HashTable.
169 // @tparam Val The type of values in the gum::HashTable.
170
175 template < typename Key, typename Val >
176 std::ostream& operator<<(std::ostream& s, const HashTable< Key, Val >& table);
177
178 // Doxygen raises warning with the following comment bloc
179 // @brief Prints the content of a gum::HashTable with pointers key in the
180 // stream.
181 // @ingroup hashtable_group
182 // @param s The stream used to print the gum::HashTable.
183 // @param table The gum::HashTable to print.
184 // @return Returns the std::ostream s.
185 // @tparam Key The type of keys in the gum::HashTable.
186 // @tparam Val The type of values in the gum::HashTable.
187
193 template < typename Key, typename Val >
194 std::ostream& operator<<(std::ostream& s, const HashTable< Key*, Val >& table);
195
196 // ===========================================================================
197 // === LISTS SPECIFIC FOR SAVING ELEMENTS IN HASHTABLES ===
198 // ===========================================================================
199
215 template < typename Key, typename Val >
218 std::pair< const Key, Val > pair;
219
222
225
230 enum class Emplace { EMPLACE };
231
235 HashTableBucket() = default;
236
242
248 HashTableBucket(const Key& k, const Val& v) : pair{k, v} {}
249
255 HashTableBucket(Key&& k, Val&& v) : pair{std::move(k), std::move(v)} {}
256
261 explicit HashTableBucket(const std::pair< const Key, Val >& p);
262
267 explicit HashTableBucket(std::pair< const Key, Val >&& p);
268
275 template < typename... Args >
276 HashTableBucket(Emplace e, Args&&... args);
277
281 ~HashTableBucket() = default;
282
287 std::pair< const Key, Val >& elt();
288
293 Key& key();
294
299 Val& val();
300 };
301
302 // ===========================================================================
303 // === DOUBLY CHAINED LISTS FOR STORING ELEMENTS IN HASH TABLES ===
304 // ===========================================================================
305
315 template < typename Key, typename Val >
317 public:
320 using key_type = Key;
321 using mapped_type = Val;
322 using value_type = std::pair< const Key, Val >;
326 using const_pointer = const value_type*;
330
331 // ============================================================================
333 // ============================================================================
335
341 HashTableList() noexcept;
342
353 HashTableList(const HashTableList< Key, Val >& from);
354
359 HashTableList(HashTableList< Key, Val >&& from) noexcept;
360
364 ~HashTableList();
365
367 // ============================================================================
369 // ============================================================================
371
388 HashTableList< Key, Val >& operator=(const HashTableList< Key, Val >& from);
389
395 HashTableList< Key, Val >& operator=(HashTableList< Key, Val >&& from) noexcept;
396
398 // ============================================================================
400 // ============================================================================
402
411 value_type& at(Size i);
412
421 const value_type& at(Size i) const;
422
429 mapped_type& operator[](const key_type& key);
430
437 const mapped_type& operator[](const key_type& key) const;
438
446 bool exists(const key_type& key) const;
447
454 void insert(Bucket* new_elt) noexcept;
455
460 void erase(Bucket* ptr);
461
465 void clear();
466
471 bool empty() const noexcept;
472
480 Bucket* bucket(const Key& key) const;
481
483 Bucket* bucket(std::string_view key) const
484 requires std::same_as< Key, std::string >;
485
487
488 private:
491 friend class HashTable< Key, Val >;
492 friend class HashTableIterator< Key, Val >;
493 friend class HashTableConstIterator< Key, Val >;
494 friend class HashTableIteratorSafe< Key, Val >;
495 friend class HashTableConstIteratorSafe< Key, Val >;
496 friend std::ostream& operator<< <>(std::ostream&, const HashTableList< Key, Val >&);
497 friend std::ostream& operator<< <>(std::ostream&, const HashTableList< Key*, Val >&);
498 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key, Val >&);
499 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key*, Val >&);
501
503 HashTableBucket< Key, Val >* _deb_list_{nullptr};
504
507
510
520 void _copy_(const HashTableList< Key, Val >& from);
521 };
522
523 // ===========================================================================
524 // === GENERIC HASH TABLES ===
525 // ===========================================================================
639 template < typename Key, typename Val >
640 class HashTable {
641 public:
644 using key_type = Key;
645 using mapped_type = Val;
646 using value_type = std::pair< const Key, Val >;
650 using const_pointer = const value_type*;
652 using difference_type = std::ptrdiff_t;
658
661
662 // ============================================================================
664 // ============================================================================
666
686 bool key_uniqueness_pol = HashTableConst::default_uniqueness_policy);
687
692 explicit HashTable(std::initializer_list< std::pair< Key, Val > > list);
693
707
713
718
720 // ============================================================================
722 // ============================================================================
724
735 const iterator& end() noexcept;
736
749 const const_iterator& end() const noexcept;
750
763 const const_iterator& cend() const noexcept;
764
778
792
806
816 const iterator_safe& endSafe() noexcept;
817
829 const const_iterator_safe& endSafe() const noexcept;
830
842 const const_iterator_safe& cendSafe() const noexcept;
843
856
869
882
884 // ============================================================================
886 // ============================================================================
888
901 HashTable< Key, Val >& operator=(const HashTable< Key, Val >& from);
902
909 HashTable< Key, Val >& operator=(HashTable< Key, Val >&& from) noexcept;
910
922 Val& operator[](const Key& key);
923
925
928 const Val& operator[](const Key& key) const;
929
932 template < typename K >
933 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
934 && !std::same_as< std::decay_t< K >, std::string >)
935 Val& operator[](const K& key);
936
938 template < typename K >
939 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
940 && !std::same_as< std::decay_t< K >, std::string >)
941 const Val& operator[](const K& key) const;
942
954 bool operator==(const HashTable< Key, Val >& from) const;
955
956
958 // ============================================================================
960 // ============================================================================
962
972 Size capacity() const noexcept;
973
975
992 void resize(Size new_size);
993
1011 void setResizePolicy(const bool new_policy) noexcept;
1012
1017 bool resizePolicy() const noexcept;
1018
1034 void setKeyUniquenessPolicy(const bool new_policy) noexcept;
1035
1040 bool keyUniquenessPolicy() const noexcept;
1041
1043 // ============================================================================
1045 // ============================================================================
1047
1055 Size size() const noexcept;
1056
1067 bool exists(const Key& key) const;
1068
1071 template < typename K >
1072 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
1073 && !std::same_as< std::decay_t< K >, std::string >)
1074 bool exists(const K& key) const;
1075
1085 optional_ref< Val > tryGet(const Key& key);
1086
1088 optional_ref< const Val > tryGet(const Key& key) const;
1089
1092 template < typename K >
1093 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
1094 && !std::same_as< std::decay_t< K >, std::string >)
1095 optional_ref< Val > tryGet(const K& key);
1096
1098 template < typename K >
1099 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
1100 && !std::same_as< std::decay_t< K >, std::string >)
1101 optional_ref< const Val > tryGet(const K& key) const;
1102
1123 value_type& insert(const Key& key, const Val& val);
1124
1143 value_type& insert(Key&& key, Val&& val);
1144
1164 value_type& insert(const std::pair< Key, Val >& elt);
1165
1183 value_type& insert(std::pair< Key, Val >&& elt);
1184
1201 template < typename... Args >
1202 value_type& emplace(Args&&... args);
1203
1218 mapped_type& getWithDefault(const Key& key, const Val& default_value);
1219
1234 mapped_type& getWithDefault(Key&& key, Val&& default_value);
1235
1247 void set(const Key& key, const Val& default_value);
1248
1258 void reset(const Key& key);
1259
1274 void erase(const Key& key);
1275
1278 template < typename K >
1279 requires(std::same_as< Key, std::string > && std::convertible_to< K, std::string_view >
1280 && !std::same_as< std::decay_t< K >, std::string >)
1281 void erase(const K& key);
1282
1293 void erase(const iterator_safe& iter);
1294
1305 void erase(const const_iterator_safe& iter);
1306
1323 void eraseByVal(const Val& val);
1324
1335 const Key& keyByVal(const Val& val) const;
1336
1349 const Key& key(const Key& key) const;
1350
1365 optional_ref< const Key > tryGetKey(const Key& key) const;
1366
1379 void eraseAllVal(const Val& val);
1380
1389 void clear();
1390
1395 bool empty() const noexcept;
1396
1417 template < typename Mount >
1418 HashTable< Key, Mount > map(Mount (*f)(Val),
1419 Size size = Size(0),
1420 bool resize_pol = HashTableConst::default_resize_policy,
1421 bool key_uniqueness_pol
1422 = HashTableConst::default_uniqueness_policy) const;
1423
1444 template < typename Mount >
1445 HashTable< Key, Mount > map(Mount (*f)(Val&),
1446 Size size = Size(0),
1447 bool resize_pol = HashTableConst::default_resize_policy,
1448 bool key_uniqueness_pol
1449 = HashTableConst::default_uniqueness_policy) const;
1450
1471 template < typename Mount >
1472 HashTable< Key, Mount > map(Mount (*f)(const Val&),
1473 Size size = Size(0),
1474 bool resize_pol = HashTableConst::default_resize_policy,
1475 bool key_uniqueness_pol
1476 = HashTableConst::default_uniqueness_policy) const;
1477
1500 template < typename Mount >
1501 HashTable< Key, Mount > map(const Mount& val,
1502 Size size = Size(0),
1503 bool resize_pol = HashTableConst::default_resize_policy,
1504 bool key_uniqueness_pol
1505 = HashTableConst::default_uniqueness_policy) const;
1506
1508
1509 private:
1512 friend class HashTableIterator< Key, Val >;
1513 friend class HashTableConstIterator< Key, Val >;
1514 friend class HashTableIteratorSafe< Key, Val >;
1515 friend class HashTableConstIteratorSafe< Key, Val >;
1516
1517 friend std::ostream& operator<< <>(std::ostream&, const HashTable< Key, Val >&);
1518 friend std::ostream& operator<< <>(std::ostream& s, const HashTable< Key*, Val >& table);
1519
1521 template < typename T1, typename T2 >
1524
1529 std::vector< HashTableList< Key, Val > > _nodes_;
1530
1533
1536
1539
1542
1545
1560 mutable Size _begin_index_{std::numeric_limits< Size >::max()};
1561
1563 mutable std::vector< HashTableConstIteratorSafe< Key, Val >* > _safe_iterators_;
1564
1565
1568
1583 void _copy_(const HashTable< Key, Val >& table);
1584
1590
1595
1612 void _insert_(Bucket* bucket);
1613 };
1614
1615 // ===========================================================================
1616 // === SAFE HASH TABLES CONST ITERATORS ===
1617 // ===========================================================================
1661 template < typename Key, typename Val >
1663 public:
1666 using iterator_category = std::forward_iterator_tag;
1667 using key_type = Key;
1668 using mapped_type = Val;
1669 using value_type = std::pair< const Key, Val >;
1674 using difference_type = std::ptrdiff_t;
1676
1677 // ============================================================================
1679 // ============================================================================
1681
1686
1687#ifndef DOXYGEN_SHOULD_SKIP_THIS
1688 // constructor for the static cendSafe/crendSafe iterator
1689 // only hashTable.cpp should use this constructor
1690 explicit consteval HashTableConstIteratorSafe(StaticInitializer init) noexcept {}
1691#endif // DOXYGEN_SHOULD_SKIP_THIS
1692
1699
1701
1713
1719
1725
1731
1736
1738 // ============================================================================
1740 // ============================================================================
1742
1749 const key_type& key() const;
1750
1752
1758 const mapped_type& val() const;
1759
1767 void clear() noexcept;
1768
1770 // ============================================================================
1772 // ============================================================================
1774
1780 HashTableConstIteratorSafe< Key, Val >&
1781 operator=(const HashTableConstIteratorSafe< Key, Val >& from);
1782
1788 HashTableConstIteratorSafe< Key, Val >&
1789 operator=(const HashTableConstIterator< Key, Val >& from);
1790
1796 HashTableConstIteratorSafe< Key, Val >&
1797 operator=(HashTableConstIteratorSafe< Key, Val >&& from) noexcept;
1798
1814 HashTableConstIteratorSafe< Key, Val >& operator++() noexcept;
1815
1822 HashTableConstIteratorSafe< Key, Val >& operator+=(Size i) noexcept;
1823
1831 HashTableConstIteratorSafe< Key, Val > operator+(Size i) const;
1832
1838 bool operator==(const HashTableConstIteratorSafe< Key, Val >& from) const noexcept;
1839
1846 const value_type& operator*() const;
1847
1849
1850 protected:
1857 friend class HashTable< Key, Val >;
1858
1860 const HashTable< Key, Val >* _table_{nullptr};
1861
1867
1870
1880
1882 [[nodiscard]] HashTableBucket< Key, Val >* _getBucket_() const noexcept;
1883
1890 [[nodiscard]] Size _getIndex_() const noexcept;
1891
1896
1901 };
1902
1903 // ===========================================================================
1904 // === HASH TABLES ITERATORS ===
1905 // ===========================================================================
1906
1953 template < typename Key, typename Val >
1954 class HashTableIteratorSafe: public HashTableConstIteratorSafe< Key, Val > {
1955 public:
1958 using iterator_category = std::forward_iterator_tag;
1959 using key_type = Key;
1960 using mapped_type = Val;
1961 using value_type = std::pair< const Key, Val >;
1962 using reference = value_type&;
1963 using const_reference = const value_type&;
1964 using pointer = value_type*;
1965 using const_pointer = const value_type*;
1966 using difference_type = std::ptrdiff_t;
1968
1969 // ============================================================================
1971 // ============================================================================
1973
1977 explicit HashTableIteratorSafe();
1978
1979#ifndef DOXYGEN_SHOULD_SKIP_THIS
1980 // constructor for the static endSafe/rendSafe iterator
1981 // only hashTable.cpp should use this constructor
1982 explicit consteval HashTableIteratorSafe(StaticInitializer init) noexcept :
1983 HashTableConstIteratorSafe< Key, Val >(init) {}
1984#endif // DOXYGEN_SHOULD_SKIP_THIS
1985
1990 explicit HashTableIteratorSafe(const HashTable< Key, Val >& tab);
1991
2003 HashTableIteratorSafe(const HashTable< Key, Val >& tab, Size ind_elt);
2004
2011
2018
2025
2029 ~HashTableIteratorSafe() noexcept;
2030
2032 // ============================================================================
2034 // ============================================================================
2036
2039 using HashTableConstIteratorSafe< Key, Val >::key;
2040 using HashTableConstIteratorSafe< Key, Val >::val;
2041 using HashTableConstIteratorSafe< Key, Val >::clear;
2043
2050 mapped_type& val();
2051
2053 // ============================================================================
2055 // ============================================================================
2057
2064
2071
2078
2094
2100 HashTableIteratorSafe< Key, Val >& operator+=(Size i) noexcept;
2101
2108 HashTableIteratorSafe< Key, Val > operator+(Size i) const;
2109
2116 bool operator==(const HashTableIteratorSafe< Key, Val >& from) const noexcept;
2117
2124 value_type& operator*();
2125
2133 const value_type& operator*() const;
2134
2136 };
2137
2138 // ===========================================================================
2139 // === UNSAFE HASH TABLES CONST ITERATORS ===
2140 // ===========================================================================
2141
2190 template < typename Key, typename Val >
2192 public:
2195 using iterator_category = std::forward_iterator_tag;
2196 using key_type = Key;
2197 using mapped_type = Val;
2198 using value_type = std::pair< const Key, Val >;
2203 using difference_type = std::ptrdiff_t;
2205
2206 // ============================================================================
2208 // ============================================================================
2210
2214 explicit HashTableConstIterator() noexcept;
2215
2216#ifndef DOXYGEN_SHOULD_SKIP_THIS
2217 // constructor for the static cend/crend iterator
2218 // only hashTable.cpp should use this constructor
2219 explicit consteval HashTableConstIterator(StaticInitializer init) noexcept {}
2220#endif // DOXYGEN_SHOULD_SKIP_THIS
2221
2227 explicit HashTableConstIterator(const HashTable< Key, Val >& tab) noexcept;
2228
2240 HashTableConstIterator(const HashTable< Key, Val >& tab, Size ind_elt);
2241
2247
2253
2257 ~HashTableConstIterator() noexcept;
2258
2260 // ============================================================================
2262 // ============================================================================
2264
2276 [[nodiscard]] const key_type& key() const;
2277
2287 [[nodiscard]] const mapped_type& val() const;
2288
2293 void clear() noexcept;
2294
2296 // ============================================================================
2298 // ============================================================================
2300
2306 HashTableConstIterator< Key, Val >&
2307 operator=(const HashTableConstIterator< Key, Val >& from) noexcept;
2308
2314 HashTableConstIterator< Key, Val >&
2315 operator=(HashTableConstIterator< Key, Val >&& from) noexcept;
2316
2318
2334 HashTableConstIterator< Key, Val >& operator++() noexcept;
2335
2341 HashTableConstIterator< Key, Val >& operator+=(Size i) noexcept;
2342
2350 HashTableConstIterator< Key, Val > operator+(Size i) const noexcept;
2351
2358 bool operator==(const HashTableConstIterator< Key, Val >& from) const noexcept;
2359
2360
2370 const value_type& operator*() const;
2371
2373
2374 protected:
2381 friend class HashTable< Key, Val >;
2382
2384 friend class HashTableConstIteratorSafe< Key, Val >;
2385
2387 const HashTable< Key, Val >* _table_{nullptr};
2388
2393 Size _index_{Size(0)};
2394
2397
2402 [[nodiscard]] typename HashTable< Key, Val >::Bucket* _getBucket_() const noexcept;
2403
2410 [[nodiscard]] Size _getIndex_() const noexcept;
2411 };
2412
2413 // ===========================================================================
2414 // === UNSAFE HASH TABLES ITERATORS ===
2415 // ===========================================================================
2416
2464 template < typename Key, typename Val >
2465 class HashTableIterator: public HashTableConstIterator< Key, Val > {
2466 public:
2469 using iterator_category = std::forward_iterator_tag;
2470 using key_type = Key;
2471 using mapped_type = Val;
2472 using value_type = std::pair< const Key, Val >;
2477 using difference_type = std::ptrdiff_t;
2479
2480 // ############################################################################
2482 // ############################################################################
2484
2488 explicit HashTableIterator() noexcept;
2489
2490#ifndef DOXYGEN_SHOULD_SKIP_THIS
2491 // constructor for the static end/rend iterator
2492 // only hashTable.cpp should use this constructor
2493 explicit consteval HashTableIterator(StaticInitializer init) noexcept :
2495#endif // DOXYGEN_SHOULD_SKIP_THIS
2496
2502 explicit HashTableIterator(const HashTable< Key, Val >& tab) noexcept;
2503
2505
2516 HashTableIterator(const HashTable< Key, Val >& tab, Size ind_elt);
2517
2523
2529
2533 ~HashTableIterator() noexcept;
2534
2536 // ============================================================================
2538 // ============================================================================
2540
2543 using HashTableConstIterator< Key, Val >::key;
2544 using HashTableConstIterator< Key, Val >::val;
2545 using HashTableConstIterator< Key, Val >::clear;
2547
2557 mapped_type& val();
2558
2560 // ============================================================================
2562 // ============================================================================
2564
2570 HashTableIterator< Key, Val >& operator=(const HashTableIterator< Key, Val >& from) noexcept;
2571
2577 HashTableIterator< Key, Val >& operator=(HashTableIterator< Key, Val >&& from) noexcept;
2578
2595 HashTableIterator< Key, Val >& operator++() noexcept;
2596
2602 HashTableIterator< Key, Val >& operator+=(Size i) noexcept;
2603
2609 HashTableIterator< Key, Val > operator+(Size i) const noexcept;
2610
2617 bool operator==(const HashTableIterator< Key, Val >& from) const noexcept;
2618
2628 value_type& operator*();
2629
2639 const value_type& operator*() const;
2640
2642 };
2643
2644
2645#ifndef DOXYGEN_SHOULD_SKIP_THIS
2646 // _static_HashTable_end_ is a 'constant' iterator initialized at compile time
2647 // that represents the end/rend iterators for all hash tables (whatever their
2648 // type). This global variable avoids creating the same iterators within every
2649 // HashTable instance (this would be quite inefficient as end is precisely
2650 // identical for all AVL trees). The same hold for const and safe end iterators.
2651 // The type of _HashTable_end_ is a pointer to void because C++ allows
2652 // pointers to void to be cast into pointers to other types (and conversely).
2653 // This avoids the painful strict-aliasing rule warning
2654 extern const HashTableIterator< int, int > _static_HashTable_end_;
2655 extern const HashTableConstIterator< int, int > _static_HashTable_cend_;
2656 extern const HashTableIteratorSafe< int, int > _static_HashTable_end_safe_;
2657 extern const HashTableConstIteratorSafe< int, int > _static_HashTable_cend_safe_;
2658
2659 inline constexpr void* const _HashTable_end_ = (void* const)&_static_HashTable_end_;
2660 inline constexpr void* const _HashTable_cend_ = (void* const)&_static_HashTable_cend_;
2661 inline constexpr void* const _HashTable_end_safe_ = (void* const)&_static_HashTable_end_safe_;
2662 inline constexpr void* const _HashTable_cend_safe_ = (void* const)&_static_HashTable_cend_safe_;
2663#endif // DOXYGEN_SHOULD_SKIP_THIS
2664} // namespace gum
2665
2666
2667#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
2668extern template class gum::HashTable< int, int >;
2669extern template class gum::HashTable< int, std::string >;
2670extern template class gum::HashTable< std::string, std::string >;
2671extern template class gum::HashTable< std::string, int >;
2672#endif
2673
2674
2675// always include the implementation of the templates
2677
2678#endif // GUM_HASHTABLE_H
Unsafe Const Iterators for hashtables.
Definition hashTable.h:2191
const value_type & const_reference
Types for STL compliance.
Definition hashTable.h:2200
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition hashTable.h:2195
Val mapped_type
Types for STL compliance.
Definition hashTable.h:2197
value_type & reference
Types for STL compliance.
Definition hashTable.h:2199
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:2198
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
Definition hashTable.h:2387
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition hashTable.h:2396
Key key_type
Types for STL compliance.
Definition hashTable.h:2196
value_type * pointer
Types for STL compliance.
Definition hashTable.h:2201
const value_type * const_pointer
Types for STL compliance.
Definition hashTable.h:2202
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table.
Definition hashTable.h:2393
std::ptrdiff_t difference_type
Types for STL compliance.
Definition hashTable.h:2203
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
Safe Iterators for hashtables.
Unsafe Iterators for hashtables.
Definition hashTable.h:2465
Val mapped_type
types for STL compliance
Definition hashTable.h:2471
value_type * pointer
types for STL compliance
Definition hashTable.h:2475
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
std::ptrdiff_t difference_type
types for STL compliance
Definition hashTable.h:2477
const value_type & const_reference
types for STL compliance
Definition hashTable.h:2474
const value_type * const_pointer
types for STL compliance
Definition hashTable.h:2476
std::forward_iterator_tag iterator_category
types for STL compliance
Definition hashTable.h:2469
Key key_type
types for STL compliance
Definition hashTable.h:2470
std::pair< const Key, Val > value_type
types for STL compliance
Definition hashTable.h:2472
value_type & reference
types for STL compliance
Definition hashTable.h:2473
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1640
Safe Const Iterators for hashtables.
Definition hashTable.h:1662
HashTableConstIteratorSafe(const HashTable< Key, Val > &tab, Size ind_elt)
Constructor for an iterator pointing to the nth element of a hashtable.
HashTableBucket< Key, Val > * _getBucket_() const noexcept
Returns the current iterator's bucket.
value_type & reference
Types for STL compliance.
Definition hashTable.h:1670
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
Key key_type
Types for STL compliance.
Definition hashTable.h:1667
HashTableBucket< LeafPair *, std::vector< Size > > * _next_bucket_
Definition hashTable.h:1879
HashTableConstIteratorSafe< Key, Val > & operator++() noexcept
Makes the iterator point to the next element in the hash table.
std::ptrdiff_t difference_type
Types for STL compliance.
Definition hashTable.h:1674
~HashTableConstIteratorSafe() noexcept
Destructor.
std::pair< const Key, Val > value_type
Types for STL compliance.
Definition hashTable.h:1669
HashTableConstIteratorSafe(const HashTable< Key, Val > &tab)
Constructor for an iterator pointing to the first element of a hashtable.
HashTableConstIteratorSafe(const HashTableConstIteratorSafe< Key, Val > &from)
Copy constructor.
HashTableConstIteratorSafe(const HashTableConstIterator< Key, Val > &from)
Copy constructor.
value_type * pointer
Types for STL compliance.
Definition hashTable.h:1672
const HashTable< LeafPair *, std::vector< Size > > * _table_
Definition hashTable.h:1860
HashTableBucket< LeafPair *, std::vector< Size > > * _bucket_
Definition hashTable.h:1869
const value_type & const_reference
Types for STL compliance.
Definition hashTable.h:1671
Val mapped_type
Types for STL compliance.
Definition hashTable.h:1668
const value_type * const_pointer
Types for STL compliance.
Definition hashTable.h:1673
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition hashTable.h:1666
HashTableConstIteratorSafe(HashTableConstIteratorSafe< Key, Val > &&from) noexcept
Move constructor.
HashTableConstIteratorSafe< Key, Val > & operator=(const HashTableConstIteratorSafe< Key, Val > &from)
Copy operator.
A chained list used by gum::HashTable.
Definition hashTable.h:316
Val mapped_type
types for STL compliance
Definition hashTable.h:321
Size size_type
types for STL compliance
Definition hashTable.h:327
void clear()
Removes all the elements of this chained list.
Key key_type
types for STL compliance
Definition hashTable.h:320
bool empty() const noexcept
Returns true if this chained list is empty.
HashTableBucket< Key, Val > Bucket
types for STL compliance
Definition hashTable.h:328
value_type * pointer
types for STL compliance
Definition hashTable.h:325
value_type & at(Size i)
Function at returns the ith element in the current chained list.
value_type & reference
types for STL compliance
Definition hashTable.h:323
const value_type * const_pointer
types for STL compliance
Definition hashTable.h:326
bool exists(const key_type &key) const
Returns true if a value with the given key exists.
void insert(Bucket *new_elt) noexcept
Inserts a new element in the chained list.
HashTableBucket< Key, Val > * _end_list_
A pointer on the last element of the chained list.
Definition hashTable.h:506
const value_type & const_reference
types for STL compliance
Definition hashTable.h:324
void _copy_(const HashTableList< Key, Val > &from)
A function used to perform copies of HashTableLists.
HashTableList() noexcept
Basic constructor that creates an empty list.
HashTableBucket< Key, Val > * _deb_list_
A pointer on the first element of the chained list.
Definition hashTable.h:503
void erase(Bucket *ptr)
Removes an element from this chained list.
std::pair< const Key, Val > value_type
types for STL compliance
Definition hashTable.h:322
Bucket * bucket(const Key &key) const
A method to get the bucket corresponding to a given key.
Size _nb_elements_
The number of elements in the chained list.
Definition hashTable.h:509
The class for generic Hash Tables.
Definition hashTable.h:640
bool resizePolicy() const noexcept
void _create_(Size size)
Used by all default constructors (general and specialized).
iterator_safe beginSafe()
const const_iterator & cend() const noexcept
const T1 & keyByVal(const T2 *&val) const
HashTableIterator< T1, T2 * > iterator
Definition hashTable.h:653
void eraseAllVal(const T2 *&val)
HashFunc< T1 > _hash_func_
Definition hashTable.h:1538
void resize(Size new_size)
HashTable(std::initializer_list< std::pair< Key, Val > > list)
Initializer list constructor.
value_type & insert(const T1 &key, const T2 *&val)
friend class HashTableIteratorSafe< Key, Val >
Definition hashTable.h:1514
Size capacity() const noexcept
std::vector< HashTableConstIteratorSafe< T1, T2 * > * > _safe_iterators_
Definition hashTable.h:1563
void _copy_(const HashTable< Key, Val > &table)
A function used to perform copies of HashTables.
bool empty() const noexcept
const T1 & key(const T1 &key) const
value_type & emplace(Args &&... args)
const value_type * const_pointer
Definition hashTable.h:650
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:646
void set(const T1 &key, const T2 *&default_value)
void setKeyUniquenessPolicy(const bool new_policy) noexcept
void erase(const T1 &key)
friend class HashTableConstIteratorSafe< Key, Val >
Definition hashTable.h:1515
const_iterator_safe cbeginSafe() const
HashTable(const HashTable< Key, Val > &from)
Copy constructor.
~HashTable()
Class destructor.
const iterator_safe & endSafe() noexcept
void reset(const T1 &key)
friend class HashTableConstIterator< Key, Val >
Definition hashTable.h:1513
const const_iterator_safe & cendSafe() const noexcept
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
void eraseByVal(const T2 *&val)
bool exists(const T1 &key) const
Size size() const noexcept
void setResizePolicy(const bool new_policy) noexcept
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
mapped_type & getWithDefault(const T1 &key, const T2 *&default_value)
std::ptrdiff_t difference_type
Definition hashTable.h:652
std::vector< HashTableList< T1, T2 * > > _nodes_
Definition hashTable.h:1529
HashTableConstIteratorSafe< T1, T2 * > const_iterator_safe
Definition hashTable.h:656
HashTableConstIterator< T1, T2 * > const_iterator
Definition hashTable.h:654
const value_type & const_reference
Definition hashTable.h:648
const_iterator cbegin() const
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
HashTableBucket< T1, T2 * > Bucket
Definition hashTable.h:660
void _clearIterators_()
Clear all the safe iterators.
HashTable(HashTable< Key, Val > &&from) noexcept
Move constructor.
optional_ref< const T1 > tryGetKey(const T1 &key) 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.
friend class HashTableIterator< Key, Val >
Definition hashTable.h:1512
bool keyUniquenessPolicy() const noexcept
optional_ref< T2 * > tryGet(const T1 &key)
HashTableIteratorSafe< T1, T2 * > iterator_safe
Definition hashTable.h:655
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
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
STL namespace.
A lightweight optional reference wrapper.
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:216
HashTableBucket< Key, Val > * prev
A pointer toward the previous bucket in the gum::HashTableList.
Definition hashTable.h:221
Key & key()
Returns the key part of the pair.
HashTableBucket(Key &&k, Val &&v)
Constructor.
Definition hashTable.h:255
~HashTableBucket()=default
Class destructor.
Emplace
A dummy type for the emplace constructor.
Definition hashTable.h:230
HashTableBucket(const Key &k, const Val &v)
Constructor.
Definition hashTable.h:248
std::pair< const Key, Val > pair
The pair stored in this bucket.
Definition hashTable.h:218
std::pair< const Key, Val > & elt()
Returns the pair stored in this bucket.
HashTableBucket< Key, Val > * next
A pointer toward the next bucket in the gum::HashTableList.
Definition hashTable.h:224
HashTableBucket()=default
Class constructor.
Val & val()
Returns the value part of the pair.
HashTableBucket(const HashTableBucket< Key, Val > &from)
Copy constructor.
Definition hashTable.h:241
Parameters specifying the default behavior of the hashtables.
Definition hashTable.h:95
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
Definition hashTable.h:109
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:102
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:124
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:116