-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathST.cpp
211 lines (182 loc) · 6.17 KB
/
ST.cpp
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
#include "ST.h"
/**
* Constructor
* initializes relevant parameters.
*/
SplashTable::SplashTable(int B, int R, int S, int h){
this->B = B; //Bucket size
this->R = R; //Recursive limit
this->S = S; //2^S number of entries
this->h = h; //Number of hashes
init();
}
/**
* Initializes some core values, hash functions and buckets.
*/
void SplashTable::init(){
noOfBuckets = exp2(S)/B; //Since B is a power of 2
totalCount = 0; //For dump file
//Initializes buckets with bucketsize B
for(int i = 0; i<noOfBuckets; i++){
buckets.push_back(Bucket(B));
}
//Initialize all hash functions with random multiplier
for(int i = 0; i<h; i++){
uint r = getRandom(1,UINT_MAX);
hashes.push_back(MHash(r,S,B));
}
}
/**
* Public build method, initializes first attempt at insertion
* uint *keys: An array of all non-negative keys
* uint *payloads: An array of all non-negative payloads
*/
int SplashTable::build(uint * keys, uint * payloads, int length){
//Insert every key-payload pair in table
for(int i = 0; i < length; i++){
//lastBucket = -1 will not generate any hits
if(!insert(keys[i], payloads[i], 0, -1)){
//If it fails, return false
return 0;
}
}
//Every key inserted into table
return 1;
}
/**
* Private insert method, returns 1 if success, 0 otherwise
* uint key: A non-negative key
* uint payload: A non-negative payload
* int l: Attempted insertions, has to be below R
* int lastBucket: In case a key is re-inserted into H-lastBucket buckets
*/
int SplashTable::insert(uint key, uint payload, int l, int lastBucket){
int hashedValue;
int minFilled = B; //Count of least filled bucket
int leastFilledBucket = 0; //Index of least filled bucket
int hitLastBucket = 0; //For hash collisions
//Loops through all hash functions
for(int i = 0; i<h; i++){
hashedValue = hashes[i].hash(key); //Hashes key
if(hashedValue == lastBucket){
//Do not attempt to insert into old bucket
hitLastBucket++;
continue;
}
//Tests if minFilled is bigger than current bucket count
if(buckets[hashedValue].count < minFilled){
minFilled = buckets[hashedValue].count;
leastFilledBucket = hashedValue;
}
}
if(minFilled == B){
// they were all full, we need to do a swap and recurse
// if we've hit our recursion limit, bad news
if (l==R){
return 0;
}
// choose a random bucket
int randomBucket;
do{
//Assigns a random bucket from the possible hash functions
//Run only once if all hash functions hash to the same bucket
//OR if found bucket is not the same as last attempt
randomBucket = hashes[getRandom(0, h-1)].hash(key);
} while (randomBucket == lastBucket & hitLastBucket != h);
//Retrieves index of oldest bucket
int index = buckets[randomBucket].getIndexOfOldest();
// swap out the old value and put in the new
int tempKey = buckets[randomBucket].keys[index];
int tempPayload = buckets[randomBucket].payload[index];
//Insert the (key, payload) in the oldest bucket
buckets[randomBucket].insert(key, payload);
//Recursive call, increment l,
return insert(tempKey, tempPayload, ++l, randomBucket);
} else {
// we had room, do the storage
Bucket *bucket = &buckets[leastFilledBucket];
bucket->insert(key, payload);
totalCount++; //increments table count
return 1; //Success
}
}
/**
* Public method, probes table and returns payload on match, else 0.
* uint key: The key to probe the table.
*/
uint SplashTable::probe(uint key){
int hashedValue;
uint value = 0; //Payload
int count = 0; //Number of matches in case of bucket collisions
//Loops through each hash value
for(int i = 0; i<h; i++){
hashedValue = hashes[i].hash(key); //Hashes key
//Loops through every entry in bucket
for(int k = 0; k<B; k++){
//Increments count
count += (buckets[hashedValue].keys[k] == key);
//Only adds a number to value if count == 1 and the key matches
//Then it's 1 * payload = payload
value += ((count == 1) & (buckets[hashedValue].keys[k] == key)) * buckets[hashedValue].payload[k];
}
}
return value; //Returns payload or 0 othervise
}
/**
* Dump method prints a file in the given format
* std::string fileName: Name of the file
*/
void SplashTable::dump(std::string fileName){
std::ofstream dumpfile; //Creates a output filestream
dumpfile.open(fileName);
//Writing first line of file
dumpfile << B << " " << S << " " << h << " " << totalCount << "\n";
//Writing hash values
for(int i = 0; i<h; i++){
dumpfile << hashes[i].getMultiplier();
if(i!=(h-1)){ //So the last multiplier don't print a ' ' before \n
dumpfile << " ";
}
}
dumpfile << "\n";
//Writing all key-value pairs
for(int i = 0; i<noOfBuckets; i++){
for(int k = 0; k<B; k++){
dumpfile << buckets[i].keys[k] << " " << buckets[i].payload[k] << "\n";
}
}
//Close file
dumpfile.close();
}
/**
* Used for generating random number.
* A bit alternative since RAND_MAX = 2^31-1
* generates numbers with an average around 2^31
*/
uint SplashTable::getRandom(uint min, uint max){
uint u1 = 0x0000ffffU & (uint) rand();
uint u2 = 0x0000ffffU & (uint) rand();
u1 = u1 << 16;
u1 = u1 | u2;
if((max - min) == UINT_MAX){
return u1;
} else {
return min + (u1 % (max - min + 1));
}
}
/**
* Used for testing to perform statistics outside SplashTable.
*/
uint SplashTable::getCount(){
return totalCount;
}
/**
* Destructor
* Frees the memory allocated to the bucket arrays.
*/
SplashTable::~SplashTable(){
for(int i = 0; i<noOfBuckets; i++){
delete [] buckets[i].keys;
delete [] buckets[i].payload;
}
}