LeechCraft
0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Toggle main menu visibility
Loading...
Searching...
No Matches
statichash.h
Go to the documentation of this file.
1
/**********************************************************************
2
* LeechCraft - modular cross-platform feature rich internet client.
3
* Copyright (C) 2006-2014 Georg Rudoy
4
*
5
* Distributed under the Boost Software License, Version 1.0.
6
* (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7
**********************************************************************/
8
9
#pragma once
10
11
#include <concepts>
12
#include <cstddef>
13
#include <cstdint>
14
#include <initializer_list>
15
#include <iterator>
16
#include <string_view>
17
#include <utility>
18
19
namespace
LC::Util
20
{
21
template
<
typename
K,
typename
V>
22
struct
KVPair
23
{
24
const
K
Key_
;
25
const
V
Val_
;
26
27
consteval
KVPair
(K name, V val)
28
:
Key_
{ name }
29
,
Val_
{ val }
30
{
31
}
32
};
33
34
template
<
size_t
N,
typename
V>
35
KVPair
(
const
char
(&) [N], V) ->
KVPair<std::string_view, V>
;
36
37
template
<
typename
K>
38
constexpr
uint64_t
DefaultHashImpl
(K);
39
40
template
<>
41
constexpr
uint64_t
DefaultHashImpl
(std::string_view name)
42
{
43
uint64_t res = 0;
44
for
(
auto
ch : name)
45
res = (res << 8) + ch;
46
return
res;
47
}
48
49
template
<std::convertible_to<u
int
64_t> K>
50
constexpr
uint64_t
DefaultHashImpl
(K val)
51
{
52
return
static_cast<
uint64_t
>
(val);
53
}
54
55
constexpr
uint64_t
DefaultHash
(
auto
val)
56
{
57
return
DefaultHashImpl
(val);
58
}
59
60
template
<
typename
K,
typename
V, auto Hasher = DefaultHash<K>>
61
consteval
auto
MakeHash
(
auto
&&... commands)
62
{
63
const
std::initializer_list<KVPair<K, V>> commandsList { commands... };
64
for
(
auto
i = commandsList.begin (); i != std::prev (commandsList.end ()); ++i)
65
for
(
auto
j = std::next (i); j != commandsList.end (); ++j)
66
if
(Hasher (i->Key_) == Hasher (j->Key_))
67
throw
"duplicate hashes"
;
68
69
return
[=] (K key, V defValue = V {})
70
{
71
const
auto
keyHash = Hasher (key);
72
V result = defValue;
73
K foundKey {};
74
(void) ((Hasher (commands.Key_) == keyHash && (result = commands.Val_, foundKey = commands.Key_,
true
)) || ...);
75
if
(foundKey != key)
76
return
defValue;
77
return
result;
78
};
79
}
80
81
template
<
typename
V>
82
consteval
auto
MakeStringHash
(
auto
&&... commands)
83
{
84
return
MakeHash<std::string_view, V>
(std::forward<
decltype
(commands)> (commands)...);
85
}
86
}
LC::Util
Definition
icoreproxy.h:34
LC::Util::MakeStringHash
consteval auto MakeStringHash(auto &&... commands)
Definition
statichash.h:82
LC::Util::MakeHash
consteval auto MakeHash(auto &&... commands)
Definition
statichash.h:61
LC::Util::DefaultHash
constexpr uint64_t DefaultHash(auto val)
Definition
statichash.h:55
LC::Util::DefaultHashImpl
constexpr uint64_t DefaultHashImpl(K)
Definition
statichash.h:50
LC::Util::KVPair
Definition
statichash.h:23
LC::Util::KVPair::Val_
const V Val_
Definition
statichash.h:25
LC::Util::KVPair::KVPair
consteval KVPair(K name, V val)
Definition
statichash.h:27
LC::Util::KVPair::Key_
const K Key_
Definition
statichash.h:24
src
util
sll
statichash.h
Generated by
1.18.0