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 namespace ogdf {
41 
42 class HashingBase;
43 
45 
50  friend class HashingBase;
51 
53  size_t m_hashValue;
54 
55 public:
57  explicit HashElementBase(size_t hashValue) : m_next(nullptr), m_hashValue(hashValue) { }
58 
60  HashElementBase* next() const { return m_next; }
61 
63  size_t hashValue() const { return m_hashValue; }
64 
66 };
67 
75 protected:
77  int m_hashMask;
81  int m_count;
83 
84 public:
86  explicit HashingBase(int minTableSize);
87 
89  HashingBase(const HashingBase& H);
90 
92  virtual ~HashingBase();
93 
95  void resize(int newTableSize);
96 
98  void insert(HashElementBase* pElement);
99 
101  void del(HashElementBase* pElement);
102 
104  void clear();
105 
107  HashingBase& operator=(const HashingBase& H);
108 
110  int size() const { return m_count; }
111 
113  int empty() const { return m_count == 0; }
114 
120  HashElementBase* firstListElement(size_t hashValue) const {
121  return *(m_table + (hashValue & m_hashMask));
122  }
123 
132  HashElementBase* firstElement(HashElementBase*** pList) const;
133 
143  HashElementBase* nextElement(HashElementBase*** pList, HashElementBase* pElement) const;
144 
145 protected:
147  void destroyAll();
148 
155  virtual void destroy(HashElementBase* pElement) = 0;
156 
158  virtual HashElementBase* copy(HashElementBase* pElement) const = 0;
159 
160 private:
162  void init(int tableSize);
163 
165  void copyAll(const HashingBase& H);
166 };
167 
175 template<class K, class I>
176 class HashElement : public HashElementBase {
177  K m_key;
178  I m_info;
179 
180 public:
182  HashElement(size_t hashValue, const K& key, const I& info)
184 
187 
189  const K& key() const { return m_key; }
190 
192  const I& info() const { return m_info; }
193 
195  I& info() { return m_info; }
196 
198 };
199 
200 
201 template<class K, class I, class H>
203 
212 template<class K>
213 class DefHashFunc {
214 public:
216  size_t hash(const K& key) const { return size_t(key); }
217 };
218 
220 template<>
221 class DefHashFunc<void*> {
222 public:
223  size_t hash(const void*& key) const { return size_t(key && 0xffffffff); }
224 };
225 
227 template<>
228 class DefHashFunc<double> {
229 public:
230  size_t hash(const double& key) const {
231  int dummy;
232  return (size_t)((double)std::numeric_limits<size_t>::max() * frexp(key, &dummy));
233  }
234 };
235 
237 template<>
238 class DefHashFunc<string> {
239 public:
240  size_t hash(const string& key) const;
241 };
242 
244 
260 template<class K, class I, class H = DefHashFunc<K>>
261 class Hashing : private HashingBase {
262  friend class HashConstIterator<K, I, H>;
264 
265 public:
268 
270  explicit Hashing(int minTableSize = 256, const H& hashFunc = H())
271  : HashingBase(minTableSize), m_hashFunc(hashFunc) { }
272 
274  Hashing(const Hashing<K, I, H>& h) = default;
275 
278 
280  int size() const { return HashingBase::size(); }
281 
283  bool empty() const { return HashingBase::size() == 0; }
284 
286  bool member(const K& key) const { return lookup(key) != nullptr; }
287 
290 
292  HashElement<K, I>* lookup(const K& key) const {
294  for (; pElement; pElement = pElement->next()) {
295  if (pElement->key() == key) {
296  return pElement;
297  }
298  }
299 
300  return nullptr;
301  }
302 
304  Hashing<K, I, H>& operator=(const Hashing<K, I, H>& hashing) = default;
305 
313  HashElement<K, I>* insert(const K& key, const I& info) {
314  HashElement<K, I>* pElement = lookup(key);
315 
316  if (pElement) {
317  pElement->info() = info;
318  } else {
319  HashingBase::insert(pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info));
320  }
321 
322  return pElement;
323  }
324 
332  HashElement<K, I>* insertByNeed(const K& key, const I& info) {
333  HashElement<K, I>* pElement = lookup(key);
334 
335  if (!pElement) {
336  HashingBase::insert(pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info));
337  }
338 
339  return pElement;
340  }
341 
348  HashElement<K, I>* fastInsert(const K& key, const I& info) {
349  HashElement<K, I>* pElement = new HashElement<K, I>(m_hashFunc.hash(key), key, info);
350  HashingBase::insert(pElement);
351  return pElement;
352  }
353 
355  void del(const K& key) {
356  HashElement<K, I>* pElement = lookup(key);
357  if (pElement) {
358  HashingBase::del(pElement);
359  delete pElement;
360  }
361  }
362 
364  void clear() { HashingBase::clear(); }
365 
366 protected:
377  }
378 
389  return (HashElement<K, I>*)(HashingBase::nextElement((HashElementBase***)pList, pElement));
390  }
391 
392 private:
394  virtual void destroy(HashElementBase* pElement) override {
395  delete (HashElement<K, I>*)(pElement);
396  }
397 
399  virtual HashElementBase* copy(HashElementBase* pElement) const override {
400  HashElement<K, I>* pX = (HashElement<K, I>*)(pElement);
401  return new HashElement<K, I>(pX->hashValue(), pX->key(), pX->info());
402  }
403 };
404 
428 template<class K, class I, class H = DefHashFunc<K>>
429 class HashConstIterator {
433 
434 public:
436  HashConstIterator() : m_pElement(nullptr), m_pList(nullptr), m_pHashing(nullptr) { }
437 
440  const Hashing<K, I, H>* pHashing)
441  : m_pElement(pElement), m_pList(pList), m_pHashing(pHashing) { }
442 
446 
449  m_pElement = it.m_pElement;
450  m_pList = it.m_pList;
451  m_pHashing = it.m_pHashing;
452  return *this;
453  }
454 
456  bool valid() const { return m_pElement != nullptr; }
457 
459  const K& key() const { return m_pElement->key(); }
460 
462  const I& info() const { return m_pElement->info(); }
463 
465  friend bool operator==(const HashConstIterator<K, I, H>& it1,
466  const HashConstIterator<K, I, H>& it2) {
467  return it1.m_pElement == it2.m_pElement;
468  }
469 
471  friend bool operator!=(const HashConstIterator<K, I, H>& it1,
472  const HashConstIterator<K, I, H>& it2) {
473  return it1.m_pElement != it2.m_pElement;
474  }
475 
478  m_pElement = m_pHashing->nextElement(&m_pList, m_pElement);
479  return *this;
480  }
481 };
482 
483 template<class K, class I, class H>
485  HashElement<K, I>*pElement, **pList;
486  pElement = firstElement(&pList);
487  return HashConstIterator<K, I, H>(pElement, pList, this);
488 }
489 
490 }
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
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:484
ogdf::DefHashFunc
Default hash functions.
Definition: Hashing.h:213
ogdf::HashConstIterator::operator!=
friend bool operator!=(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Inequality operator.
Definition: Hashing.h:471
ogdf::HashingBase::empty
int empty() const
Returns if the hash table is empty.
Definition: Hashing.h:113
ogdf::Hashing::m_hashFunc
H m_hashFunc
The hash function.
Definition: Hashing.h:263
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:120
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:348
ogdf::Hashing::Hashing
Hashing(int minTableSize=256, const H &hashFunc=H())
Creates a hash table for given initial table size minTableSize.
Definition: Hashing.h:270
ogdf::HashingBase::m_tableSizeHigh
int m_tableSizeHigh
The maximal number of elements at this table size.
Definition: Hashing.h:80
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:53
ogdf::Hashing
Hashing with chaining and table doubling.
Definition: Hashing.h:261
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:439
ogdf::HashElementBase::next
HashElementBase * next() const
Returns the successor to this element in the list.
Definition: Hashing.h:60
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:430
ogdf::Hashing::copy
virtual HashElementBase * copy(HashElementBase *pElement) const override
Returns a copy of hash element pElement.
Definition: Hashing.h:399
ogdf::HashElement::m_info
I m_info
The information value.
Definition: Hashing.h:178
ogdf::Hashing::~Hashing
~Hashing()
Destruction.
Definition: Hashing.h:277
ogdf::HashConstIterator::operator==
friend bool operator==(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Equality operator.
Definition: Hashing.h:465
ogdf::HashingBase::size
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:110
ogdf::HashingBase::m_count
int m_count
The current number of elements.
Definition: Hashing.h:81
ogdf::Hashing::destroy
virtual void destroy(HashElementBase *pElement) override
Deletes hash element pElement.
Definition: Hashing.h:394
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:84
ogdf::DefHashFunc::hash
size_t hash(const K &key) const
Returns the hash value of key.
Definition: Hashing.h:216
ogdf::HashConstIterator::HashConstIterator
HashConstIterator()
Creates a hash iterator pointing to no element.
Definition: Hashing.h:436
ogdf::DefHashFunc< double >::hash
size_t hash(const double &key) const
Definition: Hashing.h:230
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:375
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:78
ogdf::HashingBase::m_hashMask
int m_hashMask
The current table size minus one.
Definition: Hashing.h:77
ogdf::HashConstIterator::valid
bool valid() const
Returns true if the hash iterator points to an element.
Definition: Hashing.h:456
ogdf::HashingBase::m_tableSizeLow
int m_tableSizeLow
The minimal number of elements at this table size.
Definition: Hashing.h:79
ogdf::HashElement::key
const K & key() const
Returns the key value.
Definition: Hashing.h:189
ogdf::Hashing::clear
void clear()
Removes all elements from the hash table.
Definition: Hashing.h:364
ogdf::DefHashFunc< void * >::hash
size_t hash(const void *&key) const
Definition: Hashing.h:223
ogdf::Hashing::size
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:280
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:76
ogdf::HashConstIterator::m_pList
HashElement< K, I > ** m_pList
The list containg the hash element.
Definition: Hashing.h:431
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:355
ogdf::HashConstIterator::key
const K & key() const
Returns the key of the hash element pointed to.
Definition: Hashing.h:459
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:292
ogdf::HashConstIterator::m_pHashing
const Hashing< K, I, H > * m_pHashing
The associated hash table.
Definition: Hashing.h:432
ogdf::Hashing::member
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
Definition: Hashing.h:286
ogdf::HashConstIterator
Iterators for hash tables.
Definition: Hashing.h:202
ogdf::Hashing::empty
bool empty() const
Returns true iff the table is empty, i.e., contains no elements.
Definition: Hashing.h:283
ogdf::HashElementBase::m_next
HashElementBase * m_next
The successor in the list.
Definition: Hashing.h:52
ogdf::HashConstIterator::HashConstIterator
HashConstIterator(const HashConstIterator< K, I, H > &it)
Copy constructor.
Definition: Hashing.h:444
ogdf::HashConstIterator::info
const I & info() const
Returns the information of the hash element pointed to.
Definition: Hashing.h:462
ogdf::graphics::init
void init()
Definition: graphics.h:446
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:388
ogdf::HashingBase::m_table
HashElementBase ** m_table
The hash table (an array of lists).
Definition: Hashing.h:82
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:182
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:177
ogdf::HashElementBase::hashValue
size_t hashValue() const
Returns the hash value of this element.
Definition: Hashing.h:63
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:477
ogdf::HashElement
Representation of elements in a hash table.
Definition: Hashing.h:176
ogdf::HashElement::info
const I & info() const
Returns the information value.
Definition: Hashing.h:192
ogdf::HashConstIterator::operator=
HashConstIterator & operator=(const HashConstIterator< K, I, H > &it)
Assignment operator.
Definition: Hashing.h:448
ogdf::HashElementBase
Base class for elements within a hash table.
Definition: Hashing.h:49
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:332
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:313
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:74
ogdf::HashElement::next
HashElement< K, I > * next() const
Returns the successor element in the list.
Definition: Hashing.h:186
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:57
ogdf::HashElement::info
I & info()
Returns a refeence to the information value.
Definition: Hashing.h:195