cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
Hash.h
Go to the documentation of this file.
1
/*-----------------------------------------------------------------------------------------------
2
The MIT License (MIT)
3
4
Copyright (c) 2014-2026 Kim Kulling
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy of
7
this software and associated documentation files (the "Software"), to deal in
8
the Software without restriction, including without limitation the rights to
9
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
the Software, and to permit persons to whom the Software is furnished to do so,
11
subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
-----------------------------------------------------------------------------------------------*/
23
#pragma once
24
25
#include <
cppcore/CPPCoreCommon.h
>
26
27
namespace
cppcore
{
28
29
//-------------------------------------------------------------------------------------------------
34
//-------------------------------------------------------------------------------------------------
35
template
<
class
T>
36
class
THash
{
37
public
:
39
THash
() =
default
;
40
43
explicit
THash
(T hash);
44
48
explicit
THash
(
const
char
*buffer, T base);
49
53
explicit
THash
(T value, T base);
54
56
~THash
() =
default
;
57
62
static
T
toHash
(
const
char
*buffer, T base);
63
68
static
T
toHash
(T value, T base);
69
72
T
hashValue
()
const
;
73
74
private
:
75
T mHash{0};
76
};
77
78
template
<
class
T>
79
inline
THash<T>::THash
(T hash) : mHash(hash) {
80
// empty
81
}
82
83
template
<
class
T>
84
inline
THash<T>::THash
(
const
char
*buffer, T base) : mHash(
THash
::
toHash
(buffer, base)) {
85
// empty
86
}
87
88
template
<
class
T>
89
inline
THash<T>::THash
(T value, T base) : mHash(
THash
::
toHash
(value, base)) {
90
// empty
91
}
92
93
template
<
class
T>
94
inline
T
THash<T>::toHash
(
const
char
*buffer, T base) {
95
T hash = 0;
96
if
(
nullptr
== buffer) {
97
return
hash;
98
}
99
100
// using division-rest method
101
// see http://de.wikipedia.org/wiki/Divisionsrestmethode
102
for
(
size_t
i = 0; i < strlen(buffer); ++i) {
103
hash = (hash * 128 + buffer[i]) % base;
104
}
105
106
return
hash;
107
}
108
109
template
<
class
T>
110
inline
T
THash<T>::toHash
(T value, T base) {
111
const
T hash = value % base;
112
return
hash;
113
}
114
115
template
<
class
T>
116
inline
T
THash<T>::hashValue
()
const
{
117
return
mHash;
118
}
119
120
}
// Namespace cppcore
CPPCoreCommon.h
cppcore::THash::THash
THash()=default
The default class constructor.
cppcore::THash::~THash
~THash()=default
The class destructor.
cppcore::THash::toHash
static T toHash(const char *buffer, T base)
Computes the hash value for a given character buffer.
Definition
Hash.h:94
cppcore::THash::hashValue
T hashValue() const
brief Returns the stored hash value.
Definition
Hash.h:116
cppcore
Definition
DateTime.h:28
include
cppcore
Common
Hash.h
Generated by
1.15.0