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