Aleph-w 3.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>>
91 class DynLhashTable : public LhashTableVtl<Key>
92 {
93 private:
94 struct DLBucket : public LhashTableVtl<Key>::Bucket
95 {
97
98 DLBucket(const Key & key, const Record & _record)
99 : LhashTableVtl<Key>::Bucket(key), record(_record)
100 { /* Empty */
101 }
102
103 DLBucket(const Key & key, Record && _record)
104 : LhashTableVtl<Key>::Bucket(key), record(std::forward<Record>(_record))
105 { /* Empty */
106 }
107
108 DLBucket(Key && key, const Record & _record)
109 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)), record(_record)
110 { /* Empty */
111 }
112
113 DLBucket(Key && key, Record && _record)
114 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)),
115 record(std::forward<Record>(_record))
116 { /* Empty */
117 }
118 };
119
121 {
122 DLBucket *ret_val = 0;
123 size_t offset = (size_t) &ret_val->record;
124 return (DLBucket *) (((size_t) rec) - offset);
125 }
126
128 {
130 const Key & key;
132
133 public:
135 : table(_table), key(_key)
136 {
137 Record *record = table.search(key);
138 bucket = record not_eq nullptr ? record_to_bucket(record) : nullptr;
139 }
140
141 operator const Record &() const
142 {
144 << "access to unexisting entry";
145
146 return bucket->record;
147 }
148
149 DLProxy &operator =(const Record & record)
150 {
151 if (bucket != nullptr)
152 {
153 bucket->record = record;
154 return *this;
155 }
156
157 bucket = new DLBucket(key, record);
158 table.LhashTableVtl<Key>::insert(bucket);
159 return *this;
160 }
161
163 {
164 ah_invalid_argument_if(proxy.bucket == nullptr)
165 << "access to unexisting entry";
166
167 if (bucket != nullptr)
168 {
169 bucket->record = proxy.bucket->record;
170 return *this;
171 }
172
173 bucket = new DLBucket(key, proxy.bucket->record);
174 table.LhashTableVtl<Key>::insert(bucket);
175 return *this;
176 }
177 };
178
179 public:
182
186 void swap(DynLhashTable & table) noexcept
187 {
189 }
190
200 : LhashTableVtl<Key>(len, hash_fct)
201 {
202 // Empty
203 }
204
205 private:
207 {
208 for (typename LhashTableVtl<Key>::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:
222 : LhashTableVtl<Key>()
223 {
224 copy(table);
225 }
226
233 : LhashTableVtl<Key>(std::move(table))
234 {
235 // Base class move constructor properly transfers ownership
236 }
237
244 {
245 if (this == &table)
246 return *this;
247
248 this->empty();
249 copy(table);
250
251 return *this;
252 }
253
261 {
262 if (this != &table)
264 return *this;
265 }
266
267 private:
269 {
271 return &bucket->record;
272 }
273
274 public:
285 Record * insert(const Key & key, const Record & record)
286 {
287 return __insert(new DLBucket(key, record));
288 }
289
291 Record * insert(const Key & key, Record && record = Record())
292 {
293 return __insert(new DLBucket(key, std::forward<Record>(record)));
294 }
295
297 Record * insert(Key && key, const Record & record)
298 {
299 return __insert(new DLBucket(std::forward<Key>(key), record));
300 }
301
303 Record * insert(Key && key, Record && record)
304 {
305 return __insert(new DLBucket(std::forward<Key>(key),
306 std::forward<Record>(record)));
307 }
308
316 Record * search(const Key & key)
317 {
318 auto *bucket = static_cast<DLBucket *>(LhashTableVtl<Key>::search(key));
319 return bucket != nullptr ? &bucket->record : nullptr;
320 }
321
331 void remove(Record *record)
332 {
333 DLBucket *bucket = record_to_bucket(record);
335 delete bucket;
336 }
337
347 DLProxy operator [](const Key & key) const
348 {
349 return DLProxy(const_cast<DynLhashTable &>(*this), key);
350 }
351
353 DLProxy operator [](const Key & key)
354 {
355 return DLProxy(*this, key);
356 }
357 };
358} // end namespace Aleph
359
360# endif // TPL_DYNLHASH_H
C++20 concepts for constraining comparison functors.
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:639
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.
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...
void remove(Record *record)
Remove an entry from the table.
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.
typename DynLhashTable< Key, Record >::Hash_Fct_Ptr Hash_Fct_Ptr
The type of hash function pointer.
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:541
bool has_curr() const noexcept
Returns true if the iterator has a current bucket.
Definition tpl_lhash.H:723
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table.
Definition tpl_lhash.H:454
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:384
void empty() noexcept
Empties the hash table; frees memory of all buckets.
Definition tpl_lhash.H:294
void swap(GenLhashTable &other) noexcept
Definition tpl_lhash.H:232
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
Definition tpl_lhash.H:429
BucketList * table
Definition tpl_lhash.H:175
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Table::Bucket Bucket
Definition lin-hash.cc:54
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:858
Linear hashing with dynamic bucket expansion.