45# ifndef TPL_DYNSETHASH_H
46# define TPL_DYNSETHASH_H
74 template <
typename Key,
83 public GenericKeys<DynHashTable<Key, HashTable, Cmp>, Key>,
90 using Bucket =
typename HashTable<Key, Cmp>::Bucket;
124 float lower_alpha,
float upper_alpha)
133 for (
typename Base::Iterator it(
other); it.has_curr(); it.next_ne())
135 auto *bucket =
static_cast<Bucket *
>(it.get_curr());
136 insert(bucket->get_key());
166 other.lower_alpha,
other.upper_alpha,
true,
true)
192 bool contains(
const Key & key)
const noexcept {
return Base::contains(key); }
225 auto *
ret_val =
static_cast<Bucket *
>(this->Base::insert(bucket));
237 auto *
ret_val =
static_cast<Bucket *
>(this->Base::search_or_insert(bucket));
241 return {&
ret_val->get_key(),
true};
244 return {&
ret_val->get_key(),
false};
282 Key *
add(
const Key & key)
309 Key *
search(
const Key & key)
const noexcept
311 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
312 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
320 bool has(
const Key & key)
const noexcept
322 return this->Base::search(key) !=
nullptr;
325 const Key &
find(
const Key & key)
const
327 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
330 return bucket->get_key();
335 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
339 return bucket->get_key();
366 this->Base::remove(bucket);
372 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
375 this->Base::remove(bucket);
376 auto ret_val = bucket->get_key();
395 return this->Base::Iterator::get_curr_ne()->get_key();
410 return this->Base::Iterator::get_curr()->get_key();
413 void del() {
delete this->Base::Iterator::del(); }
418 return this->
get_it().get_curr();
423 return this->
get_it().get_curr();
430 return it.get_curr();
437 return it.get_curr();
445 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
457 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
469 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
476 template <
typename Key,
typename Data,
482 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
484 using Pair = std::pair<Key, Data>;
492 using Hash_Fct = std::function<size_t(
const Key &)>;
503 self->search_with_custom_hash(
hf,
k);
550 (
new typename Base::Bucket(
Pair(std::forward<Key>(key), std::forward<Data>(data))));
569 auto *bucket =
static_cast<Bucket *
>(
571 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
581 auto *bucket =
static_cast<Bucket *
>(
583 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
590 bool has(
const Key & key)
const noexcept {
return search(key) !=
nullptr; }
592 bool has(Key && key)
const noexcept {
return search(std::forward<Key>(key)) !=
nullptr; }
601 bool contains(
const Key & key)
const noexcept {
return has(key); }
609 bool contains(Key && key)
const noexcept {
return has(std::forward<Key>(key)); }
626 const Data &
find(
const Key & key)
const
646 return this->
find(key);
656 return this->
find(std::forward<Key>(key));
697 return this->
template maps<Key>([](
auto p) {
return p.first; });
702 return this->
template maps<Data>([](
auto p) {
return p.second; });
708 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
716 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
726 template <
typename Key,
typename Data,
734 template <
typename Key,
typename Data,
741 template <
typename T,
template <
typename>
class Container>
746 c2.for_each([&table](
const T & item)
762 template <
typename T,
template <
typename>
class Container>
769 template <
typename T,
template <
typename>
class Container>
776 c.for_each([&table, &
ret](
const T & i)
778 auto *ptr = table.
insert(i);
786 template <
typename T,
template <
typename>
class Container>
794 c.for_each([&table, &
ret, &i](
const T & item)
796 auto *ptr = table.
insert(item);
798 ret.append(std::pair<T, size_t>(item, i));
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#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.
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.
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 * search(const Key &key) const noexcept
Searches for a key in the hash table.
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.
DynHashTable(DynHashTable &&other) noexcept
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)
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)
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 & get_first() const
Key & find(const Key &key)
Key * search_or_insert(Key &&key)
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
bool has(Key &&key) const noexcept
Data & operator[](const Key &key)
Subscript operator for map access/insertion.
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()
DynList< Key > values() const
bool has(const Key &key) const noexcept
Checks if a key exists in the map.
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.
Data remove(const Key &key)
Removes a key-value pair from the map and returns the value.
bool contains(Key &&key) const noexcept
Checks if a key exists in the map (move version).
typename Base::Bucket Bucket
Pair * search(Key &&key) const noexcept
typename Base::Iterator Iterator
bool contains(const Key &key) const noexcept
Alias for has()
DynList< Pair * > items_ptr()
Pair * search(const Key &key) const noexcept
Searches for key and, if found, returns a pointer to the associated pair stored in the table.
std::function< size_t(const Key &)> Hash_Fct
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)
static constexpr bool has_search_with_custom_hash
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)
Pair * insert(const Key &key, Data &&data)
Equality test for containers.
Common methods to the Aleph-w ( ) containers.
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() const
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)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
size_t map_hash_fct(Fct fct, const std::pair< Key, Data > &p) noexcept
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
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.