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