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-2019 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, non-primitives used to hash
33  entries on hash tables likely need a specialized version of this class.
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Hash_H
38 #define Hash_H
39 
40 #include "label.H"
41 #include "uLabel.H"
42 #include "Hasher.H"
43 #include "fileName.H"
44 #include "wordRe.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class Hash Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 template<class T>
56 struct Hash
57 {
58  inline unsigned operator()(const T& obj, unsigned seed=0) const
59  {
60  return Hasher(&obj, sizeof(obj), seed);
61  }
62 };
63 
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 //- Hash specialization for label
68 template<>
69 struct Hash<Foam::label>
70 {
71  //- Incrementally hash a label.
72  // This will necessarily return a different value than the
73  // non-incremental version.
74  inline unsigned operator()(const label obj, unsigned seed) const
75  {
76  return Hasher(&obj, sizeof(label), seed);
77  }
78 
79  //- Return the unsigned representation of a label.
80  // This helps if people have relied on the hash value corresponding to
81  // the natural order.
82  inline unsigned operator()(const label obj) const
83  {
84  return obj;
85  }
86 };
87 
88 
89 //- Hash specialization for string
90 template<>
91 struct Hash<Foam::string>
92 {
93  inline unsigned operator()(const string& obj, unsigned seed=0) const
94  {
95  return string::hash()(obj, seed);
96  }
97 };
98 
99 
100 //- Hash specialization for word
101 template<>
102 struct Hash<Foam::word>
103 {
104  inline unsigned operator()(const word& obj, unsigned seed=0) const
105  {
106  return string::hash()(obj, seed);
107  }
108 };
109 
110 
111 //- Hash specialization for fileName
112 template<>
113 struct Hash<Foam::fileName>
114 {
115  inline unsigned operator()(const fileName& obj, unsigned seed=0) const
116  {
117  return string::hash()(obj, seed);
118  }
119 };
120 
121 
122 //- Hash specialization for wordRe
123 template<>
124 struct Hash<Foam::wordRe>
125 {
126  inline unsigned operator()(const wordRe& obj, unsigned seed=0) const
127  {
128  return string::hash()(obj, seed);
129  }
130 };
131 
132 
133 //- Hash specialization for keyType
134 template<>
135 struct Hash<Foam::keyType>
136 {
137  inline unsigned operator()(const keyType& obj, unsigned seed=0) const
138  {
139  return string::hash()(obj, seed);
140  }
141 };
142 
143 
144 //- Hash specialization for pointers, interpret pointer as a integer type.
145 template<>
146 struct Hash<void*>
147 {
148  inline unsigned operator()(const void* const& obj, unsigned seed) const
149  {
150  return Hash<intptr_t>()(intptr_t(obj), seed);
151  }
152 
153  inline unsigned operator()(const void* const& obj) const
154  {
155  return Hash<intptr_t>()(intptr_t(obj));
156  }
157 };
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 } // End namespace Foam
163 
164 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
165 
166 #endif
167 
168 // ************************************************************************* //
Foam::Hash< Foam::fileName >::operator()
unsigned operator()(const fileName &obj, unsigned seed=0) const
Definition: Hash.H:114
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::Hash< Foam::word >::operator()
unsigned operator()(const word &obj, unsigned seed=0) const
Definition: Hash.H:103
Foam::Hash< void * >::operator()
unsigned operator()(const void *const &obj) const
Definition: Hash.H:152
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:473
Foam::Hash::operator()
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:57
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::Hash< Foam::keyType >::operator()
unsigned operator()(const keyType &obj, unsigned seed=0) const
Definition: Hash.H:136
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::Hash
Hash function class. The default definition is for primitives, non-primitives used to hash entries on...
Definition: Hash.H:55
wordRe.H
Hasher.H
Misc. hashing functions, mostly from Bob Jenkins.
Foam::string::hash
Hashing function for string and derived string classes.
Definition: string.H:151
Foam::Hash< Foam::wordRe >::operator()
unsigned operator()(const wordRe &obj, unsigned seed=0) const
Definition: Hash.H:125
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
fileName.H
Foam::Hash< void * >::operator()
unsigned operator()(const void *const &obj, unsigned seed) const
Definition: Hash.H:147
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
label.H
uLabel.H
Foam::Hash< Foam::string >::operator()
unsigned operator()(const string &obj, unsigned seed=0) const
Definition: Hash.H:92