39#ifndef TPL_DYNMAPOHASH_H
40#define TPL_DYNMAPOHASH_H
114template <
typename Key,
typename Data,
119 public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
122 using Pair = std::pair<Key, Data>;
131 using Hash_Fct = std::function<size_t(
const Key &)>;
169 bool with_resize =
true)
175 lower_alpha, upper_alpha, with_resize) {}
190 return reinterpret_cast<Pair*
>(ptr);
195 return reinterpret_cast<const Pair*
>(ptr);
210 return reinterpret_cast<Pair*
>(
216 return reinterpret_cast<const Pair*
>(
217 reinterpret_cast<const char*
>(ptr) -
offsetof(
Pair, second));
273 return this->Base::insert(
Pair(key, data));
284 return this->Base::insert(
Pair(key, std::forward<Data>(data)));
295 return this->Base::insert(
Pair(std::forward<Key>(key),
296 std::forward<Data>(data)));
307 return this->Base::insert(
Pair(std::forward<Key>(key), data));
317 static_assert(std::is_default_constructible<Data>::value,
318 "MapOpenHash::search() requires Data to be default-constructible");
319 return this->Base::search(
Pair(key, Data()));
329 static_assert(std::is_default_constructible<Data>::value,
330 "MapOpenHash::search() requires Data to be default-constructible");
331 return this->Base::search(
Pair(std::move(key), Data()));
341 return search(key) !=
nullptr;
351 return search(std::move(key)) !=
nullptr;
373 return has(std::move(key));
385 static_assert(std::is_default_constructible<Data>::value,
386 "MapOpenHash::find() requires Data to be default-constructible");
387 return Base::find(
Pair(key, Data())).second;
399 static_assert(std::is_default_constructible<Data>::value,
400 "MapOpenHash::find() requires Data to be default-constructible");
401 return Base::find(
Pair(std::move(key), Data())).second;
413 static_assert(std::is_default_constructible<Data>::value,
414 "MapOpenHash::find() requires Data to be default-constructible");
415 return Base::find(
Pair(key, Data())).second;
427 static_assert(std::is_default_constructible<Data>::value,
428 "MapOpenHash::find() requires Data to be default-constructible");
429 return Base::find(
Pair(std::move(key), Data())).second;
445 static_assert(std::is_default_constructible<Data>::value,
446 "MapOpenHash::operator[] requires Data to be default-constructible");
447 return this->search_or_insert(
Pair(key, Data()))->second;
459 return this->
find(key);
469 static_assert(std::is_default_constructible<Data>::value,
470 "MapOpenHash::operator[] requires Data to be default-constructible");
471 return this->search_or_insert(
Pair(std::move(key), Data()))->second;
483 return this->
find(std::move(key));
507 static_assert(std::is_default_constructible<Data>::value,
508 "MapOpenHash::remove() requires Data to be default-constructible");
509 Base::remove(
Pair(key, Data()));
520 static_assert(std::is_default_constructible<Data>::value,
521 "MapOpenHash::remove() requires Data to be default-constructible");
522 Base::remove(
Pair(std::move(key), Data()));
534 return this->
template maps<Key>([] (
auto p) {
return p.first; });
543 return this->
template maps<Data>([] (
auto p) {
return p.second; });
555 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
569 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
591template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
614template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
C++20 concepts for constraining comparison functors.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Open addressing hash table with double hashing collision resolution.
Open addressing hash table with linear probing collision resolution.
Equivalence relation constraint for equality comparators.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
size_t map_hash_fct(Fct fct, const std::pair< Key, Data > &p) noexcept
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.
const float hash_default_lower_alpha
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Default comparator for pair types in hash maps.
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
Open addressing hash map for key-value pairs.
void remove(Key &&key)
Remove an entry by key (move semantics).
MapOpenHash(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr first_hash_fct=dft_hash_ptr_fct< Key >, Hash_Fct_Ptr second_hash_fct=snd_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha, bool with_resize=true)
Construct a map with specified parameters.
const Data & find(Key &&key) const
Find and return the value for a key (const, move).
std::pair< Key, Data > Pair
The key-value pair type stored in the map.
void remove(const Key &key)
Remove an entry by key.
DynList< Data > values() const
Get a list of all values in the map.
Key Key_Type
The type of keys in the map.
Data & operator[](const Key &key)
Access or insert a value by key.
const Data & find(const Key &key) const
Find and return the value for a key (const).
bool contains(const Key &key) const noexcept
Check if a key exists in the map.
Pair Item_Type
The item type stored in the map (key-value pair).
static const Key & get_key(const Data *data_ptr) noexcept
Pair * insert(Key &&key, Data &&data)
Insert a key-value pair (move both).
Data & find(const Key &key)
Find and return the value for a key.
static const Key & get_key(Data *data_ptr) noexcept
Get the key associated with a data pointer.
bool has(const Key &key) const noexcept
Check if a key exists in the map.
Data Data_Type
The type of mapped values.
static const Pair * data_to_pair(const Data *ptr) noexcept
Pair * insert(const Key &key, Data &&data)
Insert a key-value pair (move data).
bool contains(Key &&key) const noexcept
Check if a key exists in the map (move semantics).
typename Base::Iterator Iterator
Iterator type for traversing the map.
static const Pair * key_to_pair(const Key *ptr) noexcept
HashTable< std::pair< Key, Data >, Dft_Pair_Cmp< Key, Data, Cmp > > Base
The base hash table type.
bool has(Key &&key) const noexcept
Check if a key exists in the map (move semantics).
DynList< Key > keys() const
Get a list of all keys in the map.
static Pair * data_to_pair(Data *ptr) noexcept
Convert a data pointer to its containing pair pointer.
Data Value_Type
Alias for Data_Type (compatibility with other containers).
Pair * insert(Key &&key, const Data &data)
Insert a key-value pair (move key, copy data).
static const Data & get_data(const Key &key) noexcept
DynList< Data * > values_ptr()
Get a list of pointers to all values.
Pair * search(Key &&key) const noexcept
Search for a key in the map (move semantics).
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
size_t(*)(const Key &) Hash_Fct_Ptr
Function pointer type for hash functions.
static Data & get_data(Key &key) noexcept
Get the data associated with a key by reference.
std::function< size_t(const Key &)> Hash_Fct
Function type for hash functions.
void remove_by_data(Data &data)
Remove an entry by its data pointer.
static Pair * key_to_pair(Key *ptr) noexcept
Convert a key pointer to its containing pair pointer.
DynList< Pair * > items_ptr()
Get a list of pointers to all pairs.
Pair * search(const Key &key) const noexcept
Search for a key in the map.
Data & find(Key &&key)
Find and return the value for a key (move semantics).
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.