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
32
39#ifndef TPL_DYNMAPOHASH_H
40#define TPL_DYNMAPOHASH_H
41
42#include <cstddef>
43#include <functional>
44#include <type_traits>
45#include <utility>
46
47#include <tpl_olhash.H>
48#include <tpl_odhash.H>
49
50namespace Aleph
51{
52
113template <typename Key, typename Data,
114 class Cmp = Aleph::equal_to<Key>,
115 template <typename, class> class HashTable = ODhashTable>
117 public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
118{
120 using Pair = std::pair<Key, Data>;
121
124
125 using Base::Base;
126 using Base::insert;
127
129 using Hash_Fct = std::function<size_t(const Key &)>;
130
132 using Hash_Fct_Ptr = size_t (*) (const Key &);
133
135 using Key_Type = Key;
136
139
142
145
148
163 Hash_Fct_Ptr second_hash_fct = snd_hash_fct<Key>,
164 Cmp cmp = Cmp(),
165 float lower_alpha = hash_default_lower_alpha,
166 float upper_alpha = hash_default_upper_alpha,
167 bool with_resize = true)
168 : Base(len, std::bind(map_hash_fct<Key, Data, Hash_Fct>,
171 second_hash_fct, std::placeholders::_1),
172 Dft_Pair_Cmp<Key, Data, Cmp>(cmp),
173 lower_alpha, upper_alpha, with_resize) {}
174
186 [[nodiscard]] static Pair * key_to_pair(Key * ptr) noexcept
187 {
188 return reinterpret_cast<Pair*>(ptr);
189 }
190
191 [[nodiscard]] static const Pair * key_to_pair(const Key * ptr) noexcept
192 {
193 return reinterpret_cast<const Pair*>(ptr);
194 }
195
206 [[nodiscard]] static Pair * data_to_pair(Data * ptr) noexcept
207 {
208 return reinterpret_cast<Pair*>(
209 reinterpret_cast<char*>(ptr) - offsetof(Pair, second));
210 }
211
212 [[nodiscard]] static const Pair * data_to_pair(const Data * ptr) noexcept
213 {
214 return reinterpret_cast<const Pair*>(
215 reinterpret_cast<const char*>(ptr) - offsetof(Pair, second));
216 }
217
228 [[nodiscard]] static Data & get_data(Key & key) noexcept
229 {
230 return key_to_pair(&key)->second;
231 }
232
233 [[nodiscard]] static const Data & get_data(const Key & key) noexcept
234 {
235 return key_to_pair(&key)->second;
236 }
237
248 [[nodiscard]] static const Key & get_key(Data * data_ptr) noexcept
249 {
250 return data_to_pair(data_ptr)->first;
251 }
252
253 [[nodiscard]] static const Key & get_key(const Data * data_ptr) noexcept
254 {
255 return data_to_pair(data_ptr)->first;
256 }
257
269 Pair * insert(const Key & key, const Data & data)
270 {
271 return this->Base::insert(Pair(key, data));
272 }
273
280 Pair * insert(const Key & key, Data && data)
281 {
282 return this->Base::insert(Pair(key, std::forward<Data>(data)));
283 }
284
291 Pair * insert(Key && key, Data && data)
292 {
293 return this->Base::insert(Pair(std::forward<Key>(key),
294 std::forward<Data>(data)));
295 }
296
303 Pair * insert(Key && key, const Data & data)
304 {
305 return this->Base::insert(Pair(std::forward<Key>(key), data));
306 }
307
313 [[nodiscard]] Pair * search(const Key & key) const noexcept
314 {
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()));
318 }
319
325 [[nodiscard]] Pair * search(Key && key) const noexcept
326 {
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()));
330 }
331
337 [[nodiscard]] bool has(const Key & key) const noexcept
338 {
339 return search(key) != nullptr;
340 }
341
347 [[nodiscard]] bool has(Key && key) const noexcept
348 {
349 return search(std::move(key)) != nullptr;
350 }
351
359 [[nodiscard]] bool contains(const Key & key) const noexcept
360 {
361 return has(key);
362 }
363
369 [[nodiscard]] bool contains(Key && key) const noexcept
370 {
371 return has(std::move(key));
372 }
373
381 [[nodiscard]] Data & find(const Key & key)
382 {
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;
386 }
387
395 [[nodiscard]] Data & find(Key && key)
396 {
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;
400 }
401
409 [[nodiscard]] const Data & find(const Key & key) const
410 {
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;
414 }
415
423 [[nodiscard]] const Data & find(Key && key) const
424 {
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;
428 }
429
441 [[nodiscard]] Data & operator [] (const Key & key)
442 {
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;
446 }
447
455 [[nodiscard]] const Data & operator [] (const Key & key) const
456 {
457 return this->find(key);
458 }
459
465 [[nodiscard]] Data & operator [] (Key && key)
466 {
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;
470 }
471
479 [[nodiscard]] const Data & operator [] (Key && key) const
480 {
481 return this->find(std::move(key));
482 }
483
492 void remove_by_data(Data & data)
493 {
494 Base::remove_ptr(data_to_pair(&data));
495 }
496
503 void remove(const Key & key)
504 {
505 static_assert(std::is_default_constructible<Data>::value,
506 "MapOpenHash::remove() requires Data to be default-constructible");
507 Base::remove(Pair(key, Data()));
508 }
509
516 void remove(Key && key)
517 {
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()));
521 }
522
524 using Iterator = typename Base::Iterator;
525
531 {
532 return this->template maps<Key>([] (auto p) { return p.first; });
533 }
534
540 {
541 return this->template maps<Data>([] (auto p) { return p.second; });
542 }
543
551 {
553 for (Iterator it(*this); it.has_curr(); it.next_ne())
554 ret.append(&it.get_curr().second);
555 return ret;
556 }
557
565 {
567 for (Iterator it(*this); it.has_curr(); it.next_ne())
568 ret.append(&it.get_curr());
569 return ret;
570 }
571};
572
573
589template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
594
595
611template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
612struct MapODhash : public MapOpenHash<Key, Data, Cmp, ODhashTable>
613{
614 using MapOpenHash<Key, Data, Cmp, ODhashTable>::MapOpenHash;
615};
616
617} // end namespace Aleph
618
619#endif // TPL_DYNMAPOHASH_H
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:180
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:168
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
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:949
const float hash_default_upper_alpha
Definition hash-dry.C:40
const float hash_default_lower_alpha
Definition hash-dry.C:38
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.
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).
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.