/
stored-value.cc
86 lines (75 loc) · 1.91 KB
/
stored-value.cc
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <cassert>
#include "stored-value.hh"
#ifndef DEFAULT_HT_SIZE
#define DEFAULT_HT_SIZE 12582917
#endif
size_t HashTable::defaultNumBuckets = DEFAULT_HT_SIZE;
size_t HashTable::defaultNumLocks = 193;
static inline size_t getDefault(size_t x, size_t d) {
return x == 0 ? d : x;
}
/**
* Get the number of buckets for a hash table.
*
* @param n the desired number of buckets, if 0, use the default
*
* @return the number of buckets to create
*/
size_t HashTable::getNumBuckets(size_t n = 0) {
return getDefault(n, defaultNumBuckets);
}
/**
* Get the number of locks for a hash table.
*
* @param n the desired number of locks, if 0, use the default
*
* @return the number of locks to create
*/
size_t HashTable::getNumLocks(size_t n = 0) {
return getDefault(n, defaultNumLocks);
}
/**
* Set the default number of hashtable buckets.
*/
void HashTable::setDefaultNumBuckets(size_t to) {
if (to != 0) {
defaultNumBuckets = to;
}
}
/**
* Set the default number of hashtable locks.
*/
void HashTable::setDefaultNumLocks(size_t to) {
if (to != 0) {
defaultNumLocks = to;
}
}
void HashTable::clear() {
assert(active);
MultiLockHolder(mutexes, n_locks);
for (int i = 0; i < (int)size; i++) {
while (values[i]) {
StoredValue *v = values[i];
values[i] = v->next;
delete v;
}
depths[i] = 0;
}
}
void HashTable::visit(HashTableVisitor &visitor) {
for (int i = 0; i < (int)size; i++) {
LockHolder lh(getMutex(i));
StoredValue *v = values[i];
while (v) {
visitor.visit(v);
v = v->next;
}
}
}
void HashTable::visitDepth(HashTableDepthVisitor &visitor) {
for (int i = 0; i < (int)size; i++) {
LockHolder lh(getMutex(i));
visitor.visit(i, depths[i]);
}
}