Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 
45 namespace ogdf {
46 
48 
53  friend class HashingBase;
54 
56  size_t m_hashValue;
57 
58 public:
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 
78 protected:
80  int m_hashMask;
84  int m_count;
86 
87 public:
89  explicit HashingBase(int minTableSize);
90 
92  HashingBase(const HashingBase& H);
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 
110  HashingBase& operator=(const HashingBase& H);
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 
135  HashElementBase* firstElement(HashElementBase*** pList) const;
136 
146  HashElementBase* nextElement(HashElementBase*** pList, HashElementBase* pElement) const;
147 
148 protected:
150  void destroyAll();
151 
158  virtual void destroy(HashElementBase* pElement) = 0;
159 
161  virtual HashElementBase* copy(HashElementBase* pElement) const = 0;
162 
163 private:
165  void init(int tableSize);
166 
168  void copyAll(const HashingBase& H);
169 };
170 
178 template<class K, class I>
179 class HashElement : public HashElementBase {
180  K m_key;
181  I m_info;
182 
183 public:
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 
204 template<class K, class I, class H>
206 
215 template<class K>
216 class DefHashFunc {
217 public:
219  size_t hash(const K& key) const { return size_t(key); }
220 };
221 
223 template<>
224 class DefHashFunc<void*> {
225 public:
226  size_t hash(const void*& key) const { return size_t(key && 0xffffffff); }
227 };
228 
230 template<>
231 class DefHashFunc<double> {
232 public:
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 
240 template<>
241 class DefHashFunc<string> {
242 public:
243  size_t hash(const string& key) const;
244 };
245 
247 
263 template<class K, class I, class H = DefHashFunc<K>>
264 class Hashing : private HashingBase {
265  friend class HashConstIterator<K, I, H>;
267 
268 public:
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 
367  void clear() { HashingBase::clear(); }
368 
369 protected:
380  }
381 
392  return (HashElement<K, I>*)(HashingBase::nextElement((HashElementBase***)pList, pElement));
393  }
394 
395 private:
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 
431 template<class K, class I, class H = DefHashFunc<K>>
432 class HashConstIterator {
436 
437 public:
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 
452  m_pElement = it.m_pElement;
453  m_pList = it.m_pList;
454  m_pHashing = it.m_pHashing;
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 
468  friend bool operator==(const HashConstIterator<K, I, H>& it1,
469  const HashConstIterator<K, I, H>& it2) {
470  return it1.m_pElement == it2.m_pElement;
471  }
472 
474  friend bool operator!=(const HashConstIterator<K, I, H>& it1,
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 
486 template<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 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::Hashing::begin
HashConstIterator< K, I, H > begin() const
Returns an hash iterator to the first element in the list of all elements.
Definition: Hashing.h:487
ogdf::DefHashFunc
Default hash functions.
Definition: geometry.h:50
ogdf::HashConstIterator::operator!=
friend bool operator!=(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Inequality operator.
Definition: Hashing.h:474
ogdf::HashingBase::empty
int empty() const
Returns if the hash table is empty.
Definition: Hashing.h:116
ogdf::Hashing::m_hashFunc
H m_hashFunc
The hash function.
Definition: Hashing.h:266
ogdf::HashingBase::firstListElement
HashElementBase * firstListElement(size_t hashValue) const
Returns the first element in the list for elements with hash value hashValue.
Definition: Hashing.h:123
ogdf::HashingBase::del
void del(HashElementBase *pElement)
Removes the element pElement from the hash table.
ogdf::Hashing::fastInsert
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
ogdf::Hashing::Hashing
Hashing(int minTableSize=256, const H &hashFunc=H())
Creates a hash table for given initial table size minTableSize.
Definition: Hashing.h:273
ogdf::HashingBase::m_tableSizeHigh
int m_tableSizeHigh
The maximal number of elements at this table size.
Definition: Hashing.h:83
ogdf::Hashing::operator=
Hashing< K, I, H > & operator=(const Hashing< K, I, H > &hashing)=default
Assignment operator.
ogdf::HashElementBase::m_hashValue
size_t m_hashValue
The hash value.
Definition: Hashing.h:56
ogdf::Hashing
Hashing with chaining and table doubling.
Definition: Hashing.h:264
ogdf::HashConstIterator::HashConstIterator
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
ogdf::HashElementBase::next
HashElementBase * next() const
Returns the successor to this element in the list.
Definition: Hashing.h:63
ogdf::HashingBase::firstElement
HashElementBase * firstElement(HashElementBase ***pList) const
Returns the first element in the list of all elements in the hash table.
ogdf::HashConstIterator::m_pElement
HashElement< K, I > * m_pElement
The hash element to which the iterator points.
Definition: Hashing.h:433
ogdf::Hashing::copy
virtual HashElementBase * copy(HashElementBase *pElement) const override
Returns a copy of hash element pElement.
Definition: Hashing.h:402
ogdf::HashElement::m_info
I m_info
The information value.
Definition: Hashing.h:181
ogdf::Hashing::~Hashing
~Hashing()
Destruction.
Definition: Hashing.h:280
ogdf::HashConstIterator::operator==
friend bool operator==(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Equality operator.
Definition: Hashing.h:468
ogdf::HashingBase::size
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:113
ogdf::HashingBase::m_count
int m_count
The current number of elements.
Definition: Hashing.h:84
ogdf::Hashing::destroy
virtual void destroy(HashElementBase *pElement) override
Deletes hash element pElement.
Definition: Hashing.h:397
ogdf::HashingBase::insert
void insert(HashElementBase *pElement)
Inserts a new element pElement into the hash table.
OGDF_NEW_DELETE
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
Definition: memory.h:85
ogdf::DefHashFunc::hash
size_t hash(const K &key) const
Returns the hash value of key.
Definition: Hashing.h:219
ogdf::HashConstIterator::HashConstIterator
HashConstIterator()
Creates a hash iterator pointing to no element.
Definition: Hashing.h:439
ogdf::DefHashFunc< double >::hash
size_t hash(const double &key) const
Definition: Hashing.h:233
ogdf::Hashing::firstElement
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
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::HashingBase::m_minTableSize
int m_minTableSize
The minimal table size.
Definition: Hashing.h:81
ogdf::HashingBase::m_hashMask
int m_hashMask
The current table size minus one.
Definition: Hashing.h:80
ogdf::HashConstIterator::valid
bool valid() const
Returns true if the hash iterator points to an element.
Definition: Hashing.h:459
ogdf::HashingBase::m_tableSizeLow
int m_tableSizeLow
The minimal number of elements at this table size.
Definition: Hashing.h:82
ogdf::HashElement::key
const K & key() const
Returns the key value.
Definition: Hashing.h:192
ogdf::Hashing::clear
void clear()
Removes all elements from the hash table.
Definition: Hashing.h:367
ogdf::DefHashFunc< void * >::hash
size_t hash(const void *&key) const
Definition: Hashing.h:226
ogdf::Hashing::size
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:283
ogdf::HashingBase::destroyAll
void destroyAll()
Deletes all elements in hash table (but does not free m_table!).
ogdf::HashingBase::m_tableSize
int m_tableSize
The current table size.
Definition: Hashing.h:79
ogdf::HashConstIterator::m_pList
HashElement< K, I > ** m_pList
The list containg the hash element.
Definition: Hashing.h:434
ogdf::Hashing::del
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
ogdf::HashConstIterator::key
const K & key() const
Returns the key of the hash element pointed to.
Definition: Hashing.h:462
ogdf::Hashing::lookup
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
ogdf::HashConstIterator::m_pHashing
const Hashing< K, I, H > * m_pHashing
The associated hash table.
Definition: Hashing.h:435
ogdf::Hashing::member
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
Definition: Hashing.h:289
ogdf::HashConstIterator
Iterators for hash tables.
Definition: Hashing.h:205
ogdf::Hashing::empty
bool empty() const
Returns true iff the table is empty, i.e., contains no elements.
Definition: Hashing.h:286
ogdf::HashElementBase::m_next
HashElementBase * m_next
The successor in the list.
Definition: Hashing.h:55
ogdf::HashConstIterator::HashConstIterator
HashConstIterator(const HashConstIterator< K, I, H > &it)
Copy constructor.
Definition: Hashing.h:447
ogdf::HashConstIterator::info
const I & info() const
Returns the information of the hash element pointed to.
Definition: Hashing.h:465
ogdf::graphics::init
void init()
Definition: graphics.h:450
ogdf::Hashing::nextElement
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
ogdf::HashingBase::m_table
HashElementBase ** m_table
The hash table (an array of lists).
Definition: Hashing.h:85
basic.h
Basic declarations, included by all source files.
ogdf::HashElement::HashElement
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
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::HashElement::m_key
K m_key
The key value.
Definition: Hashing.h:180
ogdf::HashElementBase::hashValue
size_t hashValue() const
Returns the hash value of this element.
Definition: Hashing.h:66
ogdf::HashingBase::clear
void clear()
Removes all elements from the hash table.
ogdf::HashConstIterator::operator++
HashConstIterator< K, I, H > & operator++()
Moves this hash iterator to the next element (iterator gets invalid if no more elements).
Definition: Hashing.h:480
ogdf::HashElement
Representation of elements in a hash table.
Definition: Hashing.h:179
ogdf::HashElement::info
const I & info() const
Returns the information value.
Definition: Hashing.h:195
ogdf::HashConstIterator::operator=
HashConstIterator & operator=(const HashConstIterator< K, I, H > &it)
Assignment operator.
Definition: Hashing.h:451
ogdf::HashElementBase
Base class for elements within a hash table.
Definition: Hashing.h:52
ogdf::Hashing::insertByNeed
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
ogdf::Hashing::insert
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
ogdf::HashingBase::nextElement
HashElementBase * nextElement(HashElementBase ***pList, HashElementBase *pElement) const
Returns the successor of pElement in the list of all elements in the hash table.
ogdf::HashingBase
Base class for hashing with chaining and table doubling.
Definition: Hashing.h:77
ogdf::HashElement::next
HashElement< K, I > * next() const
Returns the successor element in the list.
Definition: Hashing.h:189
memory.h
Declaration of memory manager for allocating small pieces of memory.
ogdf::HashElementBase::HashElementBase
HashElementBase(size_t hashValue)
Creates a hash element with hash value hashValue.
Definition: Hashing.h:60
ogdf::HashElement::info
I & info()
Returns a refeence to the information value.
Definition: Hashing.h:198