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 | Copyright (C) 2011-2012 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 specialized version of this class.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #ifndef Hash_H
34 #define Hash_H
35 
36 #include <stdint.h>
37 
38 #include "label.H"
39 #include "uLabel.H"
40 #include "Hasher.H"
41 #include "pTraits.H"
42 #include "fileName.H"
43 #include "wordRe.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 //template<class Type> class Hash;
50 //template<> class Hash<label>;
51 
52 /*---------------------------------------------------------------------------*\
53  Class Hash Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 template<class PrimitiveType>
57 class Hash
58 {
59 public:
60 
61  Hash()
62  {}
63 
64  unsigned operator()(const PrimitiveType& p, unsigned seed) const
65  {
66  return Hasher(&p, sizeof(p), seed);
67  }
68 
69  unsigned operator()(const PrimitiveType& p) const
70  {
71  return Hasher(&p, sizeof(p));
72  }
73 
74 };
75 
76 
77 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 
79 //- Hash specialization for hashing labels
80 template<>
81 class Hash<Foam::label>
82 {
83 public:
84 
85  Hash()
86  {}
87 
88  //- Incrementally hash a label.
89  // This will necessarily return a different value than the
90  // non-incremental version.
91  unsigned operator()(const label p, unsigned seed) const
92  {
93  return Hasher(&p, sizeof(label), seed);
94  }
95 
96  //- Return the unsigned representation of a label.
97  // This helps if people have relied on the hash value corresponding to
98  // the natural order.
99  unsigned operator()(const label p) const
100  {
101  return p;
102  }
103 };
104 
105 
106 //- Hash specialization for hashing strings
107 template<>
108 class Hash<Foam::string>
109 {
110 public:
111 
112  Hash()
113  {}
114 
115  unsigned operator()(const string& p, unsigned seed) const
116  {
117  return string::hash()(p, seed);
118  }
119  unsigned operator()(const string& p) const
120  {
121  return string::hash()(p);
122  }
123 };
124 
125 
126 //- Hash specialization for hashing words
127 template<>
128 class Hash<Foam::word>
129 {
130 public:
131 
132  Hash()
133  {}
134 
135  unsigned operator()(const word& p, unsigned seed) const
136  {
137  return word::hash()(p, seed);
138  }
139  unsigned operator()(const word& p) const
140  {
141  return word::hash()(p);
142  }
143 };
144 
145 
146 //- Hash specialization for hashing fileNames
147 template<>
148 class Hash<Foam::fileName>
149 {
150 public:
151 
152  Hash()
153  {}
154 
155  unsigned operator()(const fileName& p, unsigned seed) const
156  {
157  return fileName::hash()(p, seed);
158  }
159  unsigned operator()(const fileName& p) const
160  {
161  return fileName::hash()(p);
162  }
163 };
164 
165 
166 //- Hash specialization for hashing wordRes
167 template<>
168 class Hash<Foam::wordRe>
169 {
170 public:
171 
172  Hash()
173  {}
174 
175  unsigned operator()(const wordRe& p, unsigned seed) const
176  {
177  return wordRe::hash()(p, seed);
178  }
179  unsigned operator()(const wordRe& p) const
180  {
181  return wordRe::hash()(p);
182  }
183 };
184 
185 
186 //- Hash specialization for hashing keyTypes
187 template<>
188 class Hash<Foam::keyType>
189 {
190 public:
191 
192  Hash()
193  {}
194 
195  unsigned operator()(const keyType& p, unsigned seed) const
196  {
197  return keyType::hash()(p, seed);
198  }
199  unsigned operator()(const keyType& p) const
200  {
201  return keyType::hash()(p);
202  }
203 };
204 
205 
206 
207 //- Hash specialization for hashing pointer addresses.
208 // Treat a pointer like a long.
209 // This should work for both 32-bit and 64-bit pointers.
210 template<>
211 class Hash<void*>
212 {
213 public:
214  typedef intptr_t HashType;
215 
216  Hash()
217  {}
218 
219  unsigned operator()(const void* const& p, unsigned seed) const
220  {
221  return Hash<HashType>()(HashType(p), seed);
222  }
223 
224  unsigned operator()(const void* const& p) const
225  {
226  return Hash<HashType>()(HashType(p));
227  }
228 
229 };
230 
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 } // End namespace Foam
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 #endif
239 
240 // ************************************************************************* //
Foam::Hash< void * >::operator()
unsigned operator()(const void *const &p) const
Definition: Hash.H:223
Foam::Hash< Foam::word >::operator()
unsigned operator()(const word &p, unsigned seed) const
Definition: Hash.H:134
Foam::Hash< void * >::operator()
unsigned operator()(const void *const &p, unsigned seed) const
Definition: Hash.H:218
Foam::Hash< Foam::wordRe >::operator()
unsigned operator()(const wordRe &p, unsigned seed) const
Definition: Hash.H:174
Foam::Hash< Foam::string >::Hash
Hash()
Definition: Hash.H:111
p
p
Definition: pEqn.H:62
Foam::Hash< Foam::keyType >::operator()
unsigned operator()(const keyType &p, unsigned seed) const
Definition: Hash.H:194
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::Hash< Foam::fileName >::operator()
unsigned operator()(const fileName &p, unsigned seed) const
Definition: Hash.H:154
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::Hash< Foam::keyType >::operator()
unsigned operator()(const keyType &p) const
Definition: Hash.H:198
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:476
Foam::Hash< Foam::label >::Hash
Hash()
Definition: Hash.H:84
Foam::Hash< Foam::keyType >::Hash
Hash()
Definition: Hash.H:191
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::Hash< Foam::fileName >::operator()
unsigned operator()(const fileName &p) const
Definition: Hash.H:158
Foam::wordRe
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
Foam::Hash< Foam::wordRe >::Hash
Hash()
Definition: Hash.H:171
Foam::Hash::Hash
Hash()
Definition: Hash.H:60
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::Hash
Hash function class for primitives. All non-primitives used to hash entries on hash tables likely nee...
Definition: Hash.H:56
Foam::label
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
wordRe.H
Hasher.H
Misc. hashing functions, mostly from Bob Jenkins.
fileName.H
Foam::Hash::operator()
unsigned operator()(const PrimitiveType &p, unsigned seed) const
Definition: Hash.H:63
pTraits.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Hash< Foam::string >::operator()
unsigned operator()(const string &p, unsigned seed) const
Definition: Hash.H:114
Foam::Hash< void * >::HashType
intptr_t HashType
Definition: Hash.H:213
Foam::string::hash
Hashing function class, shared by all the derived classes.
Definition: string.H:90
Foam::Hash::operator()
unsigned operator()(const PrimitiveType &p) const
Definition: Hash.H:68
label.H
Foam::Hash< Foam::word >::Hash
Hash()
Definition: Hash.H:131
Foam::Hash< Foam::word >::operator()
unsigned operator()(const word &p) const
Definition: Hash.H:138
Foam::Hash< Foam::string >::operator()
unsigned operator()(const string &p) const
Definition: Hash.H:118
Foam::Hash< void * >::Hash
Hash()
Definition: Hash.H:215
Foam::Hash< Foam::fileName >::Hash
Hash()
Definition: Hash.H:151
uLabel.H
Foam::Hash< Foam::wordRe >::operator()
unsigned operator()(const wordRe &p) const
Definition: Hash.H:178