45# ifndef TPL_DYNSETHASH_H
46# define TPL_DYNSETHASH_H
50# include <type_traits>
76 template <
typename Key,
77 template <
typename,
class>
class HashTable =
LhashTable,
79 requires EqualityComparator<Cmp, Key>
81 :
public HashTable<Key, Cmp>,
85 public GenericKeys<DynHashTable<Key, HashTable, Cmp>, Key>,
90 using Base = HashTable<Key, Cmp>;
92 using Bucket =
typename HashTable<Key, Cmp>::Bucket;
120 :
Base(len, hash_fct,
cmp, lower_alpha, upper_alpha, true, true)
126 float lower_alpha,
float upper_alpha)
127 :
Base(len, hash_fct,
cmp, lower_alpha, upper_alpha, true, true)
135 for (
typename Base::Iterator it(other); it.has_curr(); it.next_ne())
137 auto *bucket =
static_cast<Bucket *
>(it.get_curr());
138 insert(bucket->get_key());
165 other.lower_alpha, other.upper_alpha, true,
166 other.get_with_resize())
185 requires std::is_nothrow_move_constructible_v<Base>
186 :
Base(std::move(
static_cast<Base &
>(other)))
208 requires (not std::is_nothrow_move_constructible_v<Base>)
209 :
Base(other.len, other.hash_fct, other.get_compare(),
210 other.lower_alpha, other.upper_alpha, true, true)
236 bool contains(
const Key & key)
const noexcept {
return Base::contains(key); }
243 bool is_empty() const noexcept {
return this->Base::is_empty(); }
282 Bucket *ret_val =
nullptr;
285 ret_val =
static_cast<Bucket *
>(this->Base::insert(bucket));
293 if (ret_val ==
nullptr)
299 return &ret_val->get_key();
306 Bucket *ret_val =
nullptr;
309 ret_val =
static_cast<Bucket *
>(this->Base::search_or_insert(bucket));
317 if (ret_val != bucket)
320 return {&ret_val->get_key(),
true};
323 return {&ret_val->get_key(),
false};
361 Key *
add(
const Key & key)
390 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
391 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
396 const Key *
search(
const Key & key)
const noexcept
398 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
399 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
407 bool has(
const Key & key)
const noexcept
409 return this->Base::search(key) !=
nullptr;
412 const Key &
find(
const Key & key)
const
414 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
417 return bucket->get_key();
422 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
426 return bucket->get_key();
453 this->Base::remove(bucket);
459 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
462 this->Base::remove(bucket);
463 auto ret_val = bucket->get_key();
486 return this->Base::Iterator::get_curr_ne()->get_key();
501 return this->Base::Iterator::get_curr()->get_key();
504 void del() {
delete this->Base::Iterator::del(); }
512 return this->
get_it().get_curr();
517 return this->
get_it().get_curr();
524 return it.get_curr();
531 return it.get_curr();
539 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
540 requires EqualityComparator<Cmp, Key>
552 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
565 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
572 template <
typename Key,
typename Data,
573 template <
class,
class>
class HashTable =
LhashTable,
578 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
580 using Pair = std::pair<Key, Data>;
588 using Hash_Fct = std::function<size_t(
const Key &)>;
613 template <
typename K>
615 not std::is_default_constructible_v<Data> or
616 (std::is_nothrow_constructible_v<Key, K>
and
617 std::is_nothrow_default_constructible_v<Data>
and
618 std::is_nothrow_move_constructible_v<Data>);
631 self->search_with_custom_hash(hf,
k);
638 return key_to_pair<Key, Data>(&
const_cast<Key &
>(key))->second;
643 return data_to_pair<Key, Data>(data_ptr)->first;
659 :
Base(len, hash_fct_detail::map_hash_of<Key, Data>(hash_fct),
716 Base::set_hash_fct(hash_fct_detail::map_hash_of<Key, Data>(fct));
736 requires std::is_default_constructible_v<Data>
738 Base::set_hash_fct(std::move(fct));
744 requires std::is_default_constructible_v<Data>
746 Base::set_hash_fct(fct);
767 (
new typename Base::Bucket(
Pair(std::forward<Key>(key), std::forward<Data>(data))));
783 template <
typename K>
784 [[gnu::noinline, gnu::cold]]
810 [[gnu::always_inline]]
811 Pair *
search(
const Key & key)
noexcept(nothrow_search<const Key &>)
815 auto *bucket =
static_cast<Bucket *
>(
817 if (bucket !=
nullptr)
818 return &bucket->get_key();
820 if constexpr (std::is_default_constructible_v<Data>)
830 [[gnu::always_inline]]
831 const Pair *
search(
const Key & key)
const noexcept(nothrow_search<const Key &>)
835 auto *bucket =
static_cast<Bucket *
>(
837 if (bucket !=
nullptr)
838 return &bucket->get_key();
840 if constexpr (std::is_default_constructible_v<Data>)
852 [[gnu::always_inline]]
857 auto *bucket =
static_cast<Bucket *
>(
859 if (bucket !=
nullptr)
860 return &bucket->get_key();
862 if constexpr (std::is_default_constructible_v<Data>)
872 [[gnu::always_inline]]
873 const Pair *
search(Key && key)
const noexcept(nothrow_search<Key>)
877 auto *bucket =
static_cast<Bucket *
>(
879 if (bucket !=
nullptr)
880 return &bucket->get_key();
882 if constexpr (std::is_default_constructible_v<Data>)
897 bool has(
const Key & key)
const noexcept(nothrow_search<const Key &>)
899 return search(key) !=
nullptr;
903 bool has(Key && key)
const noexcept(nothrow_search<Key>)
905 return search(std::forward<Key>(key)) !=
nullptr;
914 bool contains(
const Key & key)
const noexcept(nothrow_search<const Key &>)
920 bool contains(Key && key)
const noexcept(nothrow_search<Key>)
922 return has(std::forward<Key>(key));
940 const Data &
find(
const Key & key)
const
960 return this->
find(key);
970 return this->
find(std::forward<Key>(key));
997 auto ret_val = std::move(
pair->second);
1011 return this->
template maps<Key>([](
const auto & p) ->
const Key & {
return p.first; });
1022 return this->
template maps<Data>([](
const auto & p) ->
const Data & {
return p.second; });
1028 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
1029 ret.
append(&it.get_curr().second);
1036 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
1037 ret.
append(&it.get_curr());
1046 template <
typename Key,
typename Data,
1054 template <
typename Key,
typename Data,
1061 template <
typename T,
template <
typename>
class Container>
1066 c2.for_each([&table](
const T & item)
1070 return table.
keys();
1079 return set1.
filter([&set2](
const T & i) {
return set2.
contains(i); });
1082 template <
typename T,
template <
typename>
class Container>
1089 template <
typename T,
template <
typename>
class Container>
1096 c.for_each([&table, &ret](
const T & i)
1098 auto *ptr = table.
insert(i);
1106 template <
typename T,
template <
typename>
class Container>
1114 c.for_each([&table, &ret, &i](
const T & item)
1116 auto *ptr = table.
insert(item);
1118 ret.
append(std::pair<T, size_t>(item, i));
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Iterator traits and STL-compatible iterator wrappers.
Read-only view of an Aleph iterator: the iterator of a constant container.
static Dnode * data_to_node(T &data) noexcept
Given an reference to the data in the node, returns a pointer to the Dnode object that contains it.
Iterator(const DynHashTable &)=delete
A constant table is iterated through Const_Iterator.
Key & get_curr_ne() noexcept
const Key & get_curr() const
const Key & get_curr_ne() const noexcept
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Self-adjusting dynamic hash table.
Key * add(const Key &key)
Key * search_or_insert(const Key &key)
Key * insert_bucket(Bucket *bucket)
Key * append(const Key &key)
std::pair< Key *, bool > search_or_insert_bucket(Bucket *bucket)
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
typename HashTable< Key, Cmp >::Bucket Bucket
DynHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
Creates a dynamic linear hash table.
void copy(const DynHashTable &other)
DynHashTable(const DynHashTable &other)
Copy constructor.
HashTable< Key, Cmp > Base
void remove(Key *key)
Removes a key from the hash table using its pointer.
const Key & get_last() const
DynHashTable & operator=(const DynHashTable &other)
Copy assignment.
Key * insert(const Key &key)
Inserts key into the hash set.
static Bucket * key_to_bucket(Key *key)
std::pair< Key *, bool > contains_or_insert(Key &&key)
const Key & find(const Key &key) const
Key remove(const Key &key)
typename Base::Hash_Fct Hash_Fct
Hash function type.
DynHashTable(size_t len, Hash_Fct hash_fct, Cmp cmp, float lower_alpha, float upper_alpha)
void clear()
Empties the container.
typename Base::Hash_Fct_Ptr Hash_Fct_Ptr
std::pair< Key *, bool > contains_or_insert(const Key &key)
Key * search(const Key &key) noexcept
Searches for a key in the hash table.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
bool is_empty() const noexcept
Checks if the container is empty.
const Key * search(const Key &key) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Key & get_first() const
DynHashTable(DynHashTable &&other) noexcept
Move constructor over a table that a move of its own takes, as the one of chained lists (DynSetLhash)...
Key & find(const Key &key)
Key * search_or_insert(Key &&key)
DynHashTable(DynHashTable &&other) noexcept
Move constructor over a table that no move of its own takes, as the linear one (DynSetLinHash): build...
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
DynList< Data > values() const
Returns the values of the map.
const Pair * search_by_pair(K &&key) const noexcept(nothrow_search< K >)
Data & operator[](const Key &key)
Subscript operator for map access/insertion.
bool has(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map.
void remove_by_data(Data &data)
Removes from the table the key whose pointer must be the result of a prior insertion or search.
Hash_Fct_Ptr original_hash_fct
size_t(*)(const Key &) Hash_Fct_Ptr
Pair * insert(Key &&key, Data &&data)
static const Key & get_key(Data *data_ptr)
DynList< Data * > values_ptr()
const Data & find(const Key &key) const
Pair * insert(Key &&key, const Data &data)
std::pair< Key, Data > Pair
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
static constexpr bool has_search_with_custom_hash() noexcept
DynMapHashTable(DynMapHashTable &&) noexcept=default
Move constructor: takes the pairs of the other map, which is left empty and keeps its configuration.
DynMapHashTable(const DynMapHashTable &)=default
Copy constructor: the same pairs and the same configuration.
Data remove(const Key &key)
Removes a key-value pair from the map and returns the value.
static size_t no_key_hash(const Key &) noexcept
typename Base::Bucket Bucket
void set_hash_fct(typename Base::Hash_Fct fct)
Sets a hash function of the pairs, as the table takes it.
void swap(DynMapHashTable &other) noexcept
Swaps this map with other in constant time.
typename Base::Iterator Iterator
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
static constexpr bool nothrow_search
DynList< Pair * > items_ptr()
std::function< size_t(const Key &)> Hash_Fct
Pair * search(Key &&key) noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool contains(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< Key > keys() const
Data & find(const Key &key)
Finds and returns a reference to the value associated with key.
static Data & get_data(const Key &key)
bool contains(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map; an alias for has().
void set_hash_fct(typename Base::Hash_Fct_Ptr fct)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool has(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Pair * search(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynMapHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
const Pair * search(const Key &key) const noexcept(nothrow_search< const Key & >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void set_hash_fct(Hash_Fct_Ptr fct)
Sets the hash function of the keys.
Pair * insert(const Key &key, Data &&data)
Common methods to the Aleph-w ( ) containers.
and
Insert a new element into the container by constructing it from the given args.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Common sequential searching methods on containers.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
Equivalence relation constraint for equality comparators.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
void free_bucket_not_taken(Bucket *bucket) noexcept
Frees a bucket that a hash table of lists was given and did not take.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
DynList< std::pair< T, size_t > > repeated_with_index(const Container< T > &c)
Return repeated elements paired with their occurrence count.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
const float hash_default_upper_alpha
std::decay_t< typename HeadC::Item_Type > T
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
const float hash_default_lower_alpha
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Prime number utilities for hash tables and mathematical operations.
Default comparator for pair types in hash maps.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
DynHashTable< Key, LhashTable, Cmp > Base
DynHashTable< Key, LinearHashTable, Cmp > Base
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Generic list of items stored in a container.
Aleph::DynList< T > keys() const
See items().
Generic traversal of the container through its iterator.
Lazy and scalable dynamic array implementation.
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Linear hashing with chaining.