OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Hash.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Hash
00027 
00028 Description
00029     Hash function class for primitives.  All non-primitives used to hash
00030     entries on hash tables likely need a specialized version of this class.
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #ifndef Hash_H
00035 #define Hash_H
00036 
00037 #include "label.H"
00038 #include "uLabel.H"
00039 #include "Hasher.H"
00040 #include "pTraits.H"
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 namespace Foam
00045 {
00046 
00047 /*---------------------------------------------------------------------------*\
00048                             Class Hash Declaration
00049 \*---------------------------------------------------------------------------*/
00050 
00051 template<class PrimitiveType>
00052 class Hash
00053 {
00054 public:
00055 
00056     Hash()
00057     {}
00058 
00059     unsigned operator()(const PrimitiveType& p, unsigned seed) const
00060     {
00061         return Hasher(&p, sizeof(p), seed);
00062     }
00063 
00064     unsigned operator()(const PrimitiveType& p) const
00065     {
00066         return Hasher(&p, sizeof(p));
00067     }
00068 
00069 };
00070 
00071 
00072 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00073 
00074 //- Hash specialization for hashing pointer addresses.
00075 //  Treat a pointer like a long.
00076 //  This should work for both 32-bit and 64-bit pointers.
00077 template<>
00078 class Hash<void*>
00079 {
00080 public:
00081 
00082     Hash()
00083     {}
00084 
00085     unsigned operator()(const void* const& p, unsigned seed) const
00086     {
00087         return Hash<long>()(long(p), seed);
00088     }
00089 
00090     unsigned operator()(const void* const& p) const
00091     {
00092         return Hash<long>()(long(p));
00093     }
00094 
00095 };
00096 
00097 
00098 //- Hash specialization for hashing labels
00099 template<>
00100 class Hash<Foam::label>
00101 {
00102 public:
00103 
00104     Hash()
00105     {}
00106 
00107     //- Incrementally hash a label.
00108     //  This will necessarily return a different value than the
00109     //  non-incremental version.
00110     unsigned operator()(const label& p, unsigned seed) const
00111     {
00112         return Hasher(&p, sizeof(label), seed);
00113     }
00114 
00115     //- Return the unsigned representation of a label.
00116     //  This helps if people have relied on the hash value corresponding to
00117     //  the natural order.
00118     unsigned operator()(const label& p) const
00119     {
00120         return p;
00121     }
00122 
00123 };
00124 
00125 
00126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00127 
00128 } // End namespace Foam
00129 
00130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00131 
00132 #endif
00133 
00134 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd