00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 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 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your 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, see <http://www.gnu.org/licenses/>. 00023 00024 Class 00025 Foam::Hash 00026 00027 Description 00028 Hash function class for primitives. All non-primitives used to hash 00029 entries on hash tables likely need a specialized version of this class. 00030 00031 \*---------------------------------------------------------------------------*/ 00032 00033 #ifndef Hash_H 00034 #define Hash_H 00035 00036 #include <OpenFOAM/label.H> 00037 #include <OpenFOAM/uLabel.H> 00038 #include <OpenFOAM/Hasher.H> 00039 #include <OpenFOAM/pTraits.H> 00040 00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00042 00043 namespace Foam 00044 { 00045 00046 /*---------------------------------------------------------------------------*\ 00047 Class Hash Declaration 00048 \*---------------------------------------------------------------------------*/ 00049 00050 template<class PrimitiveType> 00051 class Hash 00052 { 00053 public: 00054 00055 Hash() 00056 {} 00057 00058 unsigned operator()(const PrimitiveType& p, unsigned seed) const 00059 { 00060 return Hasher(&p, sizeof(p), seed); 00061 } 00062 00063 unsigned operator()(const PrimitiveType& p) const 00064 { 00065 return Hasher(&p, sizeof(p)); 00066 } 00067 00068 }; 00069 00070 00071 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00072 00073 //- Hash specialization for hashing pointer addresses. 00074 // Treat a pointer like a long. 00075 // This should work for both 32-bit and 64-bit pointers. 00076 template<> 00077 class Hash<void*> 00078 { 00079 public: 00080 00081 Hash() 00082 {} 00083 00084 unsigned operator()(const void* const& p, unsigned seed) const 00085 { 00086 return Hash<long>()(long(p), seed); 00087 } 00088 00089 unsigned operator()(const void* const& p) const 00090 { 00091 return Hash<long>()(long(p)); 00092 } 00093 00094 }; 00095 00096 00097 //- Hash specialization for hashing labels 00098 template<> 00099 class Hash<Foam::label> 00100 { 00101 public: 00102 00103 Hash() 00104 {} 00105 00106 //- Incrementally hash a label. 00107 // This will necessarily return a different value than the 00108 // non-incremental version. 00109 unsigned operator()(const label& p, unsigned seed) const 00110 { 00111 return Hasher(&p, sizeof(label), seed); 00112 } 00113 00114 //- Return the unsigned representation of a label. 00115 // This helps if people have relied on the hash value corresponding to 00116 // the natural order. 00117 unsigned operator()(const label& p) const 00118 { 00119 return p; 00120 } 00121 00122 }; 00123 00124 00125 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00126 00127 } // End namespace Foam 00128 00129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00130 00131 #endif 00132 00133 // ************************ vim: set sw=4 sts=4 et: ************************ //