-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashed.h
84 lines (66 loc) · 1.38 KB
/
hashed.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
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
#ifndef HASHED_H
#define HASHED_H
#include <cstdint>
#include <limits>
#include <cstring>
#include <functional>
#include "fnv.h"
struct hashed
{
hashed();
hashed(char const* start, char const* end, uint32_t hash);
static hashed literal(char const*);
char const* start;
uint32_t size;
uint32_t hash;
};
namespace std
{
template <>
struct hash<hashed>
{
size_t operator()(hashed a) const
{
return a.hash;
}
};
}
inline hashed::hashed()
{}
inline hashed::hashed(char const* start, char const* end, uint32_t hash)
: start(start)
, size(static_cast<uint32_t>(end - start))
, hash(hash)
{
std::ptrdiff_t bsize = end - start;
assert(bsize >= 0);
assert(bsize <= std::numeric_limits<uint32_t>::max());
}
inline hashed hashed::literal(char const* lit)
{
char const* p = lit;
fnv::accumulator acc;
while (*p != '\0')
{
acc(*p);
++p;
}
return hashed(lit, p, acc.get_value());
}
inline bool operator==(hashed a, hashed b)
{
if (a.hash != b.hash)
return false;
if (a.size != b.size)
return false;
return memcmp(a.start, b.start, a.size) == 0;
}
inline bool operator!=(hashed a, hashed b)
{
if (a.hash != b.hash)
return true;
if (a.size != b.size)
return true;
return memcmp(a.start, b.start, a.size) != 0;
}
#endif