38#ifndef TPL_DYNMAPOHASH_H
39#define TPL_DYNMAPOHASH_H
112template <
typename Key,
115 template <
typename,
class>
class HashTable =
ODhashTable>
116 requires EqualityComparator<Cmp, Key>
117struct MapOpenHash :
public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
120 using Pair = std::pair<Key, Data>;
127#if !defined(_MSC_VER) || defined(__clang__)
134 using Hash_Fct = std::function<size_t(
const Key &)>;
150 template <
typename K>
152 std::is_nothrow_constructible_v<Key, K>
and
153 std::is_nothrow_default_constructible_v<Data>
and
154 std::is_nothrow_move_constructible_v<Data>;
183 bool with_resize =
true)
185 hash_fct_detail::map_hash_of<Key, Data>(first_hash_fct),
186 hash_fct_detail::map_hash_of<Key, Data>(second_hash_fct),
206 return reinterpret_cast<Pair *
>(ptr);
211 return reinterpret_cast<const Pair *
>(ptr);
226 return reinterpret_cast<Pair *
>(
reinterpret_cast<char *
>(ptr) - offsetof(
Pair, second));
231 return reinterpret_cast<const Pair *
>(
reinterpret_cast<const char *
>(ptr) -
232 offsetof(
Pair, second));
245 [[nodiscard]]
static Data &
get_data(Key &key)
noexcept
250 [[nodiscard]]
static const Data &
get_data(
const Key &key)
noexcept
265 [[nodiscard]]
static const Key &
get_key(Data *data_ptr)
noexcept
270 [[nodiscard]]
static const Key &
get_key(
const Data *data_ptr)
noexcept
288 return this->Base::insert(
Pair(key, data));
299 return this->Base::insert(
Pair(key, std::forward<Data>(data)));
310 return this->Base::insert(
Pair(std::forward<Key>(key), std::forward<Data>(data)));
321 return this->Base::insert(
Pair(std::forward<Key>(key), data));
334 [[nodiscard]]
Pair *
search(
const Key &key)
noexcept(nothrow_search<const Key &>)
336 static_assert(std::is_default_constructible<Data>::value,
337 "MapOpenHash::search() requires Data to be default-constructible");
338 return this->Base::search(
Pair(key, Data()));
343 [[nodiscard]]
const Pair *
search(
const Key &key)
const noexcept(nothrow_search<const Key &>)
345 static_assert(std::is_default_constructible<Data>::value,
346 "MapOpenHash::search() requires Data to be default-constructible");
347 return this->Base::search(
Pair(key, Data()));
360 [[nodiscard]]
Pair *
search(Key &&key)
noexcept(nothrow_search<Key>)
362 static_assert(std::is_default_constructible<Data>::value,
363 "MapOpenHash::search() requires Data to be default-constructible");
364 return this->Base::search(
Pair(std::move(key), Data()));
369 [[nodiscard]]
const Pair *
search(Key &&key)
const noexcept(nothrow_search<Key>)
371 static_assert(std::is_default_constructible<Data>::value,
372 "MapOpenHash::search() requires Data to be default-constructible");
373 return this->Base::search(
Pair(std::move(key), Data()));
386 [[nodiscard]]
bool has(
const Key &key)
const noexcept(nothrow_search<const Key &>)
388 return search(key) !=
nullptr;
401 [[nodiscard]]
bool has(Key &&key)
const noexcept(nothrow_search<Key>)
403 return search(std::move(key)) !=
nullptr;
418 [[nodiscard]]
bool contains(
const Key &key)
const noexcept(nothrow_search<const Key &>)
433 [[nodiscard]]
bool contains(Key &&key)
const noexcept(nothrow_search<Key>)
435 return has(std::move(key));
445 [[nodiscard]] Data &
find(
const Key &key)
447 static_assert(std::is_default_constructible<Data>::value,
448 "MapOpenHash::find() requires Data to be default-constructible");
449 return Base::find(
Pair(key, Data())).second;
459 [[nodiscard]] Data &
find(Key &&key)
461 static_assert(std::is_default_constructible<Data>::value,
462 "MapOpenHash::find() requires Data to be default-constructible");
463 return Base::find(
Pair(std::move(key), Data())).second;
473 [[nodiscard]]
const Data &
find(
const Key &key)
const
475 static_assert(std::is_default_constructible<Data>::value,
476 "MapOpenHash::find() requires Data to be default-constructible");
477 return Base::find(
Pair(key, Data())).second;
487 [[nodiscard]]
const Data &
find(Key &&key)
const
489 static_assert(std::is_default_constructible<Data>::value,
490 "MapOpenHash::find() requires Data to be default-constructible");
491 return Base::find(
Pair(std::move(key), Data())).second;
509 static_assert(std::is_default_constructible<Data>::value,
510 "MapOpenHash::operator[] requires Data to be default-constructible");
511 Pair *
pair = this->search_or_insert(
Pair(key, Data()));
513 <<
"Hash table is full and does not resize itself";
527 return this->
find(key);
540 static_assert(std::is_default_constructible<Data>::value,
541 "MapOpenHash::operator[] requires Data to be default-constructible");
542 Pair *
pair = this->search_or_insert(
Pair(std::move(key), Data()));
544 <<
"Hash table is full and does not resize itself";
558 return this->
find(std::move(key));
582 static_assert(std::is_default_constructible<Data>::value,
583 "MapOpenHash::remove() requires Data to be default-constructible");
584 Base::remove(
Pair(key, Data()));
595 static_assert(std::is_default_constructible<Data>::value,
596 "MapOpenHash::remove() requires Data to be default-constructible");
597 Base::remove(
Pair(std::move(key), Data()));
609 return this->
template maps<Key>([](
const auto & p) ->
const Key &
621 return this->
template maps<Data>([](
const auto & p) ->
const Data &
636 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
637 ret.
append(&it.get_curr().second);
650 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
651 ret.
append(&it.get_curr());
671template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
672 requires EqualityComparator<Cmp, Key>
694template <
typename Key,
typename Data,
class Cmp = Aleph::equal_to<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
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)
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
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.
const Pair * search(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
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).
Pair Item_Type
The item type stored in the map (key-value pair).
Pair * search(Key &&key) noexcept(nothrow_search< Key >)
Search for a key in the map (move semantics).
static const Key & get_key(const Data *data_ptr) noexcept
Pair * insert(Key &&key, Data &&data)
Insert a key-value pair (move both).
bool contains(Key &&key) const noexcept(nothrow_search< Key >)
Check if a key exists in the map (move semantics).
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.
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).
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.
const Pair * search(const Key &key) const noexcept(nothrow_search< const Key & >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool has(Key &&key) const noexcept(nothrow_search< Key >)
Check if a key exists in the map (move semantics).
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
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).
static constexpr bool nothrow_search
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 * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
bool has(const Key &key) const noexcept(nothrow_search< const Key & >)
Check if a key exists in the map.
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.
bool contains(const Key &key) const noexcept(nothrow_search< const Key & >)
Check if a key exists 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.