-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathbackshift_hashmap.cc
246 lines (202 loc) · 6.7 KB
/
backshift_hashmap.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
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
#include "backshift_hashmap.h"
namespace hashmap {
int BackshiftHashMap::Open() {
buckets_ = new Bucket[num_buckets_];
memset(buckets_, 0, sizeof(Bucket) * (num_buckets_));
monitoring_ = new hashmap::Monitoring(num_buckets_, num_buckets_, static_cast<HashMap*>(this));
num_buckets_used_ = 0;
return 0;
}
int BackshiftHashMap::Close() {
if (buckets_ != NULL) {
for (uint32_t i = 0; i < num_buckets_; i++) {
if (buckets_[i].entry != NULL) {
delete[] buckets_[i].entry->data;
delete buckets_[i].entry;
}
}
delete[] buckets_;
}
if (monitoring_ != NULL) {
delete monitoring_;
}
return 0;
}
int BackshiftHashMap::Get(const std::string& key, std::string* value) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
uint64_t probe_distance = 0;
bool found = false;
uint32_t i;
for (i = 0; i < probing_max_; i++) {
uint64_t index_current = (index_init + i) % num_buckets_;
FillDistanceToInitIndex(index_current, &probe_distance);
if ( buckets_[index_current].entry == NULL
|| i > probe_distance) {
break;
}
if ( key.size() == buckets_[index_current].entry->size_key
&& memcmp(buckets_[index_current].entry->data, key.c_str(), key.size()) == 0) {
*value = std::string(buckets_[index_current].entry->data + key.size(),
buckets_[index_current].entry->size_value);
found = true;
break;
}
}
if (found) return 0;
monitoring_->AddDMB(i);
monitoring_->AddAlignedDMB(index_init, (index_init + i) % num_buckets_);
return 1;
}
int BackshiftHashMap::Put(const std::string& key, const std::string& value) {
if (num_buckets_used_ == num_buckets_) {
return 1;
}
num_buckets_used_ += 1;
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
char *data = new char[key.size() + value.size()];
memcpy(data, key.c_str(), key.size());
memcpy(data + key.size(), value.c_str(), value.size());
BackshiftHashMap::Entry *entry = new BackshiftHashMap::Entry;
entry->size_key = key.size();
entry->size_value = value.size();
entry->data = data;
uint64_t index_current = index_init;
uint64_t probe_distance = 0;
uint64_t probe_current = 0;
BackshiftHashMap::Entry *entry_temp = NULL;
uint64_t hash_temp = 0;
uint64_t i;
int num_swaps = 0;
for (i = 0; i < probing_max_; i++) {
index_current = (index_init + i) % num_buckets_;
if (buckets_[index_current].entry == NULL) {
monitoring_->SetDIB(index_current, probe_current);
buckets_[index_current].entry = entry;
buckets_[index_current].hash = hash;
break;
} else {
FillDistanceToInitIndex(index_current, &probe_distance);
if (probe_current > probe_distance) {
// Swapping the current bucket with the one to insert
entry_temp = buckets_[index_current].entry;
hash_temp = buckets_[index_current].hash;
buckets_[index_current].entry = entry;
buckets_[index_current].hash = hash;
entry = entry_temp;
hash = hash_temp;
monitoring_->SetDIB(index_current, probe_current);
probe_current = probe_distance;
num_swaps += 1;
}
}
probe_current++;
}
monitoring_->AddDFB(i);
monitoring_->AddAlignedDFB(index_init, index_current);
monitoring_->AddNumberOfSwaps(num_swaps);
return 0;
}
int BackshiftHashMap::Exists(const std::string& key) {
// TODO: implement
return 0;
}
int BackshiftHashMap::Remove(const std::string& key) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
bool found = false;
uint64_t index_current = 0;
uint64_t probe_distance = 0;
for (uint64_t i = 0; i < num_buckets_; i++) {
index_current = (index_init + i) % num_buckets_;
FillDistanceToInitIndex(index_current, &probe_distance);
if ( buckets_[index_current].entry == NULL
|| i > probe_distance) {
break;
}
if ( key.size() == buckets_[index_current].entry->size_key
&& memcmp(buckets_[index_current].entry->data, key.c_str(), key.size()) == 0) {
found = true;
break;
}
}
if (found) {
delete[] buckets_[index_current].entry->data;
delete buckets_[index_current].entry;
monitoring_->RemoveDIB(index_current);
uint64_t i = 1;
uint64_t index_previous = 0, index_swap = 0;
for (i = 1; i < num_buckets_; i++) {
index_previous = (index_current + i - 1) % num_buckets_;
index_swap = (index_current + i) % num_buckets_;
if (buckets_[index_swap].entry == NULL) {
buckets_[index_previous].entry = NULL;
monitoring_->RemoveDIB(index_previous);
break;
}
uint64_t distance;
if (FillDistanceToInitIndex(index_swap, &distance) != 0) {
fprintf(stderr, "Error in FillDistanceToInitIndex()");
}
if (distance == 0) {
buckets_[index_previous].entry = NULL;
monitoring_->RemoveDIB(index_previous);
break;
}
buckets_[index_previous].entry = buckets_[index_swap].entry;
buckets_[index_previous].hash = buckets_[index_swap].hash;
monitoring_->SetDIB(index_previous, distance-1);
}
monitoring_->AddDSB(i);
monitoring_->AddAlignedDSB(index_current, index_swap);
num_buckets_used_ -= 1;
return 0;
}
return 1;
}
int BackshiftHashMap::Resize() {
// TODO: implement
return 0;
}
// For debugging
int BackshiftHashMap::CheckDensity() {
return 0;
}
int BackshiftHashMap::BucketCounts() {
return 0;
}
int BackshiftHashMap::Dump() {
return 0;
}
int BackshiftHashMap::GetBucketState(int index) {
//printf("GetBucketState %d\n", index);
if (buckets_[index].entry == NULL) {
return 0;
}
return 1;
}
int BackshiftHashMap::FillInitIndex(uint64_t index_stored, uint64_t *index_init) {
if(buckets_[index_stored].entry == NULL) return -1;
*index_init = buckets_[index_stored].hash % num_buckets_;
return 0;
}
int BackshiftHashMap::FillDistanceToInitIndex(uint64_t index_stored, uint64_t *distance) {
if(buckets_[index_stored].entry == NULL) return -1;
uint64_t index_init = buckets_[index_stored].hash % num_buckets_;
if (index_init <= index_stored) {
*distance = index_stored - index_init;
} else {
*distance = index_stored + (num_buckets_ - index_init);
}
return 0;
}
void BackshiftHashMap::GetMetadata(std::map< std::string, std::string >& metadata) {
metadata["name"] = "backshift";
char buffer[1024];
sprintf(buffer, "{\"num_buckets\": %" PRIu64 ", \"probing_max\": %" PRIu64 "}", num_buckets_, probing_max_);
metadata["parameters_hashmap"] = buffer;
sprintf(buffer, "nb%" PRIu64 "-pm%" PRIu64 "", num_buckets_, probing_max_);
metadata["parameters_hashmap_string"] = buffer;
}
}; // end namespace hashmap