Euphoria
hash.string.h
Go to the documentation of this file.
1#pragma once
2
3#include "base/ints.h"
4
5#include <string_view>
6#include <string>
7#include <unordered_map>
8
9// 1 = include text in hash, 0=don't
10// todo(Gustav): move to a config instead
11#define USE_HASH_TEXT 1
12
13namespace eu
14{
15
18constexpr u64 hash64(const std::string_view str, u64 hash = 0xcbf29ce484222325)
19 { return str.empty() ? hash : hash64(str.substr(1), (hash ^ str[0]) * 0x100000001b3); }
20
26struct Hsh
27{
31
32 #if USE_HASH_TEXT == 1
35 std::string_view text;
36 #endif
37
39 constexpr Hsh(const std::string_view s)
40 : hash( hash64(s) )
41 #if USE_HASH_TEXT == 1
42 , text(s)
43 #endif
44 { }
45};
46
52struct HshO
53{
57
58#if USE_HASH_TEXT == 1
61 std::string text;
62#endif
63
65 constexpr HshO(const std::string& s)
66 : hash(hash64(s))
67#if USE_HASH_TEXT == 1
68 , text(s)
69#endif
70 { }
71
73 constexpr HshO(const std::string_view& s)
74 : hash(hash64(s))
75#if USE_HASH_TEXT == 1
76 , text(s)
77#endif
78 { }
79
81 constexpr HshO(const Hsh& o)
82 : hash(o.hash)
83#if USE_HASH_TEXT == 1
84 , text(o.text)
85#endif
86 { }
87};
88
89#define OP(op) \
90 constexpr bool operator op(const Hsh& lhs, const Hsh& rhs)\
91 { return lhs.hash op rhs.hash; }\
92 constexpr bool operator op(const HshO& lhs, const Hsh& rhs)\
93 { return lhs.hash op rhs.hash; }\
94 constexpr bool operator op(const Hsh& lhs, const HshO& rhs)\
95 { return lhs.hash op rhs.hash; }\
96 constexpr bool operator op(const HshO& lhs, const HshO& rhs)\
97 { return lhs.hash op rhs.hash; }
98 OP(==) OP(!=) OP(<) OP(>) OP(<=) OP(>=)
99#undef OP
100
101
102std::string string_from(const Hsh h);
103std::string string_from(const HshO& h);
104
105
108}
109
110ADD_DEFAULT_FORMATTER(eu::Hsh, std::string, eu::string_from);
111ADD_DEFAULT_FORMATTER(eu::HshO, std::string, eu::string_from);
112
113
114namespace std
115{
116
118template <> struct hash<eu::Hsh>
119{
120 std::size_t operator()(const eu::Hsh& x) const
121 { return x.hash; }
122};
123
125template <> struct hash<eu::HshO>
126{
127 std::size_t operator()(const eu::HshO& x) const
128 { return x.hash; }
129};
130
131}
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
std::string string_from(const An &a)
#define USE_HASH_TEXT
Definition hash.string.h:11
Definition assert.h:109
constexpr u64 hash64(const std::string_view str, u64 hash=0xcbf29ce484222325)
fnv-1a hash.
Definition hash.string.h:18
std::uint64_t u64
Definition ints.h:15
OP(==) OP(!
Hash of an owning string.
Definition hash.string.h:53
std::string text
Owning storage of the printable string, if enabled.
Definition hash.string.h:61
u64 hash
The actual hash.
Definition hash.string.h:56
constexpr HshO(const std::string &s)
Creates a new hash object and computes the hash at compile time if possible.
Definition hash.string.h:65
constexpr HshO(const Hsh &o)
Copy data from a non-owning hash.
Definition hash.string.h:81
constexpr HshO(const std::string_view &s)
Creates a new hash object and computes the hash at compile time if possible.
Definition hash.string.h:73
Hash of a non-owning string.
Definition hash.string.h:27
constexpr Hsh(const std::string_view s)
Creates a new hash object and computes the hash at compile time if possible.
Definition hash.string.h:39
u64 hash
The actual hash.
Definition hash.string.h:30
std::string_view text
Non-owning storage of the printable string, if enabled.
Definition hash.string.h:35