FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/label.H>
37 #include <OpenFOAM/uLabel.H>
38 #include <OpenFOAM/Hasher.H>
39 #include <OpenFOAM/pTraits.H>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class Hash Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 template<class PrimitiveType>
51 class Hash
52 {
53 public:
54 
55  Hash()
56  {}
57 
58  unsigned operator()(const PrimitiveType& p, unsigned seed) const
59  {
60  return Hasher(&p, sizeof(p), seed);
61  }
62 
63  unsigned operator()(const PrimitiveType& p) const
64  {
65  return Hasher(&p, sizeof(p));
66  }
67 
68 };
69 
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 //- Hash specialization for hashing pointer addresses.
74 // Treat a pointer like a long.
75 // This should work for both 32-bit and 64-bit pointers.
76 template<>
77 class Hash<void*>
78 {
79 public:
80 
81  Hash()
82  {}
83 
84  unsigned operator()(const void* const& p, unsigned seed) const
85  {
86  return Hash<long>()(long(p), seed);
87  }
88 
89  unsigned operator()(const void* const& p) const
90  {
91  return Hash<long>()(long(p));
92  }
93 
94 };
95 
96 
97 //- Hash specialization for hashing labels
98 template<>
99 class Hash<Foam::label>
100 {
101 public:
102 
104  {}
105 
106  //- Incrementally hash a label.
107  // This will necessarily return a different value than the
108  // non-incremental version.
109  unsigned operator()(const label& p, unsigned seed) const
110  {
111  return Hasher(&p, sizeof(label), seed);
112  }
113 
114  //- Return the unsigned representation of a label.
115  // This helps if people have relied on the hash value corresponding to
116  // the natural order.
117  unsigned operator()(const label& p) const
118  {
119  return p;
120  }
121 
122 };
123 
124 
125 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
126 
127 } // End namespace Foam
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 #endif
132 
133 // ************************ vim: set sw=4 sts=4 et: ************************ //