49#ifndef GUM_HASHTABLE_H
50#define GUM_HASHTABLE_H
64#include <initializer_list>
68#ifndef DOXYGEN_SHOULD_SKIP_THIS
71 template <
typename Key,
typename Val >
73 template <
typename Key,
typename Val >
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 >
139 template <
typename Key,
typename Val >
140 std::ostream&
operator<<(std::ostream& s,
const HashTableList< Key, Val >& list);
158 template <
typename Key,
typename Val >
159 std::ostream&
operator<<(std::ostream& s,
const HashTableList< Key*, Val >& list);
174 template <
typename Key,
typename Val >
175 std::ostream&
operator<<(std::ostream& s,
const HashTable< Key, Val >& table);
192 template <
typename Key,
typename Val >
193 std::ostream&
operator<<(std::ostream& s,
const HashTable< Key*, Val >& table);
214 template <
typename Key,
typename Val >
217 std::pair< const Key, Val >
pair;
274 template <
typename... Args >
277 pair(
std::forward< Args >(args)...) {}
288 std::pair< const Key, Val >&
elt() {
return pair; }
294 Key&
key() {
return const_cast< Key&
>(
pair.first); }
316 template <
typename Key,
typename Val >
472 bool empty() const noexcept;
517 void _copy_(
const HashTableList< Key, Val >& from);
636 template <
typename Key,
typename Val >
689 explicit HashTable(std::initializer_list< std::pair< Key, Val > > list);
919 Val& operator[](const Key&
key);
925 const Val& operator[](const Key&
key) const;
938 bool operator==(const
HashTable< Key, Val >& from) const;
952 bool operator!=(const
HashTable< Key, Val >& from) const;
1164 template < typename... Args >
1210 void set(const Key&
key, const Val& default_value);
1357 template < typename Mount >
1361 bool key_uniqueness_pol
1384 template < typename Mount >
1388 bool key_uniqueness_pol
1411 template < typename Mount >
1415 bool key_uniqueness_pol
1440 template < typename Mount >
1444 bool key_uniqueness_pol
1461 template < typename T1, typename T2 >
1601 template <
typename Key,
typename Val >
1627#ifndef DOXYGEN_SHOULD_SKIP_THIS
1900 template < typename Key, typename Val >
1908 using value_type = std::pair< const Key, Val >;
1926#ifndef DOXYGEN_SHOULD_SKIP_THIS
1930 HashTableConstIteratorSafe< Key, Val >(init) {}
2145 template < typename Key, typename Val >
2171#ifndef DOXYGEN_SHOULD_SKIP_THIS
2248 void clear() noexcept;
2344 friend class HashTable< Key, Val >;
2427 template < typename Key, typename Val >
2453#ifndef DOXYGEN_SHOULD_SKIP_THIS
2616#ifndef DOXYGEN_SHOULD_SKIP_THIS
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_;
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 >;
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.
HashTableConstIteratorSafe(const HashTable< Key, Val > &tab, Size ind_elt)
Constructor for an iterator pointing to the nth element of a hashtable.
const key_type & key() const
value_type & reference
Types for STL compliance.
Key key_type
Types for STL compliance.
Size _getIndex_() const noexcept
const mapped_type & val() const
HashTableBucket< LeafPair *, std::vector< Size > > * _next_bucket_
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.
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.
value_type * pointer
Types for STL compliance.
HashTableConstIteratorSafe()
Basic constructor: creates an iterator pointing to nothing.
HashTableConstIteratorSafe(const HashTableConstIterator< Key, Val > &from)
Copy constructor.
const HashTable< LeafPair *, std::vector< Size > > * _table_
HashTableBucket< LeafPair *, std::vector< Size > > * _bucket_
const value_type & const_reference
Types for STL compliance.
void _removeFromSafeList_() const
~HashTableConstIteratorSafe() noexcept
Destructor.
HashTableConstIteratorSafe(const HashTableConstIteratorSafe< Key, Val > &from)
Copy constructor.
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< Key, Val > & operator=(const HashTableConstIteratorSafe< Key, Val > &from)
Copy operator.
friend class HashTable< Key, Val >
void _insertIntoSafeList_() const
A chained list used by gum::HashTable.
Val mapped_type
types for STL compliance
Size size_type
types for STL compliance
bool exists(const key_type &key) const
Returns true if a value with the given key exists.
Key key_type
types for STL compliance
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
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
value_type & reference
types for STL compliance
const value_type * const_pointer
types for STL compliance
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 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.
HashTableList() noexcept
Basic constructor that creates an empty list.
std::pair< const Key, Val > value_type
types for STL compliance
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.
The class for generic Hash Tables.
void eraseByVal(const T2 *&val)
HashTableIterator< T1, T2 * > iterator
void eraseAllVal(const T2 *&val)
void _create_(Size size)
Used by all default constructors (general and specialized).
HashFunc< T1 > _hash_func_
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 >
std::vector< HashTableConstIteratorSafe< T1, T2 * > * > _safe_iterators_
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
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 _clearIterators_()
Clear all the safe iterators.
mapped_type & getWithDefault(const T1 &key, const T2 *&default_value)
friend class HashTableConstIteratorSafe< Key, Val >
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 >
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
const const_iterator & cend() const noexcept
std::vector< HashTableList< T1, T2 * > > _nodes_
bool keyUniquenessPolicy() const noexcept
HashTableConstIteratorSafe< T1, T2 * > const_iterator_safe
HashTableConstIterator< T1, T2 * > const_iterator
value_type & insert(const T1 &key, const T2 *&val)
const value_type & const_reference
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
Size size() const noexcept
iterator_safe beginSafe()
void reset(const T1 &key)
void setResizePolicy(const bool new_policy) noexcept
bool _key_uniqueness_policy_
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 >
value_type & emplace(Args &&... args)
HashTableIteratorSafe< T1, T2 * > iterator_safe
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
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.
~HashTableBucket()
Class destructor.
Key & key()
Returns the key part of the pair.
HashTableBucket(Key &&k, Val &&v)
Constructor.
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.
HashTableBucket(Emplace e, Args &&... args)
The emplace constructor.
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(const std::pair< const Key, Val > &p)
Constructor.
Val & val()
Returns the value part of the pair.
HashTableBucket(const HashTableBucket< Key, Val > &from)
Copy constructor.
HashTableBucket()
Class constructor.
HashTableBucket(std::pair< const Key, Val > &&p)
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...