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();
346 const auto offset =
reinterpret_cast<size_t>(&
ret_val->get_key());
348 return reinterpret_cast<Bucket *
>(
reinterpret_cast<size_t>(key) -
offset);
369 this->Base::remove(bucket);
375 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
378 this->Base::remove(bucket);
379 auto ret_val = bucket->get_key();
398 return this->Base::Iterator::get_curr_ne()->get_key();
413 return this->Base::Iterator::get_curr()->get_key();
416 void del() {
delete this->Base::Iterator::del(); }
421 return this->
get_it().get_curr();
426 return this->
get_it().get_curr();
433 return it.get_curr();
440 return it.get_curr();
448 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
460 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
472 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
479 template <
typename Key,
typename Data,
485 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
487 using Pair = std::pair<Key, Data>;
495 using Hash_Fct = std::function<size_t(
const Key &)>;
506 self->search_with_custom_hash(
hf,
k);
553 (
new typename Base::Bucket(
Pair(std::forward<Key>(key), std::forward<Data>(data))));
572 auto *bucket =
static_cast<Bucket *
>(
574 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
584 auto *bucket =
static_cast<Bucket *
>(
586 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
593 bool has(
const Key & key)
const noexcept {
return search(key) !=
nullptr; }
595 bool has(Key && key)
const noexcept {
return search(std::forward<Key>(key)) !=
nullptr; }
604 bool contains(
const Key & key)
const noexcept {
return has(key); }
612 bool contains(Key && key)
const noexcept {
return has(std::forward<Key>(key)); }
629 const Data &
find(
const Key & key)
const
649 return this->
find(key);
659 return this->
find(std::forward<Key>(key));
700 return this->
template maps<Key>([](
auto p) {
return p.first; });
705 return this->
template maps<Data>([](
auto p) {
return p.second; });
711 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
719 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
729 template <
typename Key,
typename Data,
737 template <
typename Key,
typename Data,
744 template <
typename T,
template <
typename>
class Container>
749 c2.for_each([&table](
const T & item)
765 template <
typename T,
template <
typename>
class Container>
772 template <
typename T,
template <
typename>
class Container>
779 c.for_each([&table, &
ret](
const T & i)
781 auto *ptr = table.
insert(i);
789 template <
typename T,
template <
typename>
class Container>
797 c.for_each([&table, &
ret, &i](
const T & item)
799 auto *ptr = table.
insert(item);
801 ret.append(std::pair<T, size_t>(item, i));
C++20 concepts for constraining comparison functors.
#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.
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)
Dynamic singly linked list with functional programming support.
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)
Singly linked list implementations with head-tail access.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
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)
DynList< std::pair< T, size_t > > repeated_with_index(const Container< T > &c)
DynList< T > repeated(const Container< T > &c)
const float hash_default_upper_alpha
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.
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.