-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cpp
executable file
·72 lines (54 loc) · 1.57 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
#include <cstdlib>
#include <ctime>
#include <sys/stat.h>
#include "HashTable.hpp"
std::size_t fileSize(const char *name);
std::size_t lineCount(const char *strg);
void readLinesFromStorage(char *strg, const char *lines[]);
signed main()
{
const std::size_t nLookUps = 100000000;
std::srand(time(nullptr));
const std::size_t fileSz = fileSize("words.txt");
std::FILE *wordStream = std::fopen("words.txt", "rb");
auto strg = new char[fileSz + 1]{};
std::fread(strg, sizeof(*strg), fileSz, wordStream);
std::fclose(wordStream);
const std::size_t nLines = lineCount(strg);
auto lines = new const char *[nLines]{};
readLinesFromStorage(strg, lines);
HashTable hashTable{elfHash};
for (std::size_t i = 0; i < nLines; ++i) hashTable.insert(lines[i], "");
for (std::size_t i = 0; i < nLookUps; ++i) hashTable.find(lines[std::rand() % nLines]);
delete[] lines;
delete[] strg;
return EXIT_SUCCESS;
}
std::size_t fileSize(const char *const name)
{
struct stat buf{};
stat(name, &buf);
return buf.st_size;
}
std::size_t lineCount(const char *strg)
{
if (*strg == '\0') return 0;
std::size_t nLines = 0;
while (*strg != '\0') {
if (*strg++ == '\n') ++nLines;
}
return nLines + 1;
}
void readLinesFromStorage(char *strg, const char *lines[])
{
const char *currLine = strg;
std::size_t idx = 0;
while (*strg != '\0') {
if (*strg++ == '\n') {
strg[-1] = '\0';
lines[idx++] = currLine;
currLine = strg;
}
}
lines[idx] = currLine;
}