-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashing.cpp
334 lines (296 loc) · 10.8 KB
/
hashing.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "header.h"
class Lexicon{
public:
int occupied; // how many keys currently stored in here
int slots; // how many slots we actually have, including the ones used and not used
int lengthOfA; // how many chars long can A be
int* T;
char* A;
int indexOfA;
int maxLoopToInsert;
Lexicon(int slots){
this->maxLoopToInsert = 0;
this->occupied = 0;
this->slots = slots;
this->indexOfA = 0; // indicates which slot of A was the word last added to
// for the slots of T, -1 will indicate empty
// for the slots of T, -2 will indicate deleted
// else, that is the value
this->T = new int[this->slots];
for(int i = 0; i < this->slots; i++){
this->T[i] = -1;
}
this->lengthOfA = 15*this->slots; // Assuming an average word length of 14 or less (14 for word and 1 for the 0 byte separating the words)
this->A = new char[this->lengthOfA];
}
~Lexicon(){
delete this->T;
}
void doubleA(){
char* newA = new char[2*this->lengthOfA];
for(int i = 0; i < this->lengthOfA; i++){
newA[i] = this->A[i];
}
this->lengthOfA = 2*this->lengthOfA;
delete this->A;
this->A = newA;
return;
}
void doubleT(){
this->slots *= 2;
delete this->T;
this->T = new int[this->slots];
for(int i = 0; i < this->slots; i++){
this->T[i] = -1;
}
this->occupied = 0;
char* newA = new char[this->lengthOfA];
for(int i = 0; i<this->lengthOfA; i++){
newA[i] = this->A[i];
}
int max = this->indexOfA;
this->indexOfA = 0;
this->A = new char[this->lengthOfA];
std::string wordSoFar = "";
for(int i = 0; i<max; i++){
if(newA[i] != '\0'){
wordSoFar += newA[i];
}
else{
int len = wordSoFar.length();
if(wordSoFar[0] == '*'){
int limit = this->indexOfA + len;
for(; this->indexOfA<limit; this->indexOfA++){
this->A[this->indexOfA] = '*';
}
this->A[this->indexOfA] = '\0';
this->indexOfA++;
wordSoFar = "";
}
else{
this->Insert(wordSoFar);
wordSoFar = "";
}
}
}
}
bool Insert(std::string word){
if(this->Search(word) != -1){return(false);}
while(1 + this->indexOfA + word.length() > this->lengthOfA || this->isFull()){this->doubleA(); this->doubleT();}
// if(this->isFull()){this->doubleT();}
for(int i = 0; i<this->slots; i++){
int index = hashing(i, this->sumAsciiMinusTwo(word));
this->maxLoopToInsert++;
if(this->T[index] < 0){
// free to use to store
// actually do the storing
this->T[index] = this->indexOfA;
for(int j=0; j<word.length(); j++){
this->A[this->indexOfA] = word[j];
this->indexOfA++;
}
this->A[this->indexOfA] = '\0';
this->indexOfA++;
this->occupied++;
return(true);
}
}
return(false);
}
int sumAsciiMinusTwo(std::string word){
int sumMinusTwo = 0;
for(int i=0; i<word.length(); i++){
sumMinusTwo += word[i];
}
sumMinusTwo -= 2;
return(sumMinusTwo);
}
int hashing(int i, int k){
return(((i*i) + k)%this->slots);
}
std::string wordInA(int index){
std::string rtn = "";
for(int i = index; i<this->lengthOfA; i++){
if(this->A[i] == '\0'){
break;
}
else{
rtn += this->A[i];
}
}
return(rtn);
}
int Search(std::string word){
for(int i=0; i<this->maxLoopToInsert; i++){
int index = this->hashing(i, this->sumAsciiMinusTwo(word));
int Tindex = this->T[index];
switch(Tindex){
// reached blank
case -1:
return(-1);
break;
// previously deleted entry
case -2:
break;
// found a spot to put it in
default:
std::string guess = this->wordInA(Tindex);
if(word.compare(guess) == 0){
return(index);
}
else if(guess.compare("") == 0 || guess[0] == '*'){
return(-1);
}
break;
}
}
return(-1);
}
int Delete(std::string word){
int loc = this->Search(word);
if(loc == -1){
return(-1);
}
this->occupied--;
for(int i = this->T[loc]; i<this->lengthOfA; i++){
if(A[i] != '\0'){
A[i] = '*';
}
else{
this->T[loc] = -2;
return(loc);
}
}
return(-1);
}
std::string Print(){
std::string rtn = " T\t\t\tA: ";
for(int i=0; i<this->indexOfA; i++){
if(A[i] == '\0'){
rtn += "\\";
}
else{
rtn += this->A[i];
}
}
rtn += "\n";
for(int i = 0; i < this->slots; i++){
rtn += std::to_string(i) + ": ";
rtn += this->T[i] >= 0 ? std::to_string(this->T[i]) : "";
rtn += "\n";
}
return(rtn);
}
bool isEmpty(){
return(this->occupied == 0);
}
bool isFull(){
return(this->occupied >= this->slots);
}
};
class HashBatch{
public:
std::string filename;
Lexicon* lexLuther;
HashBatch(std::string filename){
this->filename = filename;
}
void Create(int slots){
lexLuther = new Lexicon(slots);
return;
}
int Search(std::string word){
return(lexLuther->Search(word));
}
std::string Print(){
return(lexLuther->Print());
}
bool Empty(){
return(lexLuther->isEmpty());
}
bool Full(){
return(lexLuther->isFull());
}
int Delete(std::string word){
return(lexLuther->Delete(word));
}
bool Insert(std::string word){
return(lexLuther->Insert(word));
}
void Batch(){
std::string line;
std::ifstream myfile(filename);
if(myfile.is_open()){
while(getline(myfile,line)){
std::string firstWord = line.substr(0, line.find(" "));
// Command 10 is Insert, Command 11 is Deletion, Command 12 is Search. Command 13 is Print, Command 14 is Create. Command 15 is Comment
// Insert
if(firstWord.compare("10") == 0){
std::string secondWord = line.substr(firstWord.length() + 1, line.length() - 1);
bool inserted = Insert(secondWord);
if(!inserted){
// Not Inserted
int loc = Search(secondWord);
std::cout << secondWord + " ";
if(loc == -1){
std::cout << "\toverflow and cannot insert";
}
else{
std::cout << "\tfound at slot " + std::to_string(loc);
}
std::cout << "\n";
}
}
// Delete
else if(firstWord.compare("11") == 0){
std::string secondWord = line.substr(firstWord.length() + 1, line.length() - 1);
int deletedIndex = Delete(secondWord);
std::cout << secondWord + " ";
if(deletedIndex == -1){
std::cout << "\tnot found";
}
else{
std::cout << "\tdeleted from slot " + std::to_string(deletedIndex);
}
std::cout << "\n";
}
// Search
else if(firstWord.compare("12") == 0){
std::string secondWord = line.substr(firstWord.length() + 1, line.length() - 1);
int loc = Search(secondWord);
std::cout << secondWord + " ";
if(loc == -1){
std::cout << "\tnot found";
}
else{
std::cout << "\tfound at slot " + std::to_string(loc);
}
std::cout << "\n";
}
// Print
else if(firstWord.compare("13") == 0){
std::cout << Print();
}
// Create
else if(firstWord.compare("14") == 0){
std::string secondWord = line.substr(firstWord.length() + 1, line.length() - 1);
int slots = stoi(secondWord);
Create(slots);
}
// Comment
else if(firstWord.compare("15") == 0){
continue;
}
}
myfile.close();
}
return;
}
};
int main(int argc, char* argv[]){
std::string filename = argv[1];
HashBatch* batman = new HashBatch(filename);
batman->Batch();
delete batman;
return(0);
}