Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 
41 namespace ogdf {
42 
43 class HashingBase;
44 
46 
51  friend class HashingBase;
52 
54  size_t m_hashValue;
55 
56 public:
58  explicit HashElementBase(size_t hashValue) : m_next(nullptr), m_hashValue(hashValue) { }
59 
61  HashElementBase *next() const { return m_next; }
62 
64  size_t hashValue() const { return m_hashValue; }
65 
67 };
68 
69 
77 protected:
79  int m_hashMask;
83  int m_count;
85 
86 public:
88  explicit HashingBase(int minTableSize);
89 
91  HashingBase(const HashingBase &H);
92 
94  virtual ~HashingBase();
95 
97  void resize(int newTableSize);
98 
100  void insert(HashElementBase *pElement);
101 
103  void del(HashElementBase *pElement);
104 
106  void clear();
107 
109  HashingBase &operator=(const HashingBase &H);
110 
112  int size() const { return m_count; }
113 
115  int empty() const { return m_count==0; }
116 
122  HashElementBase *firstListElement(size_t hashValue) const {
123  return *(m_table + (hashValue & m_hashMask));
124  }
125 
134  HashElementBase *firstElement(HashElementBase ***pList) const;
135 
145  HashElementBase *nextElement(HashElementBase ***pList,
146  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 
171 
179 template<class K, class I>
181 {
182  K m_key;
183  I m_info;
184 
185 public:
187  HashElement(size_t hashValue, const K &key, const I &info) :
188  HashElementBase(hashValue), m_key(key), m_info(info) { }
189 
193  }
194 
196  const K &key() const { return m_key; }
197 
199  const I &info() const { return m_info; }
200 
202  I &info() { return m_info; }
203 
205 };
206 
207 
208 template<class K, class I, class H> class HashConstIterator;
209 
218 template<class K> class DefHashFunc {
219 public:
221  size_t hash(const K &key) const { return size_t(key); }
222 };
223 
225 template<> class DefHashFunc<void *> {
226 public:
227  size_t hash(const void * &key) const { return size_t(key && 0xffffffff); }
228 };
229 
231 template<> class DefHashFunc<double> {
232 public:
233  size_t hash(const double &key) const {
234  int dummy;
235  return (size_t)((double)numeric_limits<size_t>::max() * frexp(key,&dummy));
236  }
237 };
238 
240 template<> class DefHashFunc<string> {
241 public:
242  size_t hash(const string &key) const;
243 };
244 
245 
247 
263 template<class K, class I, class H = DefHashFunc<K> >
264 class Hashing : private HashingBase
265 {
266  friend class HashConstIterator<K,I,H>;
268 
269 public:
272 
274  explicit Hashing(int minTableSize = 256, const H &hashFunc = H())
275  : HashingBase(minTableSize), m_hashFunc(hashFunc) { }
276 
278  Hashing(const Hashing<K,I> &h) : HashingBase(h) { }
279 
282 
284  int size() const { return HashingBase::size(); }
285 
287  bool empty() const { return HashingBase::size() == 0; }
288 
290  bool member(const K &key) const { return lookup(key) != nullptr; }
291 
293  HashConstIterator<K,I,H> begin() const;
294 
296  HashElement<K,I> *lookup(const K &key) const {
297  HashElement<K,I> *pElement =
298  (HashElement<K,I> *)firstListElement(m_hashFunc.hash(key));
299  for (; pElement; pElement = pElement->next())
300  if (pElement->key() == key) return pElement;
301 
302  return nullptr;
303  }
304 
307  HashingBase::operator =(hashing);
308  m_hashFunc = hashing.m_hashFunc;
309  return *this;
310  }
311 
319  HashElement<K,I> *insert(const K &key, const I &info) {
320  HashElement<K,I> *pElement = lookup(key);
321 
322  if (pElement)
323  pElement->info() = info;
324  else
325  HashingBase::insert(pElement =
326  new HashElement<K,I>(m_hashFunc.hash(key),key,info));
327 
328  return pElement;
329  }
330 
338  HashElement<K,I> *insertByNeed(const K &key, const I &info) {
339  HashElement<K,I> *pElement = lookup(key);
340 
341  if (!pElement)
342  HashingBase::insert(pElement = new HashElement<K,I>(m_hashFunc.hash(key),key,info));
343 
344  return pElement;
345  }
346 
353  HashElement<K,I> *fastInsert(const K &key, const I &info) {
354  HashElement<K,I> *pElement = new HashElement<K,I>(m_hashFunc.hash(key),key,info);
355  HashingBase::insert(pElement);
356  return pElement;
357  }
358 
360  void del(const K &key) {
361  HashElement<K,I> *pElement = lookup(key);
362  if (pElement) {
363  HashingBase::del(pElement);
364  delete pElement;
365  }
366  }
367 
369  void clear() { HashingBase::clear(); }
370 
371 protected:
382  }
383 
394  HashElement<K,I> *pElement) const
395  {
397  (HashElementBase ***)pList,pElement));
398  }
399 
400 private:
402  virtual void destroy(HashElementBase *pElement) override {
403  delete (HashElement<K,I> *)(pElement);
404  }
405 
407  virtual HashElementBase *copy(HashElementBase *pElement) const override {
408  HashElement<K,I> *pX = (HashElement<K,I> *)(pElement);
409  return new HashElement<K,I>(pX->hashValue(),pX->key(),pX->info());
410  }
411 };
412 
413 
437 template<class K, class I, class H = DefHashFunc<K> >
438 class HashConstIterator {
442 
443 public:
445  HashConstIterator() : m_pElement(nullptr), m_pList(nullptr), m_pHashing(nullptr) { }
446 
449  const Hashing<K,I,H> *pHashing) : m_pElement(pElement), m_pList(pList),
450  m_pHashing(pHashing) { }
451 
453  HashConstIterator(const HashConstIterator<K,I,H> &it) : m_pElement(it.m_pElement),
454  m_pList(it.m_pList), m_pHashing(it.m_pHashing) { }
455 
458  m_pElement = it.m_pElement;
459  m_pList = it.m_pList;
460  m_pHashing = it.m_pHashing;
461  return *this;
462  }
463 
465  bool valid() const { return m_pElement != nullptr; }
466 
468  const K &key() const { return m_pElement->key(); }
469 
471  const I &info() const { return m_pElement->info(); }
472 
474  friend bool operator==(const HashConstIterator<K,I,H> &it1,
475  const HashConstIterator<K,I,H> &it2) { return it1.m_pElement == it2.m_pElement; }
476 
478  friend bool operator!=(const HashConstIterator<K,I,H> &it1,
479  const HashConstIterator<K,I,H> &it2) { return it1.m_pElement != it2.m_pElement; }
480 
483  m_pElement = m_pHashing->nextElement(&m_pList,m_pElement);
484  return *this;
485  }
486 };
487 
488 
489 template<class K, class I, class H>
491 {
492  HashElement<K,I> *pElement, **pList;
493  pElement = firstElement(&pList);
494  return HashConstIterator<K,I,H>(pElement,pList,this);
495 }
496 
497 
498 } // end namespace ogdf
HashElement< K, I > * lookup(const K &key) const
Returns the hash element with key key in the hash table; returns 0 if no such element.
Definition: Hashing.h:296
HashElement< K, I > * next() const
Returns the successor element in the list.
Definition: Hashing.h:191
HashElement< K, I > * m_pElement
The hash element to which the iterator points.
Definition: Hashing.h:439
bool member(const K &key) const
Returns true iff the hash table contains an element with key key.
Definition: Hashing.h:290
Default hash functions.
Definition: Hashing.h:218
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:319
I & info()
Returns a refeence to the information value.
Definition: Hashing.h:202
const K & key() const
Returns the key of the hash element pointed to.
Definition: Hashing.h:468
HashElementBase * nextElement(HashElementBase ***pList, HashElementBase *pElement) const
Returns the successor of pElement in the list of all elements in the hash table.
Declaration of memory manager for allocating small pieces of memory.
HashConstIterator(const HashConstIterator< K, I, H > &it)
Copy constructor.
Definition: Hashing.h:453
HashingBase & operator=(const HashingBase &H)
Assignment operator.
size_t hashValue() const
Returns the hash value of this element.
Definition: Hashing.h:64
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
HashElementBase(size_t hashValue)
Creates a hash element with hash value hashValue.
Definition: Hashing.h:58
int m_count
The current number of elements.
Definition: Hashing.h:83
bool empty() const
Returns true iff the table is empty, i.e., contains no elements.
Definition: Hashing.h:287
const I & info() const
Returns the information of the hash element pointed to.
Definition: Hashing.h:471
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:393
HashConstIterator & operator=(const HashConstIterator< K, I, H > &it)
Assignment operator.
Definition: Hashing.h:457
const I & info() const
Returns the information value.
Definition: Hashing.h:199
HashElement< K, I > ** m_pList
The list containg the hash element.
Definition: Hashing.h:440
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:284
int size() const
Returns the number of elements in the hash table.
Definition: Hashing.h:112
int empty() const
Returns if the hash table is empty.
Definition: Hashing.h:115
Hashing(const Hashing< K, I > &h)
Copy constructor.
Definition: Hashing.h:278
void del(HashElementBase *pElement)
Removes the element pElement from the hash table.
HashElementBase * next() const
Returns the successor to this element in the list.
Definition: Hashing.h:61
int m_hashMask
The current table size minus one.
Definition: Hashing.h:79
const Hashing< K, I, H > * m_pHashing
The associated hash table.
Definition: Hashing.h:441
HashConstIterator< K, I, H > & operator++()
Moves this hash iterator to the next element (iterator gets invalid if no more elements).
Definition: Hashing.h:482
Base class for hashing with chaining and table doubling.
Definition: Hashing.h:76
HashElementBase * firstListElement(size_t hashValue) const
Returns the first element in the list for elements with hash value hashValue.
Definition: Hashing.h:122
HashConstIterator< K, I, H > begin() const
Returns an hash iterator to the first element in the list of all elements.
Definition: Hashing.h:490
Base class for elements within a hash table.
Definition: Hashing.h:50
Iterators for hash tables.
Definition: Hashing.h:208
static void copy(const T &from, T &to)
Definition: Alg.h:61
Hashing(int minTableSize=256, const H &hashFunc=H())
Creates a hash table for given initial table size minTableSize.
Definition: Hashing.h:274
Hashing< K, I > & operator=(const Hashing< K, I > &hashing)
Assignment operator.
Definition: Hashing.h:306
size_t hash(const void *&key) const
Definition: Hashing.h:227
I m_info
The information value.
Definition: Hashing.h:183
size_t m_hashValue
The hash value.
Definition: Hashing.h:54
size_t hash(const double &key) const
Definition: Hashing.h:233
H m_hashFunc
The hash function.
Definition: Hashing.h:267
friend bool operator!=(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Inequality operator.
Definition: Hashing.h:478
static uint32_t hash(uint32_t x)
Definition: Map.h:38
int m_minTableSize
The minimal table size.
Definition: Hashing.h:80
Representation of elements in a hash table.
Definition: Hashing.h:180
int m_tableSize
The current table size.
Definition: Hashing.h:78
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
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:338
virtual HashElementBase * copy(HashElementBase *pElement) const override
Returns a copy of hash element pElement.
Definition: Hashing.h:407
int m_tableSizeHigh
The maximal number of elements at this table size.
Definition: Hashing.h:82
HashElement(size_t hashValue, const K &key, const I &info)
Creates a hash element with given hash value, key, and information.
Definition: Hashing.h:187
friend bool operator==(const HashConstIterator< K, I, H > &it1, const HashConstIterator< K, I, H > &it2)
Equality operator.
Definition: Hashing.h:474
void insert(HashElementBase *pElement)
Inserts a new element pElement into the hash table.
Basic declarations, included by all source files.
HashElementBase * m_next
The successor in the list.
Definition: Hashing.h:53
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:448
~Hashing()
Destruction.
Definition: Hashing.h:281
K m_key
The key value.
Definition: Hashing.h:182
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:353
virtual void destroy(HashElementBase *pElement) override
Deletes hash element pElement.
Definition: Hashing.h:402
size_t hash(const K &key) const
Returns the hash value of key.
Definition: Hashing.h:221
void destroyAll()
Deletes all elements in hash table (but does not free m_table!).
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:380
HashElementBase ** m_table
The hash table (an array of lists).
Definition: Hashing.h:84
HashConstIterator()
Creates a hash iterator pointing to no element.
Definition: Hashing.h:445
Hashing with chaining and table doubling.
Definition: Hashing.h:264
The namespace for all OGDF objects.
HashElementBase * firstElement(HashElementBase ***pList) const
Returns the first element in the list of all elements in the hash table.
void clear()
Removes all elements from the hash table.
void clear()
Removes all elements from the hash table.
Definition: Hashing.h:369
const K & key() const
Returns the key value.
Definition: Hashing.h:196
bool valid() const
Returns true if the hash iterator points to an element.
Definition: Hashing.h:465
void del(const K &key)
Removes the element with key key from the hash table (does nothing if no such element).
Definition: Hashing.h:360
int m_tableSizeLow
The minimal number of elements at this table size.
Definition: Hashing.h:81