Aleph-w 3.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>
117struct MapOpenHash : public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
118{
120 using Pair = std::pair<Key, Data>;
121
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;
131
133 using Hash_Fct = std::function<size_t(const Key &)>;
134
136 using Hash_Fct_Ptr = size_t (*)(const Key &);
137
139 using Key_Type = Key;
140
142 using Data_Type = Data;
143
145 using Value_Type = Data;
146
149
152
167 Hash_Fct_Ptr second_hash_fct = snd_hash_ptr_fct<Key>,
168 Cmp cmp = Cmp(),
169 float lower_alpha = hash_default_lower_alpha,
170 float upper_alpha = hash_default_upper_alpha,
171 bool with_resize = true)
172 : Base(len,
174 std::bind(map_hash_fct<Key, Data, Hash_Fct>, second_hash_fct, std::placeholders::_1),
175 Dft_Pair_Cmp<Key, Data, Cmp>(cmp),
176 lower_alpha,
177 upper_alpha,
178 with_resize)
179 {}
180
192 [[nodiscard]] static Pair *key_to_pair(Key *ptr) noexcept
193 {
194 return reinterpret_cast<Pair *>(ptr);
195 }
196
197 [[nodiscard]] static const Pair *key_to_pair(const Key *ptr) noexcept
198 {
199 return reinterpret_cast<const Pair *>(ptr);
200 }
201
212 [[nodiscard]] static Pair *data_to_pair(Data *ptr) noexcept
213 {
214 return reinterpret_cast<Pair *>(reinterpret_cast<char *>(ptr) - offsetof(Pair, second));
215 }
216
217 [[nodiscard]] static const Pair *data_to_pair(const Data *ptr) noexcept
218 {
219 return reinterpret_cast<const Pair *>(reinterpret_cast<const char *>(ptr) -
220 offsetof(Pair, second));
221 }
222
233 [[nodiscard]] static Data &get_data(Key &key) noexcept
234 {
235 return key_to_pair(&key)->second;
236 }
237
238 [[nodiscard]] static const Data &get_data(const Key &key) noexcept
239 {
240 return key_to_pair(&key)->second;
241 }
242
253 [[nodiscard]] static const Key &get_key(Data *data_ptr) noexcept
254 {
255 return data_to_pair(data_ptr)->first;
256 }
257
258 [[nodiscard]] static const Key &get_key(const Data *data_ptr) noexcept
259 {
260 return data_to_pair(data_ptr)->first;
261 }
262
274 Pair *insert(const Key &key, const Data &data)
275 {
276 return this->Base::insert(Pair(key, data));
277 }
278
285 Pair *insert(const Key &key, Data &&data)
286 {
287 return this->Base::insert(Pair(key, std::forward<Data>(data)));
288 }
289
296 Pair *insert(Key &&key, Data &&data)
297 {
298 return this->Base::insert(Pair(std::forward<Key>(key), std::forward<Data>(data)));
299 }
300
307 Pair *insert(Key &&key, const Data &data)
308 {
309 return this->Base::insert(Pair(std::forward<Key>(key), data));
310 }
311
317 [[nodiscard]] Pair *search(const Key &key) const noexcept
318 {
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()));
322 }
323
329 [[nodiscard]] Pair *search(Key &&key) const noexcept
330 {
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()));
334 }
335
341 [[nodiscard]] bool has(const Key &key) const noexcept
342 {
343 return search(key) != nullptr;
344 }
345
351 [[nodiscard]] bool has(Key &&key) const noexcept
352 {
353 return search(std::move(key)) != nullptr;
354 }
355
363 [[nodiscard]] bool contains(const Key &key) const noexcept
364 {
365 return has(key);
366 }
367
373 [[nodiscard]] bool contains(Key &&key) const noexcept
374 {
375 return has(std::move(key));
376 }
377
385 [[nodiscard]] Data &find(const Key &key)
386 {
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;
390 }
391
399 [[nodiscard]] Data &find(Key &&key)
400 {
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;
404 }
405
413 [[nodiscard]] const Data &find(const Key &key) const
414 {
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;
418 }
419
427 [[nodiscard]] const Data &find(Key &&key) const
428 {
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;
432 }
433
445 [[nodiscard]] Data &operator [] (const Key &key)
446 {
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;
450 }
451
459 [[nodiscard]] const Data &operator [] (const Key &key) const
460 {
461 return this->find(key);
462 }
463
469 [[nodiscard]] Data &operator [] (Key &&key)
470 {
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;
474 }
475
483 [[nodiscard]] const Data &operator [] (Key &&key) const
484 {
485 return this->find(std::move(key));
486 }
487
496 void remove_by_data(Data &data)
497 {
498 Base::remove_ptr(data_to_pair(&data));
499 }
500
507 void remove(const Key &key)
508 {
509 static_assert(std::is_default_constructible<Data>::value,
510 "MapOpenHash::remove() requires Data to be default-constructible");
511 Base::remove(Pair(key, Data()));
512 }
513
520 void remove(Key &&key)
521 {
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()));
525 }
526
528 using Iterator = typename Base::Iterator;
529
535 {
536 return this->template maps<Key>([](auto p)
537 {
538 return p.first;
539 });
540 }
541
547 {
548 return this->template maps<Data>([](auto p)
549 {
550 return p.second;
551 });
552 }
553
561 {
563 for (Iterator it(*this); it.has_curr(); it.next_ne())
564 ret.append(&it.get_curr().second);
565 return ret;
566 }
567
575 {
577 for (Iterator it(*this); it.has_curr(); it.next_ne())
578 ret.append(&it.get_curr());
579 return ret;
580 }
581};
582
598template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
604
620template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
622struct MapODhash : public MapOpenHash<Key, Data, Cmp, ODhashTable>
623{
624 using MapOpenHash<Key, Data, Cmp, ODhashTable>::MapOpenHash;
625};
626
627} // end namespace Aleph
628
629#endif // TPL_DYNMAPOHASH_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:182
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:170
Equivalence relation constraint for equality comparators.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4129
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t map_hash_fct(Fct fct, const std::pair< Key, Data > &p) noexcept
Definition hash-fct.H:1137
const float hash_default_upper_alpha
Definition hash-dry.C:40
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
STL namespace.
Default comparator for pair types in hash maps.
Definition ahDry.H:190
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.