Hash.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Hash
26 
27 Description
28  Hash function class for primitives. All non-primitives used to hash
29  entries on hash tables likely need a specialised version of this class.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #ifndef Hash_H
34 #define Hash_H
35 
36 #include "label.H"
37 #include "uLabel.H"
38 #include "Hasher.H"
39 #include "pTraits.H"
40 #include "fileName.H"
41 #include "wordRe.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class Hash Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class PrimitiveType>
53 class Hash
54 {
55 public:
56 
57  Hash()
58  {}
59 
60  unsigned operator()(const PrimitiveType& p, unsigned seed) const
61  {
62  return Hasher(&p, sizeof(p), seed);
63  }
64 
65  unsigned operator()(const PrimitiveType& p) const
66  {
67  return Hasher(&p, sizeof(p));
68  }
69 
70 };
71 
72 
73 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74 
75 //- Hash specialisation for hashing labels
76 template<>
77 class Hash<Foam::label>
78 {
79 public:
80 
81  Hash()
82  {}
83 
84  //- Incrementally hash a label.
85  // This will necessarily return a different value than the
86  // non-incremental version.
87  unsigned operator()(const label p, unsigned seed) const
88  {
89  return Hasher(&p, sizeof(label), seed);
90  }
91 
92  //- Return the unsigned representation of a label.
93  // This helps if people have relied on the hash value corresponding to
94  // the natural order.
95  unsigned operator()(const label p) const
96  {
97  return p;
98  }
99 };
100 
101 
102 //- Hash specialisation for hashing strings
103 template<>
104 class Hash<Foam::string>
105 {
106 public:
107 
108  Hash()
109  {}
110 
111  unsigned operator()(const string& p, unsigned seed) const
112  {
113  return string::hash()(p, seed);
114  }
115  unsigned operator()(const string& p) const
116  {
117  return string::hash()(p);
118  }
119 };
120 
121 
122 //- Hash specialisation for hashing words
123 template<>
124 class Hash<Foam::word>
125 {
126 public:
127 
128  Hash()
129  {}
130 
131  unsigned operator()(const word& p, unsigned seed) const
132  {
133  return word::hash()(p, seed);
134  }
135  unsigned operator()(const word& p) const
136  {
137  return word::hash()(p);
138  }
139 };
140 
141 
142 //- Hash specialisation for hashing fileNames
143 template<>
144 class Hash<Foam::fileName>
145 {
146 public:
147 
148  Hash()
149  {}
150 
151  unsigned operator()(const fileName& p, unsigned seed) const
152  {
153  return fileName::hash()(p, seed);
154  }
155  unsigned operator()(const fileName& p) const
156  {
157  return fileName::hash()(p);
158  }
159 };
160 
161 
162 //- Hash specialisation for hashing wordRes
163 template<>
164 class Hash<Foam::wordRe>
165 {
166 public:
167 
168  Hash()
169  {}
170 
171  unsigned operator()(const wordRe& p, unsigned seed) const
172  {
173  return wordRe::hash()(p, seed);
174  }
175  unsigned operator()(const wordRe& p) const
176  {
177  return wordRe::hash()(p);
178  }
179 };
180 
181 
182 //- Hash specialisation for hashing keyTypes
183 template<>
184 class Hash<Foam::keyType>
185 {
186 public:
187 
188  Hash()
189  {}
190 
191  unsigned operator()(const keyType& p, unsigned seed) const
192  {
193  return keyType::hash()(p, seed);
194  }
195  unsigned operator()(const keyType& p) const
196  {
197  return keyType::hash()(p);
198  }
199 };
200 
201 
202 
203 //- Hash specialisation for hashing pointer addresses.
204 // Treat a pointer like a long.
205 // This should work for both 32-bit and 64-bit pointers.
206 template<>
207 class Hash<void*>
208 {
209 public:
210 
211  Hash()
212  {}
213 
214  unsigned operator()(const void* const& p, unsigned seed) const
215  {
216  return Hash<long>()(long(p), seed);
217  }
218 
219  unsigned operator()(const void* const& p) const
220  {
221  return Hash<long>()(long(p));
222  }
223 };
224 
225 
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 
228 } // End namespace Foam
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 
232 #endif
233 
234 // ************************************************************************* //
Misc. hashing functions, mostly from Bob Jenkins.
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:53
unsigned operator()(const PrimitiveType &p, unsigned seed) const
Definition: Hash.H:59
Hash()
Definition: Hash.H:56
A class for handling file names.
Definition: fileName.H:82
A class for handling keywords in dictionaries.
Definition: keyType.H:69
Hashing function class, shared by all the derived classes.
Definition: string.H:93
A class for handling character strings derived from std::string.
Definition: string.H:79
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:476
volScalarField & p