Hash.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2012 OpenFOAM Foundation
9  Copyright (C) 2018-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::Hash
29 
30 Description
31  Hash function class.
32  The default definition is for primitives.
33  Non-primitives used to hash entries on hash tables will need
34  a specialized version.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Hash_H
39 #define Hash_H
40 
41 #include "Hasher.H"
42 #include <cstdint>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class Hash Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 template<class T>
54 struct Hash
55 {
56  unsigned operator()(const T& obj, unsigned seed=0) const
57  {
58  return Foam::Hasher(&obj, sizeof(T), seed);
59  }
60 };
61 
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 //- Hashing of nullptr, always 0
66 template<>
67 struct Hash<std::nullptr_t>
68 {
69  unsigned operator()(std::nullptr_t, unsigned seed=0) const noexcept
70  {
71  return seed;
72  }
73 };
74 
75 //- Hashing of pointers, treat as unsigned integer
76 template<>
77 struct Hash<void*>
78 {
79  unsigned operator()(const void* const ptr, unsigned seed=0) const
80  {
81  const uintptr_t addr = uintptr_t(ptr);
82  return Foam::Hasher(&addr, sizeof(addr), seed);
83  }
84 };
85 
86 
87 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
88 
89 // Specialization for common integral types
90 
91 #undef FOAM_HASH_SPECIALIZATION
92 #define FOAM_HASH_SPECIALIZATION(Type) \
93  \
94  \
95  \
96  template<> \
97  struct Hash<Type> \
98  { \
99  unsigned operator()(const Type val) const \
100  { \
101  return static_cast<unsigned>(val); \
102  } \
103  unsigned operator()(const Type val, unsigned seed) const \
104  { \
105  return Foam::Hasher(&val, sizeof(Type), seed); \
106  } \
107  }
108 
113 FOAM_HASH_SPECIALIZATION(uint32_t);
114 #undef FOAM_HASH_SPECIALIZATION
115 
116 
117 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
118 
119 } // End namespace Foam
120 
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 
123 #endif
124 
125 // ************************************************************************* //
Foam::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:581
Foam::Hash< void * >::operator()
unsigned operator()(const void *const ptr, unsigned seed=0) const
Definition: Hash.H:78
Foam::Hash::operator()
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:55
FOAM_HASH_SPECIALIZATION
#define FOAM_HASH_SPECIALIZATION(Type)
Definition: Hash.H:91
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Hasher.H
Miscellaneous hashing functions, mostly from Bob Jenkins.
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Hash< std::nullptr_t >::operator()
unsigned operator()(std::nullptr_t, unsigned seed=0) const noexcept
Definition: Hash.H:68
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33