libUTL++
Hashtable.h
1 #pragma once
2 
4 
5 #include <libutl/BitArray.h>
6 #include <libutl/Collection.h>
7 #include <libutl/PointerIntPair.h>
8 #include <libutl/Vector.h>
9 #include <libutl/dlist.h>
10 
12 
13 UTL_NS_BEGIN;
14 
16 
17 class HashFunction;
18 class HashtableIt;
19 
21 // HashFunction ////////////////////////////////////////////////////////////////////////////////////
23 
28 
31 {
32 public:
34  virtual ~HashFunction()
35  {
36  }
37 
39  virtual HashFunction* clone() const = 0;
40 
42  virtual size_t
43  hash(const Object* object, size_t size) const
44  {
45  return object->hash(size);
46  }
47 
48  size_t operator()(const Object* object, size_t size) const
49  {
50  return hash(object, size);
51  }
52 };
53 
55 // Hashtable ///////////////////////////////////////////////////////////////////////////////////////
57 
90 
92 class Hashtable : public Collection
93 {
95  friend class HashtableIt;
96 
97 public:
98  typedef HashtableIt iterator;
99 
100 public:
109  Hashtable(size_t size,
110  uint_t maxLF = 90,
111  bool owner = true,
112  bool multiSet = false,
113  const HashFunction* hfunc = nullptr)
114  {
115  init(size, maxLF, owner, multiSet, hfunc);
116  }
117 
124  Hashtable(bool owner, bool multiSet = false, const HashFunction* hfunc = nullptr)
125  {
126  init(0, 90, owner, multiSet, hfunc);
127  }
128 
129  virtual void steal(Object& rhs);
130 
131  virtual void vclone(const Object& rhs);
132 
133  virtual size_t innerAllocatedSize() const;
134 
136 
137 
138  void
140  {
141  delete _hashfn;
142  _hashfn = hashfn;
143  }
144 
146  void reserve(size_t newSize);
148 
150 
151  virtual Object* find(const Object& key) const;
152 
153  iterator findIt(const Object& key) const;
154 
155  iterator findIt(const Object& key);
156 
157  void
158  findIt(const Object& key, BidIt& it) const
159  {
160  const_cast_this->findIt(key, it);
161  IFDEBUG(it.setConst(true));
162  }
163 
164  virtual void findIt(const Object& key, BidIt& it);
166 
168 
169  bool
170  add(const Object& object)
171  {
172  return super::add(object);
173  }
174 
175  virtual bool add(const Object* object);
176 
177  void
178  add(const Collection& collection)
179  {
180  super::add(collection);
181  }
182 
183  Object*
184  addOrFind(const Object& object)
185  {
186  return super::addOrFind(object);
187  }
188 
189  virtual Object* addOrFind(const Object* object);
190 
191  bool
192  addOrUpdate(const Object& object)
193  {
194  return super::addOrUpdate(object);
195  }
196 
197  virtual bool addOrUpdate(const Object* object);
199 
201 
202  virtual void clear();
203 
208  void excise();
209 
210  bool
211  remove(const Object* key)
212  {
213  ASSERTD(key != nullptr);
214  return remove(*key);
215  }
216 
217  virtual bool remove(const Object& key);
218 
219  virtual void removeIt(BidIt& it);
221 
223 
224  iterator begin() const;
225 
226  iterator begin();
227 
228  virtual BidIt* beginNew() const;
229 
230  virtual BidIt* beginNew();
231 
232  inline iterator end() const;
233 
234  inline iterator end();
235 
236  virtual BidIt* endNew() const;
237 
238  virtual BidIt* endNew();
240 
241 private:
242  void init(size_t size = 0,
243  uint_t maxLF = 90,
244  bool owner = true,
245  bool multiSet = false,
246  const HashFunction* hashfn = nullptr);
247  void deInit();
248 
249  void grow(size_t newSize);
250 
251  void find(const Object& key, size_t& idx, ListNode*& node) const;
252 
253  void remove(size_t idx, ListNode* node);
254 
255  size_t
256  hash(const Object* object) const
257  {
258  ASSERTD(object != nullptr);
259  if (_hashfn == nullptr) return object->hash(_array.size());
260  return _hashfn->hash(object, _array.size());
261  }
262 
263  bool
264  isObject(size_t idx) const
265  {
266  return (_array[idx].getInt() == 0);
267  }
268 
269  auto getObject(size_t idx) const
270  {
271  ASSERTD(isObject(idx));
272  return _array[idx].getPointer();
273  }
274 
275  auto setObject(size_t idx, Object* obj)
276  {
277  _array[idx].set(obj, 0);
278  return obj;
279  }
280 
281  bool
282  isHead(size_t idx) const
283  {
284  return (_array[idx].getInt() == 1);
285  }
286 
287  ListNode*
288  getHead(size_t idx) const
289  {
290  ASSERTD(isHead(idx));
291  return reinterpret_cast<ListNode*>(_array[idx].getPointer());
292  }
293 
294  void
295  setHead(size_t idx, ListNode* ln)
296  {
297  _array[idx].set(reinterpret_cast<Object*>(ln), 1);
298  }
299 
300 private:
302 
303 private:
304  const HashFunction* _hashfn;
305  size_t _limit;
306  uint_t _maxLF;
307  Vector<pip_t> _array;
308  static size_t primes[];
309 };
310 
312 
313 UTL_NS_END;
314 
316 
317 #include <libutl/THashtable.h>
List node.
Definition: ListNode.h:28
virtual size_t hash(const Object *object, size_t size) const
Compute the hash function for the given object.
Definition: Hashtable.h:43
T * clone(const T *object)
Create a clone of the given object.
Definition: util_inl.h:404
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
void setHashFunction(const HashFunction *hashfn)
Set the hash function.
Definition: Hashtable.h:139
void deInit()
De-initialize UTL++.
(Pointer,Int) pair.
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Templated proxy for BidIt.
Definition: TBidIt.h:19
#define IFDEBUG(x)
Do x in DEBUG mode only.
virtual ~HashFunction()
Virtual destructor.
Definition: Hashtable.h:34
Hashtable(size_t size, uint_t maxLF=90, bool owner=true, bool multiSet=false, const HashFunction *hfunc=nullptr)
Constructor.
Definition: Hashtable.h:109
Hash function.
Definition: Hashtable.h:30
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Bi-directional Hashtable iterator.
Definition: HashtableIt.h:22
void set(const Object *object)
Set the contained object.
Definition: ListNode.h:115
Hashtable(bool owner, bool multiSet=false, const HashFunction *hfunc=nullptr)
Constructor.
Definition: Hashtable.h:124
Bi-directional iterator abstraction.
Definition: BidIt.h:25
Chained hashing collection.
Definition: Hashtable.h:92
A sequence of same-type objects.
Definition: Ordering.h:14
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
A collection of objects.
Definition: Collection.h:62