-
Notifications
You must be signed in to change notification settings - Fork 1
/
fine.h
191 lines (158 loc) · 5.03 KB
/
fine.h
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
#ifndef FINE_H
#define FINE_H
#include <atomic>
#include "hashtable.h"
#define START_NUM_BUCKETS_FINE 16
#define RESIZE_FACTOR_FINE 0.75
class Node_Fine {
public:
uint32_t key, value;
Node_Fine* next;
Node_Fine(uint32_t K, uint32_t V): key(K), value(V), next(NULL){}
};
class Bucket_Fine {
public:
uint32_t size;
Node_Fine *head;
Bucket_Fine(): size(0), head(NULL) {}
~Bucket_Fine() {
Node_Fine* head = this->head;
while(head != NULL) {
Node_Fine* next = head->next;
//delete head;
head = next;
}
}
bool hasKey(uint32_t k) {
Node_Fine* head = this->head;
while(head != NULL) {
if(head->key == k) {
return true;
}
head = head->next;
}
//Didn't find key
return false;
}
void add(uint32_t k, uint32_t v) {
if(head == NULL) {
head = new Node_Fine(k, v);
} else {
Node_Fine* node = new Node_Fine(k, v);
node->next = head;
head = node;
}
size++;
}
uint32_t get(uint32_t k) {
Node_Fine* head = this->head;
while(head != NULL) {
if(head->key == k) {
return head->value;
}
head = head->next;
}
//Didn't find key
return 0xdeadbeef;
}
};
class Fine : public HashTable {
private:
uint32_t num_buckets;
Bucket_Fine* buckets;
mutex* locks;
mutex resize_lock;
atomic<uint32_t> entries_at;
bool toresize;
//protected field entries
double balanceFactor() {
return (double) entries_at / (double) num_buckets;
}
Bucket_Fine* getBucketForKey(uint32_t key) {
return buckets + (hash_(key) % num_buckets);
}
mutex* getLockForKey(uint32_t key) {
return locks + (hash_(key) % START_NUM_BUCKETS_FINE);
}
void resize() {
//TODO
//Save old buckets
Bucket_Fine* old_buckets = buckets;
uint32_t old_size = num_buckets;
//Make new buckets with old size
num_buckets *= 2;
buckets = new Bucket_Fine[num_buckets];
entries_at = 0;
//Insert all old elements into new table
for(uint32_t i = 0; i < old_size; i++) {
Bucket_Fine* b = &old_buckets[i];
Node_Fine* head = b->head;
while(head != NULL) {
put_free(head->key, head->value);
head = head->next;
}
}
//delete[] old_buckets;
}
public:
Fine(bool toresize = true) : num_buckets(START_NUM_BUCKETS_FINE) {
buckets = new Bucket_Fine[START_NUM_BUCKETS_FINE];
locks = new mutex[START_NUM_BUCKETS_FINE];
entries_at = 0;
this->toresize = toresize;
}
uint32_t get(uint32_t key) {
mutex* bucket_lock = getLockForKey(key);
lock_guard<mutex> lock_g(*bucket_lock);
Bucket_Fine* b = getBucketForKey(key);
return b->get(key);
}
void put(uint32_t key, uint32_t val) {
mutex* bucket_lock = getLockForKey(key);
bucket_lock->lock();
Bucket_Fine* b = getBucketForKey(key);
b->add(key, val);
entries_at++;
bucket_lock->unlock();
if(toresize) {
//Only one thread should resize
if(balanceFactor() >= RESIZE_FACTOR_FINE && resize_lock.try_lock()) {
//Acquire all locks
for(int i = 0; i < START_NUM_BUCKETS_FINE; i++) {
locks[i].lock();
}
resize();
//Free all locks
for(int i = 0; i < START_NUM_BUCKETS_FINE; i++) {
locks[i].unlock();
}
resize_lock.unlock();
}
}
return;
}
//Puts without a lock, helper for resizing
void put_free(uint32_t key, uint32_t val) {
Bucket_Fine* b = getBucketForKey(key);
b->add(key, val);
entries_at++;
return;
}
bool hasKey(uint32_t key) {
mutex* bucket_lock = getLockForKey(key);
lock_guard<mutex> lock_g(*bucket_lock);
Bucket_Fine* b = getBucketForKey(key);
//return false if bucket empty
if(b->size == 0) {
return false;
}
return b->hasKey(key);
}
uint32_t size() {
return entries_at;
}
bool isEmpty() {
return Fine::size() == 0;
}
};
#endif