initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / hashes / Hash / Hash.H
blobdd16517dfe041935831a907696969452666ce790
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::Hash
28 Description
29     Hash function class for primitives.  All non-primitives used to hash
30     entries on hash tables likely need a specialized version of this class.
32 \*---------------------------------------------------------------------------*/
34 #ifndef Hash_H
35 #define Hash_H
37 #include "label.H"
38 #include "uLabel.H"
39 #include "Hasher.H"
40 #include "pTraits.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 namespace Foam
47 /*---------------------------------------------------------------------------*\
48                             Class Hash Declaration
49 \*---------------------------------------------------------------------------*/
51 template<class PrimitiveType>
52 class Hash
54 public:
56     Hash()
57     {}
59     unsigned operator()(const PrimitiveType& p, unsigned seed) const
60     {
61         return Hasher(&p, sizeof(p), seed);
62     }
64     unsigned operator()(const PrimitiveType& p) const
65     {
66         return Hasher(&p, sizeof(p));
67     }
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74 //- Hash specialization for hashing pointer addresses.
75 //  Treat a pointer like a long.
76 //  This should work for both 32-bit and 64-bit pointers.
77 template<>
78 class Hash<void*>
80 public:
82     Hash()
83     {}
85     unsigned operator()(const void* const& p, unsigned seed) const
86     {
87         return Hash<long>()(long(p), seed);
88     }
90     unsigned operator()(const void* const& p) const
91     {
92         return Hash<long>()(long(p));
93     }
98 //- Hash specialization for hashing labels
99 template<>
100 class Hash<Foam::label>
102 public:
104     Hash()
105     {}
107     //- Incrementally hash a label.
108     //  This will necessarily return a different value than the
109     //  non-incremental version.
110     unsigned operator()(const label& p, unsigned seed) const
111     {
112         return Hasher(&p, sizeof(label), seed);
113     }
115     //- Return the unsigned representation of a label.
116     //  This helps if people have relied on the hash value corresponding to
117     //  the natural order.
118     unsigned operator()(const label& p) const
119     {
120         return p;
121     }
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 } // End namespace Foam
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 #endif
134 // ************************************************************************* //