-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriehash_v4.cc
649 lines (555 loc) · 17.1 KB
/
triehash_v4.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//ME - C++17 - hope it compile - Clang
/*
* Authors: Alhussain Almarhabi
* */
//Reference - future review
/* Use assignment four, how to read files of .dat
*Use github Trie class
*/
#include <iostream>
#include <fstream>
#include<string>
#include <sstream>
#include <list>
#include <utility>
#include <iomanip>
using namespace std;
//class HashMapLinearChaining;
class LinkedList {
private:
class Node{
public:
string data;
Node* next;
Node(string v, Node* next) : data(std::move(v)), next(next) {}
};
Node* head;
public:
friend class HashMapLinearChaining;
LinkedList() : head(nullptr) {}
~LinkedList(){
Node* q;
for (Node* p = head; p != nullptr;){
q = p;
p = p->next;
delete q;
}
}
LinkedList(const LinkedList& orig) = default;
void operator = (const LinkedList& orig) = delete;
LinkedList::Node* HEAD(){ return head;}
void addStart(string v) { //O(1)
head = new Node(std::move(v),this->head);
}
int size() const { // O(n)
int count = 0;
for (Node* p = head; p != nullptr; p=p->next)
count++;
return count;
}
[[nodiscard]] string get(int i) const { //O(n)
for (Node* p = head; p != nullptr; i--, p = p->next)
if (i == 0)
return p->data;
throw "Out of bounds";
}
void showList()
{//O(n)
if (this->size() == 0){
cout << "empty" << endl;
}
else {
cout << "{";
for (int i = 0; i < this->size(); i++) {
if (i == this->size() - 1){
cout << this->get(i);
cout << "}" << endl;
break;
}
cout << this->get(i) << ", ";
}
}
}
void showList2()
{//O(n)
if (this->size() == 0){
cout << "empty" << endl;
}
else {
cout << "{";
for (int i = 0; i < 10; i++) {
if (i == 9){
cout << this->get(i);
cout << "}" << endl;
break;
}
cout << this->get(i) << ", ";
}
}
}
void removeValue(string v){
Node* p = head;
Node* q = p;
while (p->next != nullptr) {
if (p->data ==v && p == head){
head = p->next;
return;
}
if (p->data == v && p->next != nullptr){
q->next = p->next;
return;
}
q = p;
p = p->next;
}
if (p->data == v && p->next == nullptr){
q->next = nullptr;
return;
}
}
};
class Trie {
private:
class Node {
public:
Node* next[26];
bool isWord;
Node(): next(), isWord(false){
for (auto & i : next){ i = nullptr;}
this->isWord = false;
}
};
Node* root;
public:
Trie() { root = new Node();}
~Trie() {}
Trie::Node* Root(){ return root;}
// load the trie from a file
void load(const string& filename) {
std::ifstream dfile;
dfile.open(filename);
if (!dfile.is_open()){
cout << "Error: file did not open" << endl;
return;
}
std::string line;
int count = 0;
while(!dfile.eof()){
std::getline(dfile,line);
std::stringstream ss(line);
int i = 0;
char temp;
char temp1[line.size()];
while (ss >> temp){
temp1[i] = temp;
i++;
}
int wordsize = line.length();
add(temp1, wordsize);
count++;
}
cout<< "Total loaded number of words: "<< count <<endl;
dfile.close();
}
Trie(const Trie& orig) = delete;
Trie& operator =(const Trie& orig) = delete;
void add(const char word[], int wordsize){
Trie::Node* p = root;
for (int i = 0; i < wordsize ; i++ ){
if(p->next[(int)word[i] - 'a'] == nullptr){
p->next[(int)word[i] - 'a'] = new Node();
}
p = p->next[(int)word[i] - 'a'];
}
p->isWord = true;
}
void remove(const char word[], int wordsize){
Trie::Node* p = root;
for (int i = 0; i < wordsize ; i++ ){
if(p->next[(int)word[i] - 'a'] == nullptr){
return;
}
p = p->next[(int)word[i] - 'a'];
}
p->isWord = false;
}
bool contains(const char word[], int wordsize){
Trie::Node* p = root;
for (int i = 0; i < wordsize ; i++ ){
if(p->next[(int)word[i] - 'a'] == nullptr){
return false;
}
p = p->next[(int)word[i] - 'a'];
}
if (p->isWord){ return true; } else { return false;}
}
bool containsPrefix(const char word[], int wordsize){
Trie::Node* p = root;
for (int i = 0; i < wordsize ; i++ ){
if(p->next[(int)word[i] - 'a'] == nullptr){
return false;
}
p = p->next[(int)word[i] - 'a'];
}
return true;
}
void prefixNodeCombine(const char word[], int wordsize, const string& prefix){
char str[20]; int level = 0;
Trie::Node* p = root;
for (int i = 0; i < wordsize ; i++ ){
if(p->next[(int)word[i] - 'a'] == nullptr){
return;
}
p = p->next[(int)word[i] - 'a'];
}
print2(p, str, level, prefix);
}
void print(Trie::Node* roott, char str[], int level){
if (roott->isWord){
str[level] = '\0';
cout << str << endl;
}
for (int i = 0; i< 26; i++){
if (roott->next[i] ){
str[level] = i + 'a';
print(roott->next[i], str, level+1);
}
}
/*
Trie::Node* p1 = this->root;
Trie::Node* p = this->root;
list<char> printl;
char cprint;
for (int i = 0; i< 26; i++){
while (p->next[i] != nullptr && !p->isWord){
cprint = i + 'a';
cout<< cprint;
int ii = 0;
while (p->next[i]->next[ii]){
ii++;
}
p = p->next[i];
//if (p->isWord){break;}
}
p = p1;
}*/
//if (p->next[0]->next[0]->isWord){ cout<<endl<< "work"<<endl;} else{cout <<endl<<"second work"<<endl;}
/*
for (int i = 0; i< 26; i++){
if(p->next[0] == nullptr && p->isWord){
cprint =0+97;
cout << cprint;
}
p = p->next[0];// p->next[i+97];
}*/
/*
for (int i = 0; i < 26; i++){
sprint = i + 97;
cout << sprint;
}*/
/*
while (true){
char xx= 25 + 97;
p = p->next[xx];
cout << xx<<endl;
if (p->isWord){
cout<< "this worked";
break;
}
}*/
}
void print2(Trie::Node* roott, char str[], int level, const string& prefix){
if (roott->isWord){//(isleafNode(roott)){
str[level] = '\0';
cout << prefix << str << endl;
}
for (int i = 0; i< 26; i++){
if (roott->next[i] ){
str[level] = i + 'a';
print2(roott->next[i], str, level+1, prefix);
}
}
}
};
class HashMapLinearChaining {
private:
uint32_t size;
LinkedList* table;
public:
HashMapLinearChaining(uint32_t size): size(size) {
table = new LinkedList[size];
}
~HashMapLinearChaining() = default;
HashMapLinearChaining(const HashMapLinearChaining &orig) = delete;
HashMapLinearChaining &operator=(const HashMapLinearChaining &orig) = delete;
int hash(string s){
int sum = 0;
for (int i =0; s[i]!='\0';i++){
sum = sum * 26+ s[i] + ((sum >> 17)|(sum << 13));
}
return sum & (size - 1 );
}
void load(const string& filename) {
std::ifstream dfile;
dfile.open(filename);
if (!dfile.is_open()){
cout << "Error: file did not open testAdd.txt" << endl;
return;
}
std::string line;
int count = 0;
while(!dfile.eof()){
std::getline(dfile,line);
std::stringstream ss(line);
add(line);
count++;
}
cout<< "Total loaded number of words: "<< count <<endl;
}
void add(const string &s) {
uint32_t pos;
pos = hash(s);
if (pos >= 500000){pos = 0;}
LinkedList::Node *p = table[pos].head;
if (p == nullptr){
table[pos].addStart(s);
} else{
while (p->next != nullptr){
if (p->data == s){
return;}
p = p->next;
}
table[pos].addStart(s);
}
}
void remove(const string &s) {
int pos;
pos = hash(s);
if (table[pos].head == nullptr){
return;
} else {
table[pos].removeValue(s);
}
}
bool contains(const string &s) {
int pos;
pos = hash(s);
if (table[pos].head != nullptr){
if (table[pos].head->data == s){
return true;
}
} else{
return false;}
while (table[pos].head->next != nullptr){
if (table[pos].head->data == s){
return true;
}
table[pos].head = table[pos].head->next;
}
return false;
}
void computeHistogram() {
// generate an array of 10 elements
//hist[0] = number of empty bins
// hist[1] = number of bins containing 1 element
// hist[2] = number of bins containing 2 elements...
// hist[9] = number of bins containing 9 OR GREATER
// print all non-zero bins:
int empt, cont1, cont2, cont9;
empt = 0;
cont1 = 0;
cont2 = 0;
cont9 = 0;
for (int i = 0; i < size; i++) {
if (this->table[i].size() == 0) { empt++; }
else if (this->table[i].size() == 1) { cont1++; }
else if (this->table[i].size() == 2) { cont2++; }
else { cont9++; }
}
cout << "number of empty bins: " << empt << endl;
cout << "number of contains 1 bins: " << cont1 << endl;
cout << "numnber of contains 2 bins: " << cont2 << endl;
cout << "number of contains more than 9 bins: " << cont9 << endl;
for (int i = 0; i < size; i++) {
if (this->table[i].size() == 0 || this->table[i].size() == 1 || this->table[i].size() == 2){ continue;}
else {
this->table[i].showList2();
}
}
}
void printAsList(){
for (int i =0; i < size; i++){
if (this->table[i].size() != 0){
std::cout << "Table[" << i << "]: as list = ";
this->table[i].showList();
cout<<endl;}
}
}
};
int main() {
//###### this is for Trie
cout << "-------------------"<<endl<< " TRIE "<<endl<<"-------------------"<<endl;
Trie trie1,trie2;
std::string filename1, filename2, filename22, filename3, filename4;
filename1 = "./testAdd.txt";
filename2 = "./testContains.txt";
filename22 = "./testTriePrefix.txt";
filename3 = "./testRemove.txt";
filename4 = "./prideandprejudice.txt";
std::ifstream dfile1,dfile2,dfile22,dfile3,dfile4;
dfile1.open(filename1);
dfile2.open(filename2);
dfile22.open(filename22);
dfile3.open(filename3);
dfile4.open(filename4);
if (!dfile2.is_open() || !dfile1.is_open() || !dfile22.is_open() || !dfile3.is_open()){
cout << "Error: one of the file is not opened" << endl;
} else{
cout << "### Add words in testAdd.txt file"<<endl;
trie2.load(filename1);
/// Check word contain
cout<<"### Trie contained word from testContained.txt"<<endl;
std::string line;
while(!dfile2.eof()){
std::getline(dfile2,line);
std::stringstream ss(line);
int i = 0;
char temp;
char temp1[line.size()];
while (ss >> temp){
temp1[i] = temp;
i++;
}
int wordsize = line.length();
if(trie2.contains(temp1, wordsize)){
cout<<line<<endl;
}
ss.clear();
}
/// Check word with prefix
cout<<"### Check all words with prefix in testTriePrefix.txt"<<endl;
while(!dfile22.eof()){
std::getline(dfile22,line);
std::stringstream ss(line);
int i = 0;
char temp;
char temp1[line.size()];
while (ss >> temp){
temp1[i] = temp;
i++;
}
int wordsize = line.length();
if(trie2.containsPrefix(temp1, wordsize)){
trie2.prefixNodeCombine(temp1, wordsize, line);
}
}
/// Remove words in testRemove.txt
cout<< "### Remove words in testRemove.txt: Done"<<endl;
while(!dfile3.eof()){
std::getline(dfile3,line);
std::stringstream ss(line);
int i = 0;
char temp;
char temp1[line.size()];
while (ss >> temp){
temp1[i] = temp;
i++;
}
int wordsize = line.length();
trie2.remove(temp1,wordsize);
}
/// print final trie
cout<<"### print final trie"<<endl;
int level = 0;
char str[100];
trie2.print(trie2.Root(), str, level);
}
/// Load dict to a diferent trie instance
cout <<"### Creat trie and load dict.txt "<< endl;
trie1.load("./dict.txt");
std::string line;
int mainct = 0;
while(!dfile4.eof()){
//cout << "I'm inside the file"<<endl;
std::getline(dfile4,line);
//cout << line << endl;
std::stringstream ss(line);
std::string sline;
char temp;
while (ss >> sline){
// cout << "I'm inside the first loop"<<endl;
std::stringstream ss2(sline);
int i = 0;
char temp1[sline.size()];
while (ss2 >> temp){
// cout << "I'm inside the second loop"<<endl;
if (((int)temp >= 97 && (int)temp <= 122)){
temp1[i] = temp;
i++;
}
else {
break; }
}
if (!trie1.contains(temp1, i)) {
mainct++;
}
ss.clear(); ss2.clear();
}
}
cout<< "Total number of non-words at prideandprejudice file: "<< mainct<< " out of 125525"<<endl;
//##### this is for Hash - linear chaining
cout << "-------------------"<<endl<< " HASH "<<endl<<"-------------------"<<endl;
uint32_t const defhashsize = 500000;
HashMapLinearChaining hash1(defhashsize), hash2(defhashsize);
dfile1.clear();dfile2.clear();dfile3.clear();
if (!dfile1.is_open() || !dfile2.is_open() || !dfile3.is_open()){
cout << "Error: Error: one of the file is not opened" << endl;
} else{
cout << "### Add words in testAdd.txt file"<<endl;
hash2.load(filename1);
cout<< "### Hash histogram "<<endl;
hash2.computeHistogram();
cout<<endl;
cout<< "### check words that in contain file"<<endl;
if (!dfile2.is_open()){
cout << "Error: file did not open testContains.txt" << endl;
}
dfile2.seekg(0);
while(!dfile2.eof()){
std::string line2;
std::getline(dfile2,line2);
std::stringstream ss(line2);
if (hash2.contains(line2)){cout<<line2<<endl;}
}
/// Remove words in testRemove.txt
cout<< "### Remove words in testRemove.txt: Done"<<endl;
while(!dfile3.eof()){
std::string line3;
std::getline(dfile3,line3);
std::stringstream ss(line3);
hash2.remove(line3);
}
/// Print final hash
std::cout<< "### Print Hash as list "<<std::endl;
hash2.printAsList();
}
cout << "load dict into a new hash" << endl;
hash1.load("./dict.txt");
dfile4.close();
dfile4.open(filename4);
mainct = 0;
while(!dfile4.eof()){
std::getline(dfile4,line);
std::stringstream ss(line);
string sline;
char temp;
while (ss >> sline){
std::stringstream ss2(sline);
if (!hash1.contains(sline)){mainct++;}
int i = 0;
}
}
cout<< "Total number of non-words at prideandprejudice file: "<< mainct<< " out of 125525"<<endl;
dfile1.close(); dfile2.close(); dfile22.close(); dfile3.close();dfile4.close();
return 0;
}