39#ifndef TPL_DYNMAPOHASH_H
40#define TPL_DYNMAPOHASH_H
113template <
typename Key,
typename Data,
117 public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
120 using Pair = std::pair<Key, Data>;
167 bool with_resize =
true)
173 lower_alpha, upper_alpha, with_resize) {}
188 return reinterpret_cast<Pair*
>(ptr);
193 return reinterpret_cast<const Pair*
>(ptr);
208 return reinterpret_cast<Pair*
>(
214 return reinterpret_cast<const Pair*
>(
271 return this->Base::insert(
Pair(key, data));
282 return this->Base::insert(
Pair(key, std::forward<Data>(data)));
293 return this->Base::insert(
Pair(std::forward<Key>(key),
294 std::forward<Data>(data)));
305 return this->Base::insert(
Pair(std::forward<Key>(key), data));
315 static_assert(std::is_default_constructible<Data>::value,
316 "MapOpenHash::search() requires Data to be default-constructible");
317 return this->Base::search(
Pair(key,
Data()));
327 static_assert(std::is_default_constructible<Data>::value,
328 "MapOpenHash::search() requires Data to be default-constructible");
329 return this->Base::search(
Pair(std::move(key),
Data()));
339 return search(key) !=
nullptr;
349 return search(std::move(key)) !=
nullptr;
371 return has(std::move(key));
383 static_assert(std::is_default_constructible<Data>::value,
384 "MapOpenHash::find() requires Data to be default-constructible");
385 return Base::find(
Pair(key,
Data())).second;
397 static_assert(std::is_default_constructible<Data>::value,
398 "MapOpenHash::find() requires Data to be default-constructible");
399 return Base::find(
Pair(std::move(key),
Data())).second;
411 static_assert(std::is_default_constructible<Data>::value,
412 "MapOpenHash::find() requires Data to be default-constructible");
413 return Base::find(
Pair(key,
Data())).second;
425 static_assert(std::is_default_constructible<Data>::value,
426 "MapOpenHash::find() requires Data to be default-constructible");
427 return Base::find(
Pair(std::move(key),
Data())).second;
443 static_assert(std::is_default_constructible<Data>::value,
444 "MapOpenHash::operator[] requires Data to be default-constructible");
445 return this->search_or_insert(
Pair(key,
Data()))->second;
457 return this->
find(key);
467 static_assert(std::is_default_constructible<Data>::value,
468 "MapOpenHash::operator[] requires Data to be default-constructible");
469 return this->search_or_insert(
Pair(std::move(key),
Data()))->second;
481 return this->
find(std::move(key));
505 static_assert(std::is_default_constructible<Data>::value,
506 "MapOpenHash::remove() requires Data to be default-constructible");
518 static_assert(std::is_default_constructible<Data>::value,
519 "MapOpenHash::remove() requires Data to be default-constructible");
520 Base::remove(
Pair(std::move(key),
Data()));
532 return this->
template maps<Key>([] (
auto p) {
return p.first; });
541 return this->
template maps<Data>([] (
auto p) {
return p.second; });
553 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
567 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
589template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
611template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
Open addressing hash table with double hashing collision resolution.
Open addressing hash table with linear probing collision resolution.
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
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.
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).
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.
MapOpenHash(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr first_hash_fct=dft_hash_fct< Key >, Hash_Fct_Ptr second_hash_fct=snd_hash_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.
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.