forked from Priyansh2/linear_hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_hashing.cpp
More file actions
196 lines (175 loc) · 4.56 KB
/
linear_hashing.cpp
File metadata and controls
196 lines (175 loc) · 4.56 KB
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <queue>
#include <cmath>
using namespace std;
unsigned int numBuffers, bufferSize;
class Block {
// each block has at max (bufferSize/4) numbers and the possibility of putting one overflow block
vector<int> records;
Block *overflow;
public:
Block() {
overflow = NULL;
records.clear();
}
bool isPresent(int x) {
Block *node = this;
while(node) {
for(unsigned int i = 0; i < node->records.size(); i++) {
if(node->records[i] == x) {
return true;
}
}
node = node->overflow;
}
return false;
}
void add(int x) {
// as sizeof(int) is 4
if(records.size() < (bufferSize / 4)) {
records.push_back(x);
}
else {
if(overflow == NULL) {
overflow = new Block();
}
overflow->add(x);
}
}
void clearElements(vector<int> &v) {
for(unsigned int i = 0; i < records.size(); i++) {
v.push_back(records[i]);
}
records.clear();
if(overflow) {
overflow->clearElements(v);
delete overflow;
overflow = NULL;
}
}
void print() {
Block *node = this;
while(node) {
for(unsigned int i = 0; i < node->records.size(); i++) {
cout << node->records[i] << ' ';
}
node = node->overflow;
}
cout << '\n';
}
};
class HashTable {
int numRecords, numBits;
vector<Block *> buckets;
public:
HashTable() {
// initial configuration of Hash table
numRecords = 0;
numBits = 1;
buckets.push_back(new Block());
buckets.push_back(new Block());
}
unsigned int hash(int x) {
unsigned int mod = (1 << numBits);
return (unsigned int)(x % mod + mod) % mod;
}
int occupancy() {
// as sizeof(int) is 4
double ratio = 1.0 * numRecords / buckets.size();
return (int)(100 * (ratio / (bufferSize / 4)));
}
bool isPresent(int x) {
unsigned int k = hash(x);
if(k >= buckets.size()) {
k -= (1 << (numBits - 1));
}
if(buckets[k]->isPresent(x)) {
return true;
}
return false;
}
void insert(int x) {
unsigned int k = hash(x);
if(k >= buckets.size()) {
k -= (1 << (numBits - 1));
}
buckets[k]->add(x);
numRecords++;
while(occupancy() >= 75) {
buckets.push_back(new Block());
numBits = ceil(log2((double)buckets.size()));
// split old bucket and rehash
k = buckets.size() - 1 - (1 << (numBits - 1));
vector<int> v;
buckets[k]->clearElements(v);
for(unsigned int i = 0; i < v.size(); i++) {
buckets[hash(v[i])]->add(v[i]);
}
}
//print();
}
void print() {
for(unsigned int i = 0; i < buckets.size(); i++) {
cout << i << " -> ";
buckets[i]->print();
}
cout << '\n';
}
};
HashTable h;
// Buffered IO
queue<int> inputBuffer, outputBuffer;
unsigned int inputSize, outputSize;
void clearOutput() {
int ox;
while(!outputBuffer.empty()) {
ox = outputBuffer.front();
outputBuffer.pop();
cout << ox << '\n';
}
}
void clearInput() {
int ix;
while(!inputBuffer.empty()) {
ix = inputBuffer.front();
inputBuffer.pop();
if(!h.isPresent(ix)) {
h.insert(ix);
if(outputBuffer.size() == outputSize) {
clearOutput();
}
outputBuffer.push(ix);
}
}
}
int main(int argc, char *argv[]) {
if(argc < 4) {
cout << "Usage : ./a.out <filename> <numBuffers> <bufferSize>\n";
exit(-1);
}
ifstream input(argv[1]);
numBuffers = atoi(argv[2]); //Must be >=2
bufferSize = atoi(argv[3]); // Must be >=4
if(numBuffers < 2 || bufferSize < 4) {
cout << "Ensure that M>=2 and B>=4\n";
exit(-1);
}
// as sizeof(int) is 4
inputSize = (numBuffers - 1) * (bufferSize / 4);
outputSize = bufferSize / 4;
int x;
while(input >> x) {
if(inputBuffer.size() < inputSize) {
inputBuffer.push(x);
}
else {
clearInput();
inputBuffer.push(x);
}
}
clearInput();
clearOutput();
return 0;
}