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
50using namespace Aleph;
51
52namespace Aleph
53{
88 template <typename Key, typename Record, class Cmp = Aleph::equal_to<Key>>
89 class DynLhashTable : public LhashTableVtl<Key>
90 {
91 private:
92 struct DLBucket : public LhashTableVtl<Key>::Bucket
93 {
95
96 DLBucket(const Key & key, const Record & _record)
97 : LhashTableVtl<Key>::Bucket(key), record(_record)
98 { /* Empty */
99 }
100
101 DLBucket(const Key & key, Record && _record)
102 : LhashTableVtl<Key>::Bucket(key), record(std::forward<Record>(_record))
103 { /* Empty */
104 }
105
106 DLBucket(Key && key, const Record & _record)
107 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)), record(_record)
108 { /* Empty */
109 }
110
111 DLBucket(Key && key, Record && _record)
112 : LhashTableVtl<Key>::Bucket(std::forward<Key>(key)),
113 record(std::forward<Record>(_record))
114 { /* Empty */
115 }
116 };
117
119 {
120 DLBucket *ret_val = 0;
121 size_t offset = (size_t) &ret_val->record;
122 return (DLBucket *) (((size_t) rec) - offset);
123 }
124
126 {
128 const Key & key;
130
131 public:
133 : table(_table), key(_key)
134 {
135 Record *record = table.search(key);
136 bucket = record not_eq nullptr ? record_to_bucket(record) : nullptr;
137 }
138
139 operator const Record &() const
140 {
142 << "access to unexisting entry";
143
144 return bucket->record;
145 }
146
147 DLProxy &operator =(const Record & record)
148 {
149 if (bucket != nullptr)
150 {
151 bucket->record = record;
152 return *this;
153 }
154
155 bucket = new DLBucket(key, record);
156 table.LhashTableVtl<Key>::insert(bucket);
157 return *this;
158 }
159
161 {
162 ah_invalid_argument_if(proxy.bucket == nullptr)
163 << "access to unexisting entry";
164
165 if (bucket != nullptr)
166 {
167 bucket->record = proxy.bucket->record;
168 return *this;
169 }
170
171 bucket = new DLBucket(key, proxy.bucket->record);
172 table.LhashTableVtl<Key>::insert(bucket);
173 return *this;
174 }
175 };
176
177 public:
180
184 void swap(DynLhashTable & table) noexcept
185 {
187 }
188
198 : LhashTableVtl<Key>(len, hash_fct)
199 {
200 // Empty
201 }
202
203 private:
205 {
206 for (typename LhashTableVtl<Key>::Iterator it(const_cast<DynLhashTable &>(table));
207 it.has_curr(); it.next_ne())
208 {
209 auto *Bucket = static_cast<DLBucket *>(it.get_curr());
210 insert(Bucket->get_key(), Bucket->record);
211 }
212 }
213
214 public:
220 : LhashTableVtl<Key>()
221 {
222 copy(table);
223 }
224
231 : LhashTableVtl<Key>(std::move(table))
232 {
233 // Base class move constructor properly transfers ownership
234 }
235
242 {
243 if (this == &table)
244 return *this;
245
246 this->empty();
247 copy(table);
248
249 return *this;
250 }
251
259 {
260 if (this != &table)
262 return *this;
263 }
264
265 private:
267 {
269 return &bucket->record;
270 }
271
272 public:
283 Record * insert(const Key & key, const Record & record)
284 {
285 return __insert(new DLBucket(key, record));
286 }
287
289 Record * insert(const Key & key, Record && record = Record())
290 {
291 return __insert(new DLBucket(key, std::forward<Record>(record)));
292 }
293
295 Record * insert(Key && key, const Record & record)
296 {
297 return __insert(new DLBucket(std::forward<Key>(key), record));
298 }
299
301 Record * insert(Key && key, Record && record)
302 {
303 return __insert(new DLBucket(std::forward<Key>(key),
304 std::forward<Record>(record)));
305 }
306
314 Record * search(const Key & key)
315 {
316 auto *bucket = static_cast<DLBucket *>(LhashTableVtl<Key>::search(key));
317 return bucket != nullptr ? &bucket->record : nullptr;
318 }
319
329 void remove(Record *record)
330 {
331 DLBucket *bucket = record_to_bucket(record);
333 delete bucket;
334 }
335
345 DLProxy operator [](const Key & key) const
346 {
347 return DLProxy(const_cast<DynLhashTable &>(*this), key);
348 }
349
351 DLProxy operator [](const Key & key)
352 {
353 return DLProxy(*this, key);
354 }
355 };
356} // end namespace Aleph
357
358# endif // TPL_DYNLHASH_H
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.
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...
DynLhashTable(size_t len=DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_fct< Key >)
Construct a dynamic hash table.
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:524
bool has_curr() const noexcept
Returns true if the iterator has a current bucket.
Definition tpl_lhash.H:706
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table.
Definition tpl_lhash.H:437
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:367
void empty() noexcept
Empties the hash table; frees memory of all buckets.
Definition tpl_lhash.H:292
void swap(GenLhashTable &other) noexcept
Definition tpl_lhash.H:230
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
Definition tpl_lhash.H:412
BucketList * table
Definition tpl_lhash.H:173
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
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.
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:828
Linear hashing with dynamic bucket expansion.