-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringkey.hpp
51 lines (38 loc) · 1.07 KB
/
stringkey.hpp
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
//@ {
//@ "targets":[{"name":"stringkey.hpp","type":"include"}]
//@ }
#ifndef ALICE_STRINGKEY_HPP
#define ALICE_STRINGKEY_HPP
#include <cstdint>
namespace Alice
{
class Stringkey
{
public:
typedef uint64_t HashValue;
constexpr Stringkey():m_value(0){}
explicit constexpr Stringkey(const char* str) noexcept:
m_value(hash(str))
{}
constexpr bool operator<(const Stringkey& key) const noexcept
{return m_value < key.m_value;}
constexpr bool operator==(const Stringkey& key) const noexcept
{return key.m_value==m_value;}
constexpr bool operator!=(const Stringkey& key) const noexcept
{return !(key==*this);}
constexpr operator HashValue() const noexcept
{return m_value;}
private:
HashValue m_value;
static constexpr uint64_t OFFSET_BASIS=0xcbf29ce484222325;
static constexpr uint64_t FNV_PRIME=0x100000001b3;
static constexpr uint64_t hash(const char* str
,HashValue value_init=OFFSET_BASIS)
{
return *str=='\0'?
value_init
:hash(str+1, (value_init^(HashValue(*str)))*FNV_PRIME);
}
};
}
#endif