Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMapOhash.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
38#ifndef TPL_DYNMAPOHASH_H
39#define TPL_DYNMAPOHASH_H
40
41#include <cstddef>
42#include <functional>
43#include <type_traits>
44#include <utility>
45
46#include <tpl_olhash.H>
47#include <tpl_odhash.H>
48#include <ah-concepts.H>
49
50namespace Aleph {
51
112template <typename Key,
113 typename Data,
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>>
118{
120 using Pair = std::pair<Key, Data>;
121
123 using Base = HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>;
124
125 // MSVC: 'using Base::Base' imports ODhashTable(size_t) which collides
126 // with the explicitly defined MapOpenHash(size_t, ...) below (C2668).
127#if !defined(_MSC_VER) || defined(__clang__)
128 using Base::Base;
129#endif
130 using Base::insert;
132
134 using Hash_Fct = std::function<size_t(const Key &)>;
135
137 using Hash_Fct_Ptr = size_t (*)(const Key &);
138
140 using Key_Type = Key;
141
143 using Data_Type = Data;
144
145 // Whether a search by a key that comes as K (const Key &, or Key for
146 // the searches that take an rvalue) cannot throw: it builds the pair
147 // that the table searches for, a copy or a move of the key and a Data
148 // built by default. The members of the pair are checked apart, as
149 // libstdc++ 11 does not declare the constructors of std::pair noexcept
150 template <typename K>
151 static constexpr bool nothrow_search =
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>;
155
157 using Value_Type = Data;
158
161
164
178 Hash_Fct_Ptr first_hash_fct = dft_hash_ptr_fct<Key>,
179 Hash_Fct_Ptr second_hash_fct = snd_hash_ptr_fct<Key>,
180 Cmp cmp = Cmp(),
181 float lower_alpha = hash_default_lower_alpha,
182 float upper_alpha = hash_default_upper_alpha,
183 bool with_resize = true)
184 : Base(len,
185 hash_fct_detail::map_hash_of<Key, Data>(first_hash_fct),
186 hash_fct_detail::map_hash_of<Key, Data>(second_hash_fct),
187 Dft_Pair_Cmp<Key, Data, Cmp>(cmp),
188 lower_alpha,
189 upper_alpha,
190 with_resize)
191 {}
192
204 [[nodiscard]] static Pair *key_to_pair(Key *ptr) noexcept
205 {
206 return reinterpret_cast<Pair *>(ptr);
207 }
208
209 [[nodiscard]] static const Pair *key_to_pair(const Key *ptr) noexcept
210 {
211 return reinterpret_cast<const Pair *>(ptr);
212 }
213
224 [[nodiscard]] static Pair *data_to_pair(Data *ptr) noexcept
225 {
226 return reinterpret_cast<Pair *>(reinterpret_cast<char *>(ptr) - offsetof(Pair, second));
227 }
228
229 [[nodiscard]] static const Pair *data_to_pair(const Data *ptr) noexcept
230 {
231 return reinterpret_cast<const Pair *>(reinterpret_cast<const char *>(ptr) -
232 offsetof(Pair, second));
233 }
234
245 [[nodiscard]] static Data &get_data(Key &key) noexcept
246 {
247 return key_to_pair(&key)->second;
248 }
249
250 [[nodiscard]] static const Data &get_data(const Key &key) noexcept
251 {
252 return key_to_pair(&key)->second;
253 }
254
265 [[nodiscard]] static const Key &get_key(Data *data_ptr) noexcept
266 {
267 return data_to_pair(data_ptr)->first;
268 }
269
270 [[nodiscard]] static const Key &get_key(const Data *data_ptr) noexcept
271 {
272 return data_to_pair(data_ptr)->first;
273 }
274
286 Pair *insert(const Key &key, const Data &data)
287 {
288 return this->Base::insert(Pair(key, data));
289 }
290
297 Pair *insert(const Key &key, Data &&data)
298 {
299 return this->Base::insert(Pair(key, std::forward<Data>(data)));
300 }
301
308 Pair *insert(Key &&key, Data &&data)
309 {
310 return this->Base::insert(Pair(std::forward<Key>(key), std::forward<Data>(data)));
311 }
312
319 Pair *insert(Key &&key, const Data &data)
320 {
321 return this->Base::insert(Pair(std::forward<Key>(key), data));
322 }
323
334 [[nodiscard]] Pair *search(const Key &key) noexcept(nothrow_search<const Key &>)
335 {
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()));
339 }
340
343 [[nodiscard]] const Pair *search(const Key &key) const noexcept(nothrow_search<const Key &>)
344 {
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()));
348 }
349
360 [[nodiscard]] Pair *search(Key &&key) noexcept(nothrow_search<Key>)
361 {
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()));
365 }
366
369 [[nodiscard]] const Pair *search(Key &&key) const noexcept(nothrow_search<Key>)
370 {
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()));
374 }
375
386 [[nodiscard]] bool has(const Key &key) const noexcept(nothrow_search<const Key &>)
387 {
388 return search(key) != nullptr;
389 }
390
401 [[nodiscard]] bool has(Key &&key) const noexcept(nothrow_search<Key>)
402 {
403 return search(std::move(key)) != nullptr;
404 }
405
418 [[nodiscard]] bool contains(const Key &key) const noexcept(nothrow_search<const Key &>)
419 {
420 return has(key);
421 }
422
433 [[nodiscard]] bool contains(Key &&key) const noexcept(nothrow_search<Key>)
434 {
435 return has(std::move(key));
436 }
437
445 [[nodiscard]] Data &find(const Key &key)
446 {
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;
450 }
451
459 [[nodiscard]] Data &find(Key &&key)
460 {
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;
464 }
465
473 [[nodiscard]] const Data &find(const Key &key) const
474 {
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;
478 }
479
487 [[nodiscard]] const Data &find(Key &&key) const
488 {
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;
492 }
493
507 [[nodiscard]] Data &operator [] (const Key &key)
508 {
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()));
512 ah_overflow_error_if(pair == nullptr)
513 << "Hash table is full and does not resize itself";
514
515 return pair->second;
516 }
517
525 [[nodiscard]] const Data &operator [] (const Key &key) const
526 {
527 return this->find(key);
528 }
529
538 [[nodiscard]] Data &operator [] (Key &&key)
539 {
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()));
543 ah_overflow_error_if(pair == nullptr)
544 << "Hash table is full and does not resize itself";
545
546 return pair->second;
547 }
548
556 [[nodiscard]] const Data &operator [] (Key &&key) const
557 {
558 return this->find(std::move(key));
559 }
560
569 void remove_by_data(Data &data)
570 {
571 Base::remove_ptr(data_to_pair(&data));
572 }
573
580 void remove(const Key &key)
581 {
582 static_assert(std::is_default_constructible<Data>::value,
583 "MapOpenHash::remove() requires Data to be default-constructible");
584 Base::remove(Pair(key, Data()));
585 }
586
593 void remove(Key &&key)
594 {
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()));
598 }
599
601 using Iterator = typename Base::Iterator;
602
607 [[nodiscard]] DynList<Key> keys() const
608 {
609 return this->template maps<Key>([](const auto & p) -> const Key &
610 {
611 return p.first;
612 });
613 }
614
619 [[nodiscard]] DynList<Data> values() const
620 {
621 return this->template maps<Data>([](const auto & p) -> const Data &
622 {
623 return p.second;
624 });
625 }
626
634 {
635 DynList<Data *> ret;
636 for (Iterator it(*this); it.has_curr(); it.next_ne())
637 ret.append(&it.get_curr().second);
638 return ret;
639 }
640
648 {
649 DynList<Pair *> ret;
650 for (Iterator it(*this); it.has_curr(); it.next_ne())
651 ret.append(&it.get_curr());
652 return ret;
653 }
654};
655
671template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
672 requires EqualityComparator<Cmp, Key>
678
694template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
696struct MapODhash : public MapOpenHash<Key, Data, Cmp, ODhashTable>
697{
698 using MapOpenHash<Key, Data, Cmp, ODhashTable>::MapOpenHash;
700};
701
702} // end namespace Aleph
703
704#endif // TPL_DYNMAPOHASH_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
Definition ah-dry.H:2881
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
Equivalence relation constraint for equality comparators.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
Definition hash-dry.C:40
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
const float hash_default_lower_alpha
Definition hash-dry.C:38
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
Default comparator for pair types in hash maps.
Definition ahDry.H:206
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.