-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDB.h
52 lines (48 loc) · 1.27 KB
/
NDB.h
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
#pragma once
#include <vector>
#include <string>
#include <unordered_set>
enum class NDBChar
{
Bit0 = 1,
Bit1 = 2,
Wildcard = 3
};
class NDBRecord
{
public:
NDBRecord() = default;
explicit NDBRecord(std::vector<NDBChar> characters);
NDBChar& operator[](int index);
[[nodiscard]] std::size_t Size() const;
std::vector<NDBChar>& Characters();
[[nodiscard]] const std::vector<NDBChar>& Characters() const;
[[nodiscard]] std::string ToString() const;
bool operator==(const NDBRecord& other) const;
private:
std::vector<NDBChar> _characters;
};
struct NDBRecordHash
{
std::size_t operator()(const NDBRecord& record) const
{
std::size_t seed = record.Size();
for(auto& ch : record.Characters()) {
auto i = static_cast<std::size_t>(ch);
seed ^= i + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
using NDBRecordUnorderedSet = std::unordered_set<NDBRecord, NDBRecordHash>;
class NDB
{
public:
void Add(NDBRecord record);
NDBRecordUnorderedSet& Records();
[[nodiscard]] const NDBRecordUnorderedSet& Records() const;
[[nodiscard]] std::size_t Size() const;
void DumpToFile(const std::string& filename) const;
private:
NDBRecordUnorderedSet _records;
};