-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
executable file
·83 lines (64 loc) · 2.26 KB
/
Main.cpp
File metadata and controls
executable file
·83 lines (64 loc) · 2.26 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
#include <cstdlib>
#include <ctime>
#include <sys/stat.h>
#include "HashTable.hpp"
std::size_t fileSize(const char *name);
std::size_t cntLines(const char *strg);
void readLinesFromStorage(const char *strg, HashTable::String *lines[]);
signed main()
{
const std::size_t nLookUps = 100000000;
std::srand(time(nullptr));
const 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 = cntLines(strg);
auto lines = new HashTable::String *[nLines]{};
readLinesFromStorage(strg, lines);
delete[] strg;
HashTable hashTable{crc32Hash};
HashTable::String placeholder{""};
for (std::size_t i = 0; i < nLines; ++i) {
hashTable.insert(*lines[i], placeholder);
}
for (std::size_t i = 0; i < nLookUps; ++i) hashTable.find(*lines[std::rand() % nLines]);
for (size_t i = 0; i < nLines; ++i) std::free(lines[i]);
delete[] lines;
return EXIT_SUCCESS;
}
std::size_t fileSize(const char *const name)
{
struct stat buf{};
stat(name, &buf);
return buf.st_size;
}
std::size_t cntLines(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(const char *strg, HashTable::String *lines[])
{
const char *currLine = strg;
size_t lineLen = 0;
size_t idx = 0;
while (*strg != '\0') {
if (*strg++ == '\n') {
lines[idx] = static_cast<HashTable::String *>(std::calloc(1, sizeof(HashTable::String)));
lineLen = strg - 1 - currLine;
std::strncpy(static_cast<char *>(*lines[idx++]), currLine,
(lineLen > HashTable::StringSize - 1) ? HashTable::StringSize - 1 : lineLen);
currLine = strg;
}
}
lines[idx] = static_cast<HashTable::String *>(std::calloc(1, sizeof(HashTable::String)));
lineLen = (strg == currLine) ? 0 : strg - currLine;
std::strncpy(static_cast<char *>(*lines[idx]), currLine,
(lineLen > HashTable::StringSize - 1) ? HashTable::StringSize - 1 : lineLen);
}