Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynLhash.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
44# ifndef TPL_DYNLHASH_H
45# define TPL_DYNLHASH_H
46
47# include <tpl_lhash.H>
48# include <ah-errors.H>
49# include <ah-concepts.H>
50
51using namespace Aleph;
52
53namespace Aleph
54{
89 template <typename Key, typename Record, class Cmp = Aleph::equal_to<Key>>
90 requires EqualityComparator<Cmp, Key>
91 class DynLhashTable : public LhashTableVtl<Key, Cmp>
92 {
94
95 private:
96 struct DLBucket : public LhashTableVtl<Key>::Bucket
97 {
99
100 DLBucket(const Key & key, const Record & _record)
101 : LhashTableVtl<Key>::Bucket(key), record(_record)
102 { /* Empty */
103 }
104
105 DLBucket(const Key & key, Record && _record)
106 : LhashTableVtl<Key>::Bucket(key), record(std::forward<Record>(_record))
107 { /* Empty */
108 }
109
110 DLBucket(Key && key, const Record & _record)
111 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)), record(_record)
112 { /* Empty */
113 }
114
115 DLBucket(Key && key, Record && _record)
116 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)),
117 record(std::forward<Record>(_record))
118 { /* Empty */
119 }
120 };
121
123 {
124 const size_t offset = __builtin_offsetof(DLBucket, record);
125 char * addr = reinterpret_cast<char *>(rec);
126 return reinterpret_cast<DLBucket *>(addr - offset);
127 }
128
130 {
132 const Key & key;
134
135 public:
136 DLProxy(DynLhashTable & _table, const Key & _key)
137 : table(_table), key(_key)
138 {
139 Record *record = table.search(key);
140 bucket = record not_eq nullptr ? record_to_bucket(record) : nullptr;
141 }
142
143 operator const Record &() const
144 {
146 << "access to unexisting entry";
147
148 return bucket->record;
149 }
150
151 DLProxy &operator =(const Record & record)
152 {
153 if (bucket != nullptr)
154 {
155 bucket->record = record;
156 return *this;
157 }
158
159 bucket = table.take(new DLBucket(key, record));
160 return *this;
161 }
162
163 DLProxy &operator =(const DLProxy & proxy)
164 {
165 ah_invalid_argument_if(proxy.bucket == nullptr)
166 << "access to unexisting entry";
167
168 if (bucket != nullptr)
169 {
170 bucket->record = proxy.bucket->record;
171 return *this;
172 }
173
174 bucket = table.take(new DLBucket(key, proxy.bucket->record));
175 return *this;
176 }
177 };
178
179 public:
182
186 void swap(DynLhashTable & table) noexcept
187 {
188 this->Base::swap(table);
189 }
190
199 Hash_Fct_Ptr hash_fct = dft_hash_ptr_fct<Key>)
200 : Base(len, hash_fct)
201 {
202 // Empty
203 }
204
205 private:
207 {
208 for (typename Base::Iterator it(const_cast<DynLhashTable &>(table));
209 it.has_curr(); it.next_ne())
210 {
211 auto *Bucket = static_cast<DLBucket *>(it.get_curr());
212 insert(Bucket->get_key(), Bucket->record);
213 }
214 }
215
216 public:
233
240 : Base(std::move(table))
241 {
242 // Base class move constructor properly transfers ownership
243 }
244
257 {
258 if (this != &table)
259 {
260 DynLhashTable other(table);
261 swap(other);
262 }
263
264 return *this;
265 }
266
274 {
275 if (this != &table)
276 Base::operator=(std::move(table));
277 return *this;
278 }
279
280 private:
281 // Puts in the table a bucket whose key it does not hold, and returns
282 // it. If the insertion throws before the bucket is in a list, without
283 // memory for the lists of a table that a move left without them, the
284 // bucket is freed
286 {
287 try
288 {
289 Base::insert(bucket);
290 }
291 catch (...)
292 {
294 throw;
295 }
296 return bucket;
297 }
298
299 // The table refuses a key that it holds already; the bucket is then
300 // freed, as when the insertion throws before it takes it
302 {
303 DLBucket *taken = nullptr;
304 try
305 {
306 taken = static_cast<DLBucket *>(Base::insert(bucket));
307 }
308 catch (...)
309 {
311 throw;
312 }
313
314 if (taken == nullptr)
315 {
316 delete bucket;
317 return nullptr;
318 }
319 return &bucket->record;
320 }
321
322 public:
334 Record * insert(const Key & key, const Record & record)
335 {
336 return __insert(new DLBucket(key, record));
337 }
338
340 Record * insert(const Key & key, Record && record = Record())
341 {
342 return __insert(new DLBucket(key, std::forward<Record>(record)));
343 }
344
346 Record * insert(Key && key, const Record & record)
347 {
348 return __insert(new DLBucket(std::forward<Key>(key), record));
349 }
350
352 Record * insert(Key && key, Record && record)
353 {
354 return __insert(new DLBucket(std::forward<Key>(key),
355 std::forward<Record>(record)));
356 }
357
365 Record * search(const Key & key)
366 {
367 auto *bucket = static_cast<DLBucket *>(Base::search(key));
368 return bucket != nullptr ? &bucket->record : nullptr;
369 }
370
380 void remove(Record *record)
381 {
382 DLBucket *bucket = record_to_bucket(record);
383 Base::remove(bucket);
384 delete bucket;
385 }
386
396 DLProxy operator [](const Key & key) const
397 {
398 return DLProxy(const_cast<DynLhashTable &>(*this), key);
399 }
400
402 DLProxy operator [](const Key & key)
403 {
404 return DLProxy(*this, key);
405 }
406 };
407} // end namespace Aleph
408
409# endif // TPL_DYNLHASH_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
DLProxy(DynLhashTable &_table, const Key &_key)
DLProxy & operator=(const Record &record)
Dynamic hash table mapping keys to records with separate chaining.
DynLhashTable(const DynLhashTable &table)
Copy constructor.
Record * search(const Key &key)
Search for a key in the table.
DLBucket * take(DLBucket *bucket)
DynLhashTable(DynLhashTable &&table) noexcept
Move constructor.
DynLhashTable(size_t len=DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >)
Construct a dynamic hash table.
void copy(const DynLhashTable &table)
Record * insert(const Key &key, Record &&record=Record())
This is an overloaded member function, provided for convenience. It differs from the above function o...
typename Base::Hash_Fct_Ptr Hash_Fct_Ptr
The type of hash function pointer.
void remove(Record *record)
Remove an entry from the table.
LhashTableVtl< Key, Cmp > Base
Record * insert(Key &&key, const Record &record)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynLhashTable & operator=(const DynLhashTable &table)
Copy assignment operator.
Record * insert(Key &&key, Record &&record)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Record * __insert(DLBucket *bucket)
void swap(DynLhashTable &table) noexcept
Swap contents with another table.
DLProxy operator[](const Key &key) const
Access or insert an entry using subscript notation.
Record * insert(const Key &key, const Record &record)
Insert a key-record pair into the table.
static DLBucket * record_to_bucket(Record *rec)
Iterator over a GenLhashTable hash table.
Definition tpl_lhash.H:711
bool has_curr() const noexcept
Returns true if the iterator has a current bucket.
Definition tpl_lhash.H:893
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table.
Definition tpl_lhash.H:587
GenLhashTable & operator=(const GenLhashTable &)=delete
Bucket * insert(Bucket *bucket)
Inserts bucket into the table and returns its address if the key is not already in the table; otherwi...
Definition tpl_lhash.H:503
Hash_Fct get_hash_fct() const noexcept
Definition tpl_lhash.H:436
size_t(*)(const Key &) Hash_Fct_Ptr
Definition tpl_lhash.H:192
const size_t & capacity() const noexcept
Returns the table capacity.
Definition tpl_lhash.H:694
void swap(GenLhashTable &other) noexcept
Swaps this table with other in constant time.
Definition tpl_lhash.H:276
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
Definition tpl_lhash.H:555
BucketList * table
Definition tpl_lhash.H:206
constexpr float get_upper_alpha() const noexcept
Returns the upper load factor threshold.
Definition hashDry.H:1564
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
Definition hashDry.H:1569
constexpr float get_lower_alpha() const noexcept
Returns the lower load factor threshold.
Definition hashDry.H:1559
void free_bucket_not_taken(Bucket *bucket) noexcept
Frees a bucket that a hash table of lists was given and did not take.
Definition hashDry.H:1336
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Table::Bucket Bucket
Definition lin-hash.cc:57
Main namespace for Aleph-w library functions.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
STL namespace.
DLBucket(Key &&key, const Record &_record)
DLBucket(Key &&key, Record &&_record)
DLBucket(const Key &key, Record &&_record)
DLBucket(const Key &key, const Record &_record)
Generic hash table with collision resolution by separate chaining and buckets with virtual destructor...
Definition tpl_lhash.H:1028
Hash tables with collision resolution by separate chaining.