forked from epyon/string-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_hash.hpp
158 lines (129 loc) · 4.28 KB
/
string_hash.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2018-2018 ChaosForge
// http://chaosforge.org/
//
// For conditions of distribution and use, see LICENSE file in root folder.
#ifndef NV_STRING_HASH_HPP
#define NV_STRING_HASH_HPP
#include <string>
#include <functional>
namespace detail
{
using uint32 = unsigned long;
using uint64 = unsigned long long;
template < typename H >
struct fnv_hash_values;
template <>
struct fnv_hash_values< uint32 >
{
static constexpr uint32 basis = 2166136261UL;
static constexpr uint32 prime = 16777619UL;
};
template <>
struct fnv_hash_values< uint64 >
{
static constexpr uint64 basis = 14695981039346656037ULL;
static constexpr uint64 prime = 1099511628211ULL;
};
template < typename H >
struct fnv_hash
{
static constexpr H hash_basis = fnv_hash_values< H >::basis;
static constexpr H hash_prime = fnv_hash_values< H >::prime;
static constexpr H string_hash( const char* str, H basis = hash_basis )
{
return str != nullptr ? str_hash_impl( str[0], str + 1, basis ) : 0;
}
template < typename T >
static constexpr H hash( const T* p, std::size_t sz, H basis = hash_basis )
{
return p != nullptr ? hash_impl( static_cast<const char*>( p ), sizeof( T )*sz, basis ) : 0;
}
protected:
static constexpr H str_hash_impl( char c, const char* remain, H value )
{
return c == 0 ? value : str_hash_impl( remain[0], remain + 1, static_cast<H>( value ^ static_cast<H>( c ) ) * hash_prime );
}
static constexpr H hash_impl( const char* current, std::size_t remain, H value )
{
return remain == 0 ? value : hash_impl( current + 1, remain - 1, static_cast<H>( value ^ static_cast<H>( current[0] ) ) * hash_prime );
}
};
template < typename H >
class string_hash
{
public:
using hash_type = H;
using value_type = H;
using hasher = fnv_hash< H >;
constexpr string_hash() {}
constexpr explicit string_hash( hash_type value ) : m_value( value ) {}
constexpr string_hash( const char* str, std::size_t len ) : m_value( hasher::hash( str, len ) ) {}
constexpr string_hash( const char* str ) : m_value( hasher::string_hash( str ) ) {}
inline string_hash( const std::string_view& rhs ) : m_value( hasher::hash( rhs.data(), rhs.size() ) ) {}
inline string_hash( const std::string& rhs ) : m_value( hasher::hash( rhs.data(), rhs.size() ) ) {}
// Literal constructors
template< std::size_t N >
constexpr string_hash( char( &s )[N] ) : m_value( hasher::hash( s, N - 1 ) ) {}
template< std::size_t N >
constexpr string_hash( const char( &s )[N] ) : m_value( hasher::hash( s, N - 1 ) ) {}
constexpr bool is_valid() const { return m_value != 0; }
constexpr explicit operator bool() const { return is_valid(); }
constexpr H value() const { return m_value; }
protected:
hash_type m_value;
};
using shash32 = string_hash< uint32 >;
using shash64 = string_hash< uint64 >;
using shash = string_hash< std::size_t >;
constexpr shash32 operator "" _sh32( const char* str, std::size_t len )
{
return shash32( detail::fnv_hash< uint32 >::hash( str, len ) );
}
constexpr shash64 operator "" _sh64( const char* str, std::size_t len )
{
return shash64( detail::fnv_hash< uint64 >::hash( str, len ) );
}
constexpr shash operator "" _sh( const char* str, std::size_t len )
{
return shash( detail::fnv_hash< std::size_t >::hash( str, len ) );
}
template < typename H >
constexpr bool operator==( const string_hash<H>& lhs, const string_hash<H>& rhs )
{
return lhs.value() == rhs.value();
}
template < typename H >
constexpr bool operator!=( const string_hash<H>& lhs, const string_hash<H>& rhs )
{
return lhs.value() != rhs.value();
}
template < typename H >
constexpr bool operator>( const string_hash<H>& lhs, const string_hash<H>& rhs )
{
return lhs.value() > rhs.value();
}
template < typename H >
constexpr bool operator<( const string_hash<H>& lhs, const string_hash<H>& rhs )
{
return lhs.value() < rhs.value();
}
}
namespace std
{
template < typename H >
struct hash<detail::string_hash<H>>
{
std::size_t operator()( const detail::string_hash<H>& sh ) const
{
return std::size_t( sh.value() );
}
};
}
using detail::operator "" _sh32;
using detail::operator "" _sh64;
using detail::operator "" _sh;
using detail::shash32;
using detail::shash64;
using detail::shash;
using detail::string_hash;
#endif // NV_STRING_HASH_HPP