49#ifndef GUM_HASHTABLE_H
50#define GUM_HASHTABLE_H
65#include <initializer_list>
69#ifndef DOXYGEN_SHOULD_SKIP_THIS
72 template <
typename Key,
typename Val >
74 template <
typename Key,
typename Val >
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 >
140 template <
typename Key,
typename Val >
141 std::ostream&
operator<<(std::ostream& s,
const HashTableList< Key, Val >& list);
159 template <
typename Key,
typename Val >
160 std::ostream&
operator<<(std::ostream& s,
const HashTableList< Key*, Val >& list);
175 template <
typename Key,
typename Val >
176 std::ostream&
operator<<(std::ostream& s,
const HashTable< Key, Val >& table);
193 template <
typename Key,
typename Val >
194 std::ostream&
operator<<(std::ostream& s,
const HashTable< Key*, Val >& table);
215 template <
typename Key,
typename Val >
218 std::pair< const Key, Val >
pair;
275 template <
typename... Args >
287 std::pair< const Key, Val >&
elt();
315 template <
typename Key,
typename Val >
471 bool empty() const noexcept;
484 requires
std::same_as< Key,
std::
string >;
520 void _copy_(
const HashTableList< Key, Val >& from);
639 template <
typename Key,
typename Val >
692 explicit HashTable(std::initializer_list< std::pair< Key, Val > > list);
922 Val& operator[](const Key&
key);
928 const Val& operator[](const Key&
key) const;
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);
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;
954 bool operator==(const
HashTable< Key, Val >& from) const;
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 >)
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 >)
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 >)
1201 template < typename... Args >
1247 void set(const Key&
key, const Val& default_value);
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 >)
1417 template < typename Mount >
1421 bool key_uniqueness_pol
1444 template < typename Mount >
1448 bool key_uniqueness_pol
1471 template < typename Mount >
1475 bool key_uniqueness_pol
1500 template < typename Mount >
1504 bool key_uniqueness_pol
1521 template < typename T1, typename T2 >
1661 template <
typename Key,
typename Val >
1687#ifndef DOXYGEN_SHOULD_SKIP_THIS
1953 template < typename Key, typename Val >
1961 using value_type = std::pair< const Key, Val >;
1979#ifndef DOXYGEN_SHOULD_SKIP_THIS
1983 HashTableConstIteratorSafe< Key, Val >(init) {}
2190 template < typename Key, typename Val >
2216#ifndef DOXYGEN_SHOULD_SKIP_THIS
2293 void clear() noexcept;
2381 friend class HashTable< Key, Val >;
2410 [[nodiscard]] Size
_getIndex_() const noexcept;
2464 template < typename Key, typename Val >
2490#ifndef DOXYGEN_SHOULD_SKIP_THIS
2645#ifndef DOXYGEN_SHOULD_SKIP_THIS
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_;
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 >;
Unsafe Const Iterators for hashtables.
const value_type & const_reference
Types for STL compliance.
std::forward_iterator_tag iterator_category
Types for STL compliance.
Val mapped_type
Types for STL compliance.
value_type & reference
Types for STL compliance.
std::pair< const Key, Val > value_type
Types for STL compliance.
const HashTable< Key, Val > * _table_
The hash table the iterator is pointing to.
HashTable< Key, Val >::Bucket * _bucket_
The bucket in the chained list pointed to by the iterator.
Key key_type
Types for STL compliance.
value_type * pointer
Types for STL compliance.
const value_type * const_pointer
Types for STL compliance.
Size _index_
The index of the chained list pointed by the iterator in the array of nodes of the hash table.
std::ptrdiff_t difference_type
Types for STL compliance.
HashTableConstIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
Safe Iterators for hashtables.
Unsafe Iterators for hashtables.
Val mapped_type
types for STL compliance
value_type * pointer
types for STL compliance
HashTableIterator() noexcept
Basic constructor: creates an iterator pointing to nothing.
std::ptrdiff_t difference_type
types for STL compliance
const value_type & const_reference
types for STL compliance
const value_type * const_pointer
types for STL compliance
std::forward_iterator_tag iterator_category
types for STL compliance
Key key_type
types for STL compliance
std::pair< const Key, Val > value_type
types for STL compliance
value_type & reference
types for STL compliance
Set of pairs of elements with fast search for both elements.
Safe Const Iterators for hashtables.
const mapped_type & val() const
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.
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
void _insertIntoSafeList_() const
Key key_type
Types for STL compliance.
HashTableBucket< LeafPair *, std::vector< Size > > * _next_bucket_
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.
~HashTableConstIteratorSafe() noexcept
Destructor.
std::pair< const Key, Val > value_type
Types for STL compliance.
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.
const HashTable< LeafPair *, std::vector< Size > > * _table_
void _removeFromSafeList_() const
HashTableBucket< LeafPair *, std::vector< Size > > * _bucket_
const value_type & const_reference
Types for STL compliance.
Size _getIndex_() const noexcept
Val mapped_type
Types for STL compliance.
const value_type * const_pointer
Types for STL compliance.
std::forward_iterator_tag iterator_category
Types for STL compliance.
HashTableConstIteratorSafe(HashTableConstIteratorSafe< Key, Val > &&from) noexcept
Move constructor.
HashTableConstIteratorSafe< Key, Val > & operator=(const HashTableConstIteratorSafe< Key, Val > &from)
Copy operator.
friend class HashTable< Key, Val >
const key_type & key() const
A chained list used by gum::HashTable.
Val mapped_type
types for STL compliance
Size size_type
types for STL compliance
void clear()
Removes all the elements of this chained list.
Key key_type
types for STL compliance
bool empty() const noexcept
Returns true if this chained list is empty.
HashTableBucket< Key, Val > Bucket
types for STL compliance
value_type * pointer
types for STL compliance
value_type & at(Size i)
Function at returns the ith element in the current chained list.
value_type & reference
types for STL compliance
const value_type * const_pointer
types for STL compliance
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.
const value_type & const_reference
types for STL compliance
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.
void erase(Bucket *ptr)
Removes an element from this chained list.
std::pair< const Key, Val > value_type
types for STL compliance
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.
The class for generic Hash Tables.
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
void eraseAllVal(const T2 *&val)
HashFunc< T1 > _hash_func_
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 >
Size capacity() const noexcept
std::vector< HashTableConstIteratorSafe< T1, T2 * > * > _safe_iterators_
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
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
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 >
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 >
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
std::vector< HashTableList< T1, T2 * > > _nodes_
HashTableConstIteratorSafe< T1, T2 * > const_iterator_safe
HashTableConstIterator< T1, T2 * > const_iterator
const value_type & const_reference
const_iterator cbegin() const
void _erase_(HashTableBucket< Key, Val > *bucket, Size index)
Erases a given bucket.
HashTableBucket< T1, T2 * > Bucket
void _clearIterators_()
Clear all the safe iterators.
bool _key_uniqueness_policy_
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 >
bool keyUniquenessPolicy() const noexcept
optional_ref< T2 * > tryGet(const T1 &key)
HashTableIteratorSafe< T1, T2 * > iterator_safe
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.
Classes providing basic hash functions for hash tables.
Implementation of the HashTable.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
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.
HashTableBucket< Key, Val > * prev
A pointer toward the previous bucket in the gum::HashTableList.
Key & key()
Returns the key part of the pair.
HashTableBucket(Key &&k, Val &&v)
Constructor.
~HashTableBucket()=default
Class destructor.
Emplace
A dummy type for the emplace constructor.
HashTableBucket(const Key &k, const Val &v)
Constructor.
std::pair< const Key, Val > pair
The pair stored in this bucket.
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.
HashTableBucket()=default
Class constructor.
Val & val()
Returns the value part of the pair.
HashTableBucket(const HashTableBucket< Key, Val > &from)
Copy constructor.
Parameters specifying the default behavior of the hashtables.
static constexpr Size default_mean_val_by_slot
The average number of elements admissible by slots.
static constexpr Size default_size
The default number of slots in hashtables.
static constexpr bool default_uniqueness_policy
A Boolean indicating the default behavior when trying to insert more than once elements with identica...
static constexpr bool default_resize_policy
A Boolean indicating whether inserting too many values into the hashtable makes it resize itself auto...