38#ifndef TPL_DYNMAPOHASH_H
39#define TPL_DYNMAPOHASH_H
112template <
typename Key,
120 using Pair = std::pair<Key, Data>;
127#if !defined(_MSC_VER) || defined(__clang__)
133 using Hash_Fct = std::function<size_t(
const Key &)>;
171 bool with_resize =
true)
194 return reinterpret_cast<Pair *
>(ptr);
199 return reinterpret_cast<const Pair *
>(ptr);
214 return reinterpret_cast<Pair *
>(
reinterpret_cast<char *
>(ptr) -
offsetof(
Pair, second));
219 return reinterpret_cast<const Pair *
>(
reinterpret_cast<const char *
>(ptr) -
276 return this->Base::insert(
Pair(key, data));
287 return this->Base::insert(
Pair(key, std::forward<Data>(data)));
298 return this->Base::insert(
Pair(std::forward<Key>(key), std::forward<Data>(data)));
309 return this->Base::insert(
Pair(std::forward<Key>(key), data));
319 static_assert(std::is_default_constructible<Data>::value,
320 "MapOpenHash::search() requires Data to be default-constructible");
321 return this->Base::search(
Pair(key, Data()));
331 static_assert(std::is_default_constructible<Data>::value,
332 "MapOpenHash::search() requires Data to be default-constructible");
333 return this->Base::search(
Pair(std::move(key), Data()));
343 return search(key) !=
nullptr;
353 return search(std::move(key)) !=
nullptr;
375 return has(std::move(key));
387 static_assert(std::is_default_constructible<Data>::value,
388 "MapOpenHash::find() requires Data to be default-constructible");
389 return Base::find(
Pair(key, Data())).second;
401 static_assert(std::is_default_constructible<Data>::value,
402 "MapOpenHash::find() requires Data to be default-constructible");
403 return Base::find(
Pair(std::move(key), Data())).second;
415 static_assert(std::is_default_constructible<Data>::value,
416 "MapOpenHash::find() requires Data to be default-constructible");
417 return Base::find(
Pair(key, Data())).second;
429 static_assert(std::is_default_constructible<Data>::value,
430 "MapOpenHash::find() requires Data to be default-constructible");
431 return Base::find(
Pair(std::move(key), Data())).second;
447 static_assert(std::is_default_constructible<Data>::value,
448 "MapOpenHash::operator[] requires Data to be default-constructible");
449 return this->search_or_insert(
Pair(key, Data()))->second;
461 return this->
find(key);
471 static_assert(std::is_default_constructible<Data>::value,
472 "MapOpenHash::operator[] requires Data to be default-constructible");
473 return this->search_or_insert(
Pair(std::move(key), Data()))->second;
485 return this->
find(std::move(key));
509 static_assert(std::is_default_constructible<Data>::value,
510 "MapOpenHash::remove() requires Data to be default-constructible");
511 Base::remove(
Pair(key, Data()));
522 static_assert(std::is_default_constructible<Data>::value,
523 "MapOpenHash::remove() requires Data to be default-constructible");
524 Base::remove(
Pair(std::move(key), Data()));
536 return this->
template maps<Key>([](
auto p)
563 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
577 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
598template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
620template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Doubly-linked list (defined in tpl_dynList.H).
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)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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
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.
size_t(*)(const Key &) Hash_Fct_Ptr
Function pointer type for hash functions.
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).
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.