-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
106 lines (85 loc) · 2.14 KB
/
hash.cpp
File metadata and controls
106 lines (85 loc) · 2.14 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
#include "hash.hpp"
#include "game.hpp"
#include <iostream>
#include <iomanip>
#include <vector>
#include <tuple>
#include <unordered_map>
struct debug_hasher
{
using result_type = std::size_t;
void operator()(void const* key, std::size_t len) noexcept
{
unsigned char const* p = static_cast<unsigned char const*>(key);
unsigned char const* const e = p + len;
for (; p < e; ++p)
buf.push_back(*p);
}
explicit operator std::size_t() noexcept
{
return buf.size();
}
void dump(std::ostream& os)
{
os << std::hex;
os << std::setfill('0');
unsigned int n = 0;
for (auto c : buf) {
os << std::setw(2) << (unsigned)c << ' ';
if (++n == 16) {
os << '\n';
n = 0;
}
}
os << '\n';
os << std::dec;
os << std::setfill(' ');
}
std::vector<unsigned char> buf;
};
void test_unordered_map()
{
using hash = lock3::hash<lock3::fn1va64_hasher>;
std::unordered_map<game::item, int, hash> prices;
prices.emplace(game::item {0}, 100);
prices.emplace(game::item {1}, 200);
std::cout << prices[game::item {0}] << '\n';
std::cout << prices[game::item {1}] << '\n';
assert(prices.find(game::item {42}) == prices.end());
}
int main()
{
using namespace lock3;
debug_hasher h;
// Base types
hash_append(h, char(42));
hash_append(h, 42);
hash_append(h, 42ull);
enum E0 { A, B, C };
enum class E1 { A, B, C };
hash_append(h, C);
hash_append(h, E1::C);
int n = 42;
hash_append(h, &n);
hash_append(h, nullptr);
// User-defined types
class S0 { };
class S1 { int x = 42; };
struct S2 { int x = 42; private: int y = 42; };
struct S { union { int x; }; };
hash_append(h, S0());
hash_append(h, S1());
hash_append(h, S2());
// hash_append(h, S()); // error: anonymous union
hash_append(h, std::make_pair(42, 'a'));
hash_append(h, std::make_tuple(42, 'a', 32.0));
game::player andrew {"andrew", {100, 100}, {50, 50}};
hash_append(h, andrew);
game::monster dragon {-1};
hash_append(h, dragon);
game::item sword {42};
hash_append(h, sword);
h.dump(std::cout);
test_unordered_map();
// FIXME: Test bitwise hashable things.
}