Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Hashing.h
Go to the documentation of this file.
1
35#pragma once
36
37#include <ogdf/basic/basic.h>
38#include <ogdf/basic/memory.h>
39
40#include <cmath>
41#include <cstddef>
42#include <limits>
43#include <string>
44
45namespace ogdf {
46
48
53 friend class HashingBase;
54
56 size_t m_hashValue;
57
58public:
60 explicit HashElementBase(size_t hashValue) : m_next(nullptr), m_hashValue(hashValue) { }
61
63 HashElementBase* next() const { return m_next; }
64
66 size_t hashValue() const { return m_hashValue; }
67
69};
70
78protected:
84 int m_count;
86
87public:
89 explicit HashingBase(int minTableSize);
90
93
95 virtual ~HashingBase();
96
98 void resize(int newTableSize);
99
101 void insert(HashElementBase* pElement);
102
104 void del(HashElementBase* pElement);
105
107 void clear();
108
111
113 int size() const { return m_count; }
114
116 int empty() const { return m_count == 0; }
117
123 HashElementBase* firstListElement(size_t hashValue) const {
124 return *(m_table + (hashValue & m_hashMask));
125 }
126
136
147
148protected:
151
158 virtual void destroy(HashElementBase* pElement) = 0;
159
161 virtual HashElementBase* copy(HashElementBase* pElement) const = 0;
162
163private:
165 void init(int tableSize);
166
168 void copyAll(const HashingBase& H);
169};
170
178template<class K, class I>
182
183public:
185 HashElement(size_t hashValue, const K& key, const I& info)
187
190
192 const K& key() const { return m_key; }
193
195 const I& info() const { return m_info; }
196
198 I& info() { return m_info; }
199
201};
202
203
204template<class K, class I, class H>
205class HashConstIterator;
206
215template<class K>
217public:
219 size_t hash(const K& key) const { return size_t(key); }
220};
221
223template<>
224class DefHashFunc<void*> {
225public:
226 size_t hash(const void*& key) const { return size_t(key && 0xffffffff); }
227};
228
230template<>
231class DefHashFunc<double> {
232public:
233 size_t hash(const double& key) const {
234 int dummy;
235 return (size_t)((double)std::numeric_limits<size_t>::max() * frexp(key, &dummy));
236 }
237};
238
240template<>
241class DefHashFunc<string> {
242public:
243 size_t hash(const string& key) const;
244};
245
247
263template<class K, class I, class H = DefHashFunc<K>>
264class Hashing : private HashingBase {
265 friend class HashConstIterator<K, I, H>;
267
268public:
271
273 explicit Hashing(int minTableSize = 256, const H& hashFunc = H())
274 : HashingBase(minTableSize), m_hashFunc(hashFunc) { }
275
277 Hashing(const Hashing<K, I, H>& h) = default;
278
281
283 int size() const { return HashingBase::size(); }
284
286 bool empty() const { return HashingBase::size() == 0; }
287
289 bool member(const K& key) const { return lookup(key) != nullptr; }
290
293
295 HashElement<K, I>* lookup(const K& key) const {
297 for (; pElement; pElement = pElement->next()) {
298 if (pElement->key() == key) {
299 return pElement;
300 }
301 }
302
303 return nullptr;
304 }
305
307 Hashing<K, I, H>& operator=(const Hashing<K, I, H>& hashing) = default;
308
316 HashElement<K, I>* insert(const K& key, const I& info) {
317 HashElement<K, I>* pElement = lookup(key);
318
319 if (pElement) {
320 pElement->info() = info;
321 } else {
322 HashingBase::insert(pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info));
323 }
324
325 return pElement;
326 }
327
335 HashElement<K, I>* insertByNeed(const K& key, const I& info) {
336 HashElement<K, I>* pElement = lookup(key);
337
338 if (!pElement) {
339 HashingBase::insert(pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info));
340 }
341
342 return pElement;
343 }
344
351 HashElement<K, I>* fastInsert(const K& key, const I& info) {
352 HashElement<K, I>* pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info);
353 HashingBase::insert(pElement);
354 return pElement;
355 }
356
358 void del(const K& key) {
359 HashElement<K, I>* pElement = lookup(key);
360 if (pElement) {
361 HashingBase::del(pElement);
362 delete pElement;
363 }
364 }
365
368
369protected:
381
394
395private:
397 virtual void destroy(HashElementBase* pElement) override {
398 delete (HashElement<K, I>*)(pElement);
399 }
400
402 virtual HashElementBase* copy(HashElementBase* pElement) const override {
403 HashElement<K, I>* pX = (HashElement<K, I>*)(pElement);
404 return new HashElement<K, I>(pX->hashValue(), pX->key(), pX->info());
405 }
406};
407
431template<class K, class I, class H = DefHashFunc<K>>
436
437public:
439 HashConstIterator() : m_pElement(nullptr), m_pList(nullptr), m_pHashing(nullptr) { }
440
443 const Hashing<K, I, H>* pHashing)
444 : m_pElement(pElement), m_pList(pList), m_pHashing(pHashing) { }
445
449
453 m_pList = it.m_pList;
455 return *this;
456 }
457
459 bool valid() const { return m_pElement != nullptr; }
460
462 const K& key() const { return m_pElement->key(); }
463
465 const I& info() const { return m_pElement->info(); }
466
469 const HashConstIterator<K, I, H>& it2) {
470 return it1.m_pElement == it2.m_pElement;
471 }
472
475 const HashConstIterator<K, I, H>& it2) {
476 return it1.m_pElement != it2.m_pElement;
477 }
478
481 m_pElement = m_pHashing->nextElement(&m_pList, m_pElement);
482 return *this;
483 }
484};
485
486template<class K, class I, class H>
488 HashElement<K, I>*pElement, **pList;
489 pElement = firstElement(&pList);
490 return HashConstIterator<K, I, H>(pElement, pList, this);
491}
492
493}
Basic declarations, included by all source files.
size_t hash(const double &key) const
Definition Hashing.h:233
size_t hash(const string &key) const
size_t hash(const void *&key) const
Definition Hashing.h:226
Default hash functions.
Definition Hashing.h:216
size_t hash(const K &key) const
Returns the hash value of key.
Definition Hashing.h:219
Iterators for hash tables.
Definition Hashing.h:432
HashConstIterator(const HashConstIterator< K, I, H > &it)
Copy constructor.
Definition Hashing.h:447
HashConstIterator< K, I, H > & operator++()
Moves this hash iterator to the next element (iterator gets invalid if no more elements).
Definition Hashing.h:480
HashConstIterator & operator=(const HashConstIterator< K, I, H > &it)
Assignment operator.
Definition Hashing.h:451
bool valid() const
Returns true if the hash iterator points to an element.
Definition Hashing.h:459
HashConstIterator()
Creates a hash iterator pointing to no element.
Definition Hashing.h:439
const K & key() const
Returns the key of the hash element pointed to.
Definition Hashing.h:462
HashElement< K, I > * m_pElement
The hash element to which the iterator points.
Definition Hashing.h:433
friend bool operator==(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Equality operator.
Definition Hashing.h:468
HashElement< K, I > ** m_pList
The list containg the hash element.
Definition Hashing.h:434
const I & info() const
Returns the information of the hash element pointed to.
Definition Hashing.h:465
friend bool operator!=(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Inequality operator.
Definition Hashing.h:474
const Hashing< K, I, H > * m_pHashing
The associated hash table.
Definition Hashing.h:435
HashConstIterator(HashElement< K, I > *pElement, HashElement< K, I > **pList, const Hashing< K, I, H > *pHashing)
Creates a hash iterator pointing to element pElement in list pList of hash table pHashing.
Definition Hashing.h:442
Base class for elements within a hash table.
Definition Hashing.h:52
size_t m_hashValue
The hash value.
Definition Hashing.h:56
size_t hashValue() const
Returns the hash value of this element.
Definition Hashing.h:66
HashElementBase(size_t hashValue)
Creates a hash element with hash value hashValue.
Definition Hashing.h:60
HashElementBase * next() const
Returns the successor to this element in the list.
Definition Hashing.h:63
HashElementBase * m_next
The successor in the list.
Definition Hashing.h:55
Representation of elements in a hash table.
Definition Hashing.h:179
HashElement< K, I > * next() const
Returns the successor element in the list.
Definition Hashing.h:189
const K & key() const
Returns the key value.
Definition Hashing.h:192
HashElement(size_t hashValue, const K &key, const I &info)
Creates a hash element with given hash value, key, and information.
Definition Hashing.h:185
const I & info() const
Returns the information value.
Definition Hashing.h:195
K m_key
The key value.
Definition Hashing.h:180
I & info()
Returns a refeence to the information value.
Definition Hashing.h:198
I m_info
The information value.
Definition Hashing.h:181
Base class for hashing with chaining and table doubling.
Definition Hashing.h:77
void init(int tableSize)
Initializes the table for given table size.
HashElementBase ** m_table
The hash table (an array of lists).
Definition Hashing.h:85
void del(HashElementBase *pElement)
Removes the element pElement from the hash table.
virtual ~HashingBase()
Destruction.
HashElementBase * nextElement(HashElementBase ***pList, HashElementBase *pElement) const
Returns the successor of pElement in the list of all elements in the hash table.
HashingBase & operator=(const HashingBase &H)
Assignment operator.
HashElementBase * firstElement(HashElementBase ***pList) const
Returns the first element in the list of all elements in the hash table.
int m_tableSizeHigh
The maximal number of elements at this table size.
Definition Hashing.h:83
HashingBase(int minTableSize)
Creates a hash table with minimum table size minTableSize.
int m_count
The current number of elements.
Definition Hashing.h:84
void clear()
Removes all elements from the hash table.
void resize(int newTableSize)
Resizes the hash table to newTableSize.
int m_minTableSize
The minimal table size.
Definition Hashing.h:81
virtual void destroy(HashElementBase *pElement)=0
Called to delete hash element.
int size() const
Returns the number of elements in the hash table.
Definition Hashing.h:113
virtual HashElementBase * copy(HashElementBase *pElement) const =0
Called to create a copy of the element pElement.
int m_tableSize
The current table size.
Definition Hashing.h:79
int m_tableSizeLow
The minimal number of elements at this table size.
Definition Hashing.h:82
HashingBase(const HashingBase &H)
Copy constructor.
void insert(HashElementBase *pElement)
Inserts a new element pElement into the hash table.
int m_hashMask
The current table size minus one.
Definition Hashing.h:80
void copyAll(const HashingBase &H)
Copies all elements from H to this hash table.
void destroyAll()
Deletes all elements in hash table (but does not free m_table!).
int empty() const
Returns if the hash table is empty.
Definition Hashing.h:116
HashElementBase * firstListElement(size_t hashValue) const
Returns the first element in the list for elements with hash value hashValue.
Definition Hashing.h:123
Hashing with chaining and table doubling.
Definition Hashing.h:264
HashElement< K, I > * insert(const K &key, const I &info)
Inserts a new element with key key and information info into the hash table.
Definition Hashing.h:316
void del(const K &key)
Removes the element with key key from the hash table (does nothing if no such element).
Definition Hashing.h:358
Hashing(int minTableSize=256, const H &hashFunc=H())
Creates a hash table for given initial table size minTableSize.
Definition Hashing.h:273
~Hashing()
Destruction.
Definition Hashing.h:280
Hashing(const Hashing< K, I, H > &h)=default
Copy constructor.
virtual void destroy(HashElementBase *pElement) override
Deletes hash element pElement.
Definition Hashing.h:397
virtual HashElementBase * copy(HashElementBase *pElement) const override
Returns a copy of hash element pElement.
Definition Hashing.h:402
HashConstIterator< K, I, H > begin() const
Returns an hash iterator to the first element in the list of all elements.
Definition Hashing.h:487
void clear()
Removes all elements from the hash table.
Definition Hashing.h:367
HashElement< K, I > * fastInsert(const K &key, const I &info)
Inserts a new element with key key and information info into the hash table.
Definition Hashing.h:351
bool empty() const
Returns true iff the table is empty, i.e., contains no elements.
Definition Hashing.h:286
H m_hashFunc
The hash function.
Definition Hashing.h:266
Hashing< K, I, H > & operator=(const Hashing< K, I, H > &hashing)=default
Assignment operator.
HashElement< K, I > * insertByNeed(const K &key, const I &info)
Inserts a new element with key key and information info into the hash table.
Definition Hashing.h:335
int size() const
Returns the number of elements in the hash table.
Definition Hashing.h:283
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
Definition Hashing.h:289
HashElement< K, I > * firstElement(HashElement< K, I > ***pList) const
Returns the first element in the list of all elements in the hash table.
Definition Hashing.h:378
HashElement< K, I > * lookup(const K &key) const
Returns the hash element with key key in the hash table; returns nullptr if no such element exists.
Definition Hashing.h:295
HashElement< K, I > * nextElement(HashElement< K, I > ***pList, HashElement< K, I > *pElement) const
Returns the successor of pElement in the list of all elements in the hash table.
Definition Hashing.h:391
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition memory.h:85
Declaration of memory manager for allocating small pieces of memory.
The namespace for all OGDF objects.