45# ifndef TPL_DYNSETHASH_H
46# define TPL_DYNSETHASH_H
73 template <
typename Key,
81 public GenericKeys<DynHashTable<Key, HashTable, Cmp>, Key>,
88 using Bucket =
typename HashTable<Key, Cmp>::Bucket;
122 float lower_alpha,
float upper_alpha)
131 for (
typename Base::Iterator it(
other); it.has_curr(); it.next_ne())
133 auto *bucket =
static_cast<Bucket *
>(it.get_curr());
134 insert(bucket->get_key());
164 other.lower_alpha,
other.upper_alpha,
true,
true)
200 auto *
ret_val =
static_cast<Bucket *
>(this->Base::insert(bucket));
212 auto *
ret_val =
static_cast<Bucket *
>(this->Base::search_or_insert(bucket));
216 return {&
ret_val->get_key(),
true};
219 return {&
ret_val->get_key(),
false};
257 Key *
add(
const Key & key)
284 Key *
search(
const Key & key)
const noexcept
286 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
287 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
295 bool has(
const Key & key)
const noexcept
297 return this->Base::search(key) !=
nullptr;
301 bool contains(
const Key & key)
const noexcept {
return has(key); }
303 const Key &
find(
const Key & key)
const
305 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
308 return bucket->get_key();
313 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
317 return bucket->get_key();
324 const auto offset =
reinterpret_cast<size_t>(&
ret_val->get_key());
326 return reinterpret_cast<Bucket *
>(
reinterpret_cast<size_t>(key) -
offset);
347 this->Base::remove(bucket);
353 auto *bucket =
static_cast<Bucket *
>(this->Base::search(key));
356 this->Base::remove(bucket);
357 auto ret_val = bucket->get_key();
376 return this->Base::Iterator::get_curr_ne()->get_key();
391 return this->Base::Iterator::get_curr()->get_key();
394 void del() {
delete this->Base::Iterator::del(); }
399 return this->
get_it().get_curr();
404 return this->
get_it().get_curr();
411 return it.get_curr();
418 return it.get_curr();
426 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
437 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
448 template <
typename Key,
class Cmp = Aleph::equal_to<Key>>
455 template <
typename Key,
typename Data,
460 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
462 using Pair = std::pair<Key, Data>;
481 self->search_with_custom_hash(
hf,
k);
528 (
new typename Base::Bucket(
Pair(std::forward<Key>(key), std::forward<Data>(data))));
547 auto *bucket =
static_cast<Bucket *
>(
549 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
559 auto *bucket =
static_cast<Bucket *
>(
561 return bucket !=
nullptr ? &bucket->get_key() :
nullptr;
568 bool has(
const Key & key)
const noexcept {
return search(key) !=
nullptr; }
570 bool has(Key && key)
const noexcept {
return search(std::forward<Key>(key)) !=
nullptr; }
573 bool contains(
const Key & key)
const noexcept {
return has(key); }
575 bool contains(Key && key)
const noexcept {
return has(std::forward<Key>(key)); }
612 return this->
find(key);
622 return this->
find(std::forward<Key>(key));
663 return this->
template maps<Key>([](
auto p) {
return p.first; });
668 return this->
template maps<Data>([](
auto p) {
return p.second; });
674 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
682 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
692 template <
typename Key,
typename Data,
700 template <
typename Key,
typename Data,
707 template <
typename T,
template <
typename>
class Container>
728 template <
typename T,
template <
typename>
class Container>
735 template <
typename T,
template <
typename>
class Container>
742 c.for_each([&table, &
ret](
const T & i)
744 auto *ptr = table.
insert(i);
752 template <
typename T,
template <
typename>
class Container>
760 c.for_each([&table, &
ret, &i](
const T & item)
762 auto *ptr = table.
insert(item);
764 ret.
append(std::pair<T, size_t>(item, i));
#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
Alias for has()
typename HashTable< Key, Cmp >::Bucket Bucket
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)
typename Base::Hash_Fct_Ptr Hash_Fct_Ptr
DynHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_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.
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.
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)
Append a new item by copy.
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.
DynMapHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_fct, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
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
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
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 criteria.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
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.
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
std::decay_t< typename HeadC::Item_Type > T
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
size_t dft_hash_fct(const Key &key) noexcept
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.