forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashcontainer.hh
679 lines (576 loc) · 20.9 KB
/
hashcontainer.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef CLICK_HASHCONTAINER_HH
#define CLICK_HASHCONTAINER_HH
#include "hashcode.hh"
#if CLICK_DEBUG_HASHMAP
# define click_hash_assert(x) assert(x)
#else
# define click_hash_assert(x)
#endif
template <typename T> class HashContainer_adapter;
template <typename T, typename A = HashContainer_adapter<T> > class HashContainer_const_iterator;
template <typename T, typename A = HashContainer_adapter<T> > class HashContainer_iterator;
template <typename T, typename A = HashContainer_adapter<T> > class HashContainer;
template <typename T, typename A>
class HashContainer_rep : public A {
T **buckets;
size_t nbuckets;
size_t size;
mutable size_t first_bucket;
friend class HashContainer<T, A>;
friend class HashContainer_const_iterator<T, A>;
friend class HashContainer_iterator<T, A>;
};
template <typename T>
class HashContainer_adapter { public:
typedef typename T::key_type key_type;
typedef typename T::key_const_reference key_const_reference;
static T *&hashnext(T *e) {
return e->_hashnext;
}
static key_const_reference hashkey(const T *e) {
return e->hashkey();
}
static bool hashkeyeq(const key_type &a, const key_type &b) {
return a == b;
}
};
/** @class HashContainer
@brief Intrusive hash table template.
The HashContainer template implements a hash table or associative array
suitable for use in the kernel or at user level.
HashContainer is <em>intrusive.</em> This means it does not manage its
contents' memory. While non-intrusive containers are more common in the
STL, intrusive containers make it simple to store objects in more than one
container at a time.
Unlike many hash tables HashContainer does not automatically grow itself to
maintain good lookup performance. Its users are expected to call rehash()
when appropriate. See unbalanced().
With the default adapter type (A), the template type T must:
<ul>
<li>Define a "key_type" type that supports equality.</li>
<li>Define a "key_const_reference" type, usually the same as "key_type."</li>
<li>Contain a member "T *_hashnext" accessible to HashContainer_adapter<T>.</li>
<li>Define a "hashkey()" member function that returns the relevant hash key.
This function must have return type "key_const_reference."</li>
</ul>
These requirements can be changed by supplying a different A, or adapter,
type.
HashContainer can store multiple elements with the same key, although this
is not the normal use. An element stored in a HashContainer should not
modify its key.
HashContainer is used to implement Click's HashTable template.
*/
template <typename T, typename A>
class HashContainer { public:
/** @brief Key type. */
typedef typename A::key_type key_type;
/** @brief Value type.
*
* Must meet the HashContainer requirements defined by type A. */
typedef T value_type;
/** @brief Type of sizes. */
typedef size_t size_type;
enum {
#if CLICK_LINUXMODULE
max_bucket_count = 4194303,
#else
max_bucket_count = (size_t) -1,
#endif
initial_bucket_count = 63
};
/** @brief Construct an empty HashContainer. */
HashContainer();
/** @brief Construct an empty HashContainer with at least @a n buckets. */
explicit HashContainer(size_type n);
/** @brief Destroy the HashContainer. */
~HashContainer();
/** @brief Return the number of elements stored. */
inline size_type size() const {
return _rep.size;
}
/** @brief Return true iff size() == 0. */
inline bool empty() const {
return _rep.size == 0;
}
/** @brief Return the number of buckets. */
inline size_type bucket_count() const {
return _rep.nbuckets;
}
/** @brief Return the number of elements in bucket @a n. */
inline size_type bucket_size(size_type n) const {
click_hash_assert(n < _rep.nbuckets);
size_type s = 0;
for (T *element = _rep.buckets[n]; element; element = _rep.hashnext(element))
++s;
return s;
}
/** @brief Return the bucket number containing elements with @a key. */
size_type bucket(const key_type &key) const;
/** @brief Return true if this HashContainer should be rebalanced. */
inline bool unbalanced() const {
return _rep.size > 2 * _rep.nbuckets && _rep.nbuckets < max_bucket_count;
}
typedef HashContainer_const_iterator<T, A> const_iterator;
typedef HashContainer_iterator<T, A> iterator;
/** @brief Return an iterator for the first element in the container.
*
* @note HashContainer iterators return elements in random order. */
inline iterator begin();
/** @overload */
inline const_iterator begin() const;
/** @brief Return an iterator for the end of the container.
* @invariant end().live() == false */
inline iterator end();
/** @overload */
inline const_iterator end() const;
/** @brief Return an iterator for the first element in bucket @a n. */
inline iterator begin(size_type n);
/** @brief Return an iterator for an element with @a key, if any.
*
* If no element with @a key exists in the table, find() returns an
* iterator that compares equal to end(). However, this iterator is
* special, and can also be used to efficiently insert an element with key
* @a key. In particular, the return value of find() always has
* can_insert(), and can thus be passed to insert_at() or set(). (It
* will insert elements at the head of the relevant bucket.) */
inline iterator find(const key_type &key);
/** @overload */
inline const_iterator find(const key_type &key) const;
/** @brief Return an iterator for an element with key @a key, if any.
*
* Like find(), but additionally moves any found element to the head of
* its bucket, possibly speeding up future lookups. */
inline iterator find_prefer(const key_type &key);
/** @brief Return an element for @a key, if any.
*
* Returns null if no element for @a key currently exists. Equivalent
* to find(key).get(). */
inline T *get(const key_type &key) const;
/** @brief Insert an element at position @a it.
* @param it iterator
* @param element element
* @pre @a it.can_insert()
* @pre @a it.bucket() == bucket(@a element->hashkey())
* @pre @a element != NULL
* @pre @a element is not already in the HashContainer
*
* Inserts @a element at the position in the hash table indicated by @a
* it. For instance, if @a it == begin(@em n) for some bucket number @em
* n, then @a element becomes the first element in bucket @em n. Other
* elements in the bucket, if any, are chained along.
*
* On return, @a it is updated to point immediately after @a element.
* If @a it was not live before, then it will not be live after.
*
* @note HashContainer never automatically rehashes itself, so element
* insertion leaves any existing iterators valid. For best performance,
* however, users must call balance() to resize the container when it
* becomes unbalanced(). */
inline void insert_at(iterator &it, T *element);
/** @brief Replace the element at position @a it with @a element.
* @param it iterator
* @param element element (can be null)
* @param balance whether to balance the hash table
* @return the previous value of it.get()
* @pre @a it.can_insert()
* @pre @a it.bucket() == bucket(@a element->hashkey())
* @pre @a element is not already in the HashContainer
*
* Replaces the element pointed to by @a it with @a element, and returns
* the former element. If @a element is null the former element is
* removed. If there is no former element then @a element is inserted.
* When inserting an element with @a balance true, set() may rebalance the
* hash table.
*
* As a side effect, @a it is advanced to point at the newly inserted @a
* element. If @a element is null, then @a it is advanced to point at the
* next element as by ++@a it. */
T *set(iterator &it, T *element, bool balance = false);
/** @brief Replace the element with @a element->hashkey() with @a element.
* @param element element
* @return the previous value of find(@a element->hashkey()).get()
* @pre @a element is not already in the HashContainer
*
* Finds an element with the same hashkey as @a element, removes it from
* the HashContainer, and replaces it with @a element. If there is no
* former element then @a element is inserted. */
inline T *set(T *element);
/** @brief Remove the element at position @a it.
* @param it iterator
* @return the previous value of it.get()
*
* As a side effect, @a it is advanced to the next element as by ++@a it. */
inline T *erase(iterator &it);
/** @brief Remove an element with hashkey @a key.
* @return the element removed, if any
*
* Roughly equivalent to erase(find(key)). */
inline T *erase(const key_type &key);
/** @brief Removes all elements from the container.
* @post size() == 0 */
void clear();
/** @brief Swaps the contents of *this and @a x. */
inline void swap(HashContainer<T, A> &x);
/** @brief Rehash the table, ensuring it contains at least @a n buckets.
*
* If @a n < bucket_count(), this function may make the hash table
* slower.
*
* @note Rehashing invalidates all existing iterators. */
void rehash(size_type n);
/** @brief Rehash the table if it is unbalanced.
*
* @note Rehashing invalidates all existing iterators. */
inline void balance() {
if (unbalanced())
rehash(bucket_count() + 1);
}
private:
HashContainer_rep<T, A> _rep;
HashContainer(const HashContainer<T, A> &);
HashContainer<T, A> &operator=(const HashContainer<T, A> &);
friend class HashContainer_iterator<T, A>;
friend class HashContainer_const_iterator<T, A>;
};
/** @class HashContainer_const_iterator
* @brief The const_iterator type for HashContainer. */
template <typename T, typename A>
class HashContainer_const_iterator { public:
typedef typename HashContainer<T, A>::size_type size_type;
/** @brief Construct an uninitialized iterator. */
HashContainer_const_iterator() {
}
/** @brief Return a pointer to the element, null if *this == end(). */
T *get() const {
return _element;
}
/** @brief Return a pointer to the element, null if *this == end(). */
T *operator->() const {
return _element;
}
/** @brief Return a reference to the element.
* @pre *this != end() */
T &operator*() const {
return *_element;
}
/** @brief Return true iff *this != end(). */
inline bool live() const {
return _element;
}
typedef T *(HashContainer_const_iterator::*unspecified_bool_type)() const;
/** @brief Return true iff *this != end(). */
inline operator unspecified_bool_type() const {
return _element ? &HashContainer_const_iterator::get : 0;
}
/** @brief Return the corresponding HashContainer. */
const HashContainer<T, A> *hashcontainer() const {
return _hc;
}
/** @brief Return the bucket number this iterator is in. */
size_type bucket() const {
return _bucket;
}
/** @brief Advance this iterator to the next element. */
void operator++() {
if (_element && _hc->_rep.hashnext(_element)) {
_pprev = &_hc->_rep.hashnext(_element);
_element = *_pprev;
} else if (_bucket != _hc->_rep.nbuckets) {
for (++_bucket; _bucket != _hc->_rep.nbuckets; ++_bucket)
if (*(_pprev = &_hc->_rep.buckets[_bucket])) {
_element = *_pprev;
return;
}
_element = 0;
}
}
/** @brief Advance this iterator to the next element. */
void operator++(int) {
++*this;
}
private:
T *_element;
T **_pprev;
size_type _bucket;
const HashContainer<T, A> *_hc;
inline HashContainer_const_iterator(const HashContainer<T, A> *hc)
: _hc(hc) {
_bucket = hc->_rep.first_bucket;
_pprev = &hc->_rep.buckets[_bucket];
if (unlikely(_bucket == hc->_rep.nbuckets))
_element = 0;
else if (!(_element = *_pprev)) {
(*this)++;
hc->_rep.first_bucket = _bucket;
}
}
inline HashContainer_const_iterator(const HashContainer<T, A> *hc, size_type b, T **pprev, T *element)
: _element(element), _pprev(pprev), _bucket(b), _hc(hc) {
click_hash_assert((!_pprev && !_element) || *_pprev == _element);
}
friend class HashContainer<T, A>;
friend class HashContainer_iterator<T, A>;
};
/** @class HashContainer_iterator
@brief The iterator type for HashContainer. */
template <typename T, typename A>
class HashContainer_iterator : public HashContainer_const_iterator<T, A> { public:
typedef HashContainer_const_iterator<T, A> inherited;
/** @brief Construct an uninitialized iterator. */
HashContainer_iterator() {
}
/** @brief Return true iff elements can be inserted here.
*
* Specifically, returns true iff this iterator is valid to pass to
* HashContainer::insert_at() or HashContainer::set(). All live()
* iterators can_insert(), but some !live() iterators can_insert() as
* well. */
bool can_insert() const {
return this->_bucket < this->_hc->bucket_count();
}
/** @brief Return the corresponding HashContainer. */
HashContainer<T, A> *hashcontainer() const {
return const_cast<HashContainer<T, A> *>(this->_hc);
}
private:
inline HashContainer_iterator(HashContainer<T, A> *hc)
: inherited(hc) {
}
inline HashContainer_iterator(HashContainer<T, A> *hc, typename inherited::size_type b, T **pprev, T *element)
: inherited(hc, b, pprev, element) {
}
friend class HashContainer<T, A>;
};
template <typename T, typename A>
HashContainer<T, A>::HashContainer()
{
_rep.size = 0;
_rep.nbuckets = initial_bucket_count;
_rep.buckets = new T *[_rep.nbuckets];
_rep.first_bucket = _rep.nbuckets;
for (size_type b = 0; b < _rep.nbuckets; ++b)
_rep.buckets[b] = 0;
}
template <typename T, typename A>
HashContainer<T, A>::HashContainer(size_type nb)
{
size_type b = 1;
while (b < nb && b < max_bucket_count)
b = ((b + 1) << 1) - 1;
_rep.size = 0;
_rep.nbuckets = b;
_rep.buckets = new T *[_rep.nbuckets];
_rep.first_bucket = _rep.nbuckets;
for (b = 0; b < _rep.nbuckets; ++b)
_rep.buckets[b] = 0;
}
template <typename T, typename A>
HashContainer<T, A>::~HashContainer()
{
delete[] _rep.buckets;
}
template <typename T, typename A>
inline typename HashContainer<T, A>::size_type
HashContainer<T, A>::bucket(const key_type &key) const
{
return ((size_type) hashcode(key)) % _rep.nbuckets;
}
template <typename T, typename A>
inline typename HashContainer<T, A>::const_iterator
HashContainer<T, A>::begin() const
{
return const_iterator(this);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::iterator
HashContainer<T, A>::begin()
{
return iterator(this);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::const_iterator
HashContainer<T, A>::end() const
{
return const_iterator(this, -1, 0, 0);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::iterator
HashContainer<T, A>::end()
{
return iterator(this, -1, 0, 0);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::iterator
HashContainer<T, A>::begin(size_type b)
{
click_hash_assert(b < _rep.nbuckets);
return iterator(this, b, &_rep.buckets[b], _rep.buckets[b]);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::iterator
HashContainer<T, A>::find(const key_type &key)
{
size_type b = bucket(key);
T **pprev;
for (pprev = &_rep.buckets[b]; *pprev; pprev = &_rep.hashnext(*pprev))
if (_rep.hashkeyeq(_rep.hashkey(*pprev), key))
return iterator(this, b, pprev, *pprev);
return iterator(this, b, &_rep.buckets[b], 0);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::const_iterator
HashContainer<T, A>::find(const key_type &key) const
{
return const_cast<HashContainer<T, A> *>(this)->find(key);
}
template <typename T, typename A>
inline typename HashContainer<T, A>::iterator
HashContainer<T, A>::find_prefer(const key_type &key)
{
size_type b = bucket(key);
T **pprev;
for (pprev = &_rep.buckets[b]; *pprev; pprev = &_rep.hashnext(*pprev))
if (_rep.hashkeyeq(_rep.hashkey(*pprev), key)) {
T *element = *pprev;
*pprev = _rep.hashnext(element);
_rep.hashnext(element) = _rep.buckets[b];
_rep.buckets[b] = element;
return iterator(this, b, &_rep.buckets[b], element);
}
return iterator(this, b, &_rep.buckets[b], 0);
}
template <typename T, typename A>
inline T *HashContainer<T, A>::get(const key_type &key) const
{
return find(key).get();
}
template <typename T, typename A>
T *HashContainer<T, A>::set(iterator &it, T *element, bool balance)
{
click_hash_assert(it._hc == this && it._bucket < _rep.nbuckets);
click_hash_assert(bucket(_rep.hashkey(element)) == it._bucket);
click_hash_assert(!it._element || _rep.hashkeyeq(_rep.hashkey(element), _rep.hashkey(it._element)));
T *old = it.get();
if (unlikely(old == element))
return old;
if (!element) {
--_rep.size;
if (!(*it._pprev = it._element = _rep.hashnext(old)))
++it;
return old;
}
if (old)
_rep.hashnext(element) = _rep.hashnext(old);
else {
++_rep.size;
if (unlikely(unbalanced()) && balance) {
rehash(bucket_count() + 1);
it._bucket = bucket(_rep.hashkey(element));
it._pprev = &_rep.buckets[it._bucket];
}
if (!(_rep.hashnext(element) = *it._pprev))
_rep.first_bucket = 0;
}
*it._pprev = it._element = element;
return old;
}
template <typename T, typename A>
inline void HashContainer<T, A>::insert_at(iterator &it, T *element)
{
click_hash_assert(it._hc == this && it._bucket < _rep.nbuckets);
click_hash_assert(bucket(_rep.hashkey(element)) == it._bucket);
++_rep.size;
if (!(_rep.hashnext(element) = it._element))
_rep.first_bucket = 0;
*it._pprev = element;
it._pprev = &_rep.hashnext(element);
}
template <typename T, typename A>
inline T *HashContainer<T, A>::set(T *element)
{
iterator it = find(_rep.hashkey(element));
return set(it, element);
}
template <typename T, typename A>
inline T *HashContainer<T, A>::erase(iterator &it)
{
click_hash_assert(it._hc == this);
return set(it, 0);
}
template <typename T, typename A>
inline T *HashContainer<T, A>::erase(const key_type &key)
{
iterator it = find(key);
return set(it, 0);
}
template <typename T, typename A>
inline void HashContainer<T, A>::clear()
{
for (size_type b = 0; b < _rep.nbuckets; ++b)
_rep.buckets[b] = 0;
_rep.size = 0;
}
template <typename T, typename A>
inline void HashContainer<T, A>::swap(HashContainer<T, A> &o)
{
HashContainer_rep<T, A> rep(_rep);
_rep = o._rep;
o._rep = rep;
}
template <typename T, typename A>
void HashContainer<T, A>::rehash(size_type n)
{
size_type new_nbuckets = 1;
while (new_nbuckets < n && new_nbuckets < max_bucket_count)
new_nbuckets = ((new_nbuckets + 1) << 1) - 1;
click_hash_assert(new_nbuckets > 0 && new_nbuckets <= max_bucket_count);
if (_rep.nbuckets == new_nbuckets)
return;
T **new_buckets = new T *[new_nbuckets];
for (size_type b = 0; b < new_nbuckets; ++b)
new_buckets[b] = 0;
size_type old_nbuckets = _rep.nbuckets;
T **old_buckets = _rep.buckets;
_rep.nbuckets = new_nbuckets;
_rep.buckets = new_buckets;
_rep.first_bucket = 0;
for (size_t b = 0; b < old_nbuckets; b++)
for (T *element = old_buckets[b]; element; ) {
T *next = _rep.hashnext(element);
size_type new_b = bucket(_rep.hashkey(element));
_rep.hashnext(element) = new_buckets[new_b];
new_buckets[new_b] = element;
element = next;
}
delete[] old_buckets;
}
template <typename T, typename A>
inline bool
operator==(const HashContainer_const_iterator<T, A> &a, const HashContainer_const_iterator<T, A> &b)
{
click_hash_assert(a.hashcontainer() == b.hashcontainer());
return a.get() == b.get();
}
template <typename T, typename A>
inline bool
operator!=(const HashContainer_const_iterator<T, A> &a, const HashContainer_const_iterator<T, A> &b)
{
click_hash_assert(a.hashcontainer() == b.hashcontainer());
return a.get() != b.get();
}
#endif