-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.cpp
More file actions
134 lines (120 loc) · 4.38 KB
/
Copy pathDictionary.cpp
File metadata and controls
134 lines (120 loc) · 4.38 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
//
// Dictionary.cpp
// Project3
//
// Created by Rumeysa Bulut on 29.11.2017.
// Copyright © 2017 Rumeysa Bulut. All rights reserved.
//
#include <iostream>
#include <sstream>
#include <fstream>
#include <cmath>
#include <ctime>
#include "Dictionary.hpp"
#define M 131071
using namespace std;
Dictionary::Dictionary(){
count = 0; all_collision = 0; avg_collision1 = 0; avg_collision2 = 0; avg_collision3 = 3;
ifstream inputFile;
inputFile.open("ds-set-input.txt");
if(!inputFile){
cout<< "Error opening input file" << endl;
return;
}
clock_t start = clock();
block_insertion(inputFile);
clock_t end = clock();
double elapsed_time = double (end - start) / CLOCKS_PER_SEC;
cout << "Insertion finished after :" << elapsed_time << " seconds" << endl << endl;
cout << "Average number of collisions (first 1000)\t | " << (long double) avg_collision1/1000 << endl;
cout << "Average number of collisions (first 10000)\t | " << (long double) avg_collision2/10000 << endl;
cout << "Average number of collisions (first 100000)\t | " << (long double)avg_collision3/100000 << endl;
cout << "Average number of collisions (overall)\t | " << (long double) all_collision/(M-1) << endl << endl;
start = clock();
block_lookup();
end = clock();
elapsed_time = double (end - start) / CLOCKS_PER_SEC;
cout << "Lookup finished after :" << elapsed_time << " seconds" << endl << endl;
inputFile.close();
}
void Dictionary::block_insertion(ifstream &inputFile){
BookCharacter charobj;
vector<bool> check_empty;
unsigned long index;
unsigned long line_count = 0;
unsigned long collision = 0;
string line;
newCharacter.resize(M);
check_empty.resize(M);
for (unsigned long i = 0; i < M; i++) {
check_empty[i] = true; //it is for checking the index is empty or not. True means empty.
}
while (getline(inputFile, line)) {
line_count++;
charobj = BookCharacter(line);
index = hash(charobj.getKey());
if(check_empty[index]){ //if that index is empty
newCharacter[index] = charobj; //insert the character
check_empty[index] = false; //and make the index false as not empty
}
else{ // do probing and insert
while (!check_empty[index]) { //until it founds the true (empty) index
collision++; //total collision number
count++; //collision count spesific to one index and one book character
index = quadratic_probe(count, charobj.getKey());
}
newCharacter[index] = charobj; //insert the character
check_empty[index] = false;
count = 0;
}
if(line_count == 1000)
avg_collision1 = collision;
else if(line_count == 10000)
avg_collision2 = collision;
else if(line_count == 100000)
avg_collision3 = collision;
else if(line_count == M-1)
all_collision = collision;
}
}
unsigned long Dictionary::hash(unsigned long k){
unsigned long hashed_value; //will be index
double A = (sqrt(5) - 1)/2;
hashed_value = floor(M * fmod((k*A), 1));
return hashed_value;
}
unsigned long Dictionary::quadratic_probe(unsigned long i, unsigned long k){
unsigned long index;
unsigned long h = hash(k);
index = (h + 7*i + 3*i*i) % M;
return index;
}
void Dictionary::block_lookup(){
BookCharacter wanted;
unsigned long index;
count = 0;
ifstream lookupFile;
ofstream lookupOut;
lookupFile.open("ds-set-lookup.txt");
lookupOut.open("ds-set-output-dict.txt");
if(!lookupFile){
cout<< "Error opening input file" << endl;
return;
}
string line;
while(getline(lookupFile, line)){
wanted = BookCharacter(line);
index = hash(wanted.getKey());
if( newCharacter[index].getKey() == wanted.getKey()){
lookupOut << line << '\t' << newCharacter[index].getCharacter() << endl;
}
else {
while( newCharacter[index].getKey() != wanted.getKey()){
count++;
index = quadratic_probe(count, wanted.getKey());
}
count = 0;
lookupOut << line << '\t' << newCharacter[index].getCharacter() << endl;
}
}
}