-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
214 lines (199 loc) · 6.73 KB
/
main.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
#include <iostream>
#include <bitset>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#define NUM_HASH_FUNCS 4
#define BF_SIZE 57707801
std::bitset<BF_SIZE> bloomFilter;
bool startState = true;
std::vector<int> hashFunc(std::string hashValue) {
std::vector<int> result;
result.reserve(NUM_HASH_FUNCS);
for (int i = 0; i < NUM_HASH_FUNCS; ++i) {
hashValue += std::to_string(i);
std::hash<std::string> hasher;
auto hashed = hasher(hashValue);
result[i] = (int) (hashed % BF_SIZE);
}
return result;
}
void showUsage() {
std::cin.clear();
std::cout << std::endl;
std::cout << "|*********************************|" << std::endl;
std::cout << "|****** Bloom Filter Usage *******|" << std::endl;
std::cout << "|*********************************|" << std::endl;
std::cout << "| [0] Show Bloom filter info |" << std::endl;
std::cout << "| [1] Load new input file |" << std::endl;
std::cout << "| [2] Add new value |" << std::endl;
std::cout << "| [3] Load check file |" << std::endl;
std::cout << "| [4] Check new value |" << std::endl;
std::cout << "| [5] Restart Bloom filter |" << std::endl;
std::cout << "| [6] Stop program |" << std::endl;
std::cout << "|*********************************|" << std::endl;
std::cout << std::endl;
startState = false;
}
void showBFInfo() {
std::cin.clear();
std::cout << std::endl;
std::cout << "Bloom filter information:" << std::endl;
std::cout << "Number of hash functions: " << NUM_HASH_FUNCS << std::endl;
std::cout << "Size of the Bloom filter: " << BF_SIZE << std::endl;
std::cout << "Maximum storage capacity value: " << (int) (BF_SIZE / (NUM_HASH_FUNCS / log(2))) << std::endl;
std::cout << "False positive probability: " << (int)(pow(2, -NUM_HASH_FUNCS) * 100) << "%" << std::endl;
std::cout << std::endl;
startState = false;
}
void loadInputFile() {
std::cout << "Type the file name or path of input file: ";
std::string inputFilePath;
std::ifstream inputFile;
std::vector<std::string> inputHashValueVector;
std::string hashValueString;
std::cin >> inputFilePath;
std::cin.clear();
std::cin.ignore(10000,'\n');
inputFile.open(inputFilePath);
if (inputFile.fail()) {
std::cout << "The input file is not exist!" << std::endl;
return;
}
while (!inputFile.eof()) {
std::getline(inputFile, hashValueString);
if (hashValueString.empty()) continue;
if (hashValueString.back() == '\r') hashValueString.pop_back();
std::cout << "Value: " << hashValueString << std::endl;
// hashValueString.pop_back();
inputHashValueVector.push_back(hashValueString);
}
if (inputHashValueVector.empty()) {
std::cout << "The input file is empty!" << std::endl;
return;
}
inputFile.close();
std::cout << "Here" << std::endl;
for (const std::string& hashValue : inputHashValueVector) {
std::vector<int> hashIndex = hashFunc(hashValue);
for (int i = 0; i < NUM_HASH_FUNCS; ++i) {
bloomFilter[hashIndex[i]] = true;
}
}
std::cout << "Complete load the new input file \"" << inputFilePath << "\" to the Bloom filter" << std::endl;
startState = false;
}
void addValue() {
std::cout << "Type the new value for Bloom filter: ";
std::string inputValue;
std::cin >> inputValue;
std::cin.clear();
std::cin.ignore(10000,'\n');
std::vector<int> hashIndex = hashFunc(inputValue);
for (int i = 0; i < NUM_HASH_FUNCS; ++i) bloomFilter[hashIndex[i]] = true;
std::cout << "Complete add \"" << inputValue << "\" to the Bloom filter" << std::endl;
startState = false;
}
void loadCheckFile() {
std::cout << "Type the name or path of the check file: ";
std::string checkFilePath;
std::ifstream checkFile;
std::vector<std::string> checkHashValueVector;
std::string hashValueString;
std::cin >> checkFilePath;
std::cin.clear();
std::cin.ignore(10000,'\n');
checkFile.open(checkFilePath, std::ios::in);
while (!checkFile.eof()) {
std::getline(checkFile, hashValueString);
if (hashValueString.empty()) break;
checkHashValueVector.push_back(hashValueString);
}
if (checkHashValueVector.empty()) {
std::cout << "The check file is empty!" << std::endl;
return;
}
checkFile.close();
for (const std::string& hashValue : checkHashValueVector) {
std::vector<int> hashIndex = hashFunc(hashValue);
for (int i = 0; i < NUM_HASH_FUNCS; ++i) {
if (bloomFilter[hashIndex[i]]) {
std::cout << "Exist " << hashValue << std::endl;
break;
}
}
}
std::cout << "Complete check values in \"" << checkFilePath << "\" with the Bloom filter" << std::endl;
startState = false;
}
void checkValue() {
std::cin.clear();
std::cout << "Type the value which need to check on Bloom filter: ";
std::string inputValue;
std::cin >> inputValue;
std::cin.clear();
std::cin.ignore(10000,'\n');
std::vector<int> hashIndex = hashFunc(inputValue);
for (int i = 0; i < NUM_HASH_FUNCS; ++i) {
if (bloomFilter[hashIndex[i]]) {
std::cout << "Exist " << inputValue << std::endl;
break;
}
}
startState = false;
}
void restartBF() {
std::cin.clear();
startState = true;
bloomFilter.reset();
std::cout << "The Bloom filter has been reset!" << std::endl;
}
int main() {
loadInputFile();
// int option;
// while (true) {
// showUsage();
// if (startState) {
// std::cout << "Select some option in panel: ";
// } else {
// std::cout << "Select new option in panel: ";
// }
// std::cin >> option;
// std::cin.clear();
// std::cin.ignore(10000,'\n');
// if (std::cin.fail()) {
// std::cout << "Invalid the option value!" << std::endl;
// continue;
// }
// if (option == 0) {
// showBFInfo();
// continue;
// }
// if (option == 1) {
// loadInputFile();
// continue;
// }
// if (option == 2) {
// addValue();
// continue;
// }
// if (option == 3) {
// loadCheckFile();
// continue;
// }
// if (option == 4) {
// checkValue();
// continue;
// }
// if (option == 5) {
// restartBF();
// continue;
// }
// if (option == 6) {
// break;
// }
// std::cout << "Invalid command value!" << std::endl;
// }
return 0;
}