Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

HashUtility.h

Go to the documentation of this file.
00001 /* SimData: Data Infrastructure for Simulations
00002  * Copyright (C) 2002 Mark Rose <tm2@stm.lbl.gov>
00003  * 
00004  * This file is part of SimData.
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 
00027 #ifndef __SIMDATA_HASHUTILITY_H__
00028 #define __SIMDATA_HASHUTILITY_H__
00029 
00030 # if defined(_MSC_VER) && (_MSC_VER <= 1300)
00031 #pragma warning(disable : 4251)
00032 # endif
00033 
00034 
00035 #include <SimData/Export.h>
00036 #include <SimData/Uniform.h>
00037 #include <SimData/hash_map.h>
00038 #include <SimData/Namespace.h>
00039 
00040 #include <iostream>
00041 #include <string>
00042 
00043 
00044 NAMESPACE_SIMDATA
00045 
00046 
00047 
00053 
00054 struct HashT;
00055 
00056 extern SIMDATA_EXPORT uint32 newhash4_cstring(std::string const &);
00057 extern SIMDATA_EXPORT HashT newhasht_cstring(std::string const &);
00058 
00059 
00062 struct SIMDATA_EXPORT HashT {
00063         uint32 a, b;
00064 
00067         HashT() {
00068                 a = b = 0;
00069         }
00070 
00077         HashT(uint32 x) {
00078                 a = x; b = 0;
00079         }
00080 
00086         HashT(uint32 b_, uint32 a_) {
00087                 a = a_; b = b_;
00088         }
00089 
00092         HashT(const HashT &x) {
00093                 a = x.a; b = x.b;
00094         }
00095 
00098         HashT(std::string const &x) {
00099                 *this = newhasht_cstring(x);
00100         }
00101 
00107         bool operator ==(uint32 x) const {
00108                 return (b == 0 && a == x);
00109         }
00110 
00116         bool operator !=(uint32 x) const {
00117                 return (b != 0 || a != x);
00118         }
00119 
00122         bool operator ==(HashT const &x) const {
00123                 return (b == x.b && a == x.a);
00124         }
00125 
00128         bool operator !=(HashT const &x) const {
00129                 return (b != x.b || a != x.a);
00130         }
00131 
00134         HashT & operator =(std::string &x) {
00135                 *this = newhasht_cstring(x);
00136                 return *this;
00137         }
00138 
00144         HashT & operator =(uint32 x) {
00145                 a = x; b = 0;
00146                 return *this;
00147         }
00148 
00151         HashT & operator =(HashT const &x) {
00152                 a = x.a; b = x.b;
00153                 return *this;
00154         }
00155 
00158         std::string str() const;
00159 };
00160 
00161 typedef HashT hasht;
00162 
00165 inline HashT hash_string(std::string const &key) { return newhasht_cstring(key); }
00166 
00167 
00168 
00171 #if defined(_MSC_VER) && (_MSC_VER >= 1300) 
00172         struct eqint: public std::hash_compare<int const> {
00173                 size_t operator ()(int const& i) const {
00174                         return i;
00175                 }
00176                 bool operator()(int const& i1, int const& i2) const {
00177                         return (i1 < i2);
00178                 }
00179         };
00180 #else
00181         struct eqint {
00182                 bool operator()(int i1, int i2) const {
00183                         return (i1 == i2);
00184                 }
00185         };
00186 #endif
00187 
00190 struct hashint {
00191         int operator()(int i) const {
00192                 return i;
00193         }
00194 };
00195 
00196 
00199 #if defined(_MSC_VER) && (_MSC_VER >= 1300) 
00200         struct eqstr: public std::hash_compare<char const*>
00201                 {
00202                         size_t operator()(char const* s ) const {
00203                                 return newhash4_cstring(s);
00204                         }
00205                         bool operator()(char const* s1, char const* s2) const {
00206                                 return (strcmp(s1, s2) < 0);
00207                         }
00208                 };
00209 #else
00210         struct eqstr {
00211                 bool operator()(const char* s1, const char* s2) const {
00212                         return (strcmp(s1, s2) == 0);
00213                 }
00214         
00215         };
00216 #endif
00217 
00220 struct hasht_hash {
00221         uint32 operator()(hasht i1) const {
00222                 return i1.a;
00223         }
00224 };
00225 
00228 #if defined(_MSC_VER) && (_MSC_VER >= 1300) 
00229         struct hasht_eq: public std::hash_compare<hasht const> {
00230                 size_t operator()( hasht const& i1 ) const {
00231                 return i1.a;
00232                         }
00233                 bool operator()(hasht const& i1, hasht const& i2) const {
00234                         return (i1.a < i2.a || (i1.a == i2.a && i1.b < i2.b));
00235                 }
00236         };
00237 #else
00238         struct hasht_eq {
00239                 bool operator()(hasht i1, hasht i2) const {
00240                         return ((i1.a == i2.a) && (i1.b == i2.b));
00241                 }
00242         };
00243 #endif
00244 
00245 
00248 struct SIMDATA_EXPORT hashstring {
00249         static HASH<const char*> h;
00250         int operator()(const std::string &s) const {
00251                 return h(s.c_str());
00252         }
00253 };
00254 
00257 #if defined(_MSC_VER) && (_MSC_VER >= 1300) 
00258         struct eqstring: public std::hash_compare<std::string const> {
00259                 size_t operator()( std::string const& a ) const {
00260                         return eqstr()(a.c_str());
00261                 }
00262                 bool operator()(std::string const& a, std::string const& b) const {
00263                                 return (a.compare(b) < 0);
00264                 }
00265         };
00266 #else
00267         struct eqstring {
00268                 bool operator()(const std::string &a, const std::string &b) const {
00269                         return a == b;
00270                 }
00271         };
00272 #endif
00273 
00274 
00277 template <class T>
00278 struct HASHT_MAP {
00279         typedef typename HASH_MAPS<hasht, T, hasht_hash, hasht_eq>::Type Type;
00280 };
00281 
00282 extern std::ostream & operator<<(std::ostream &o, const hasht &x);
00283 
00284 
00286 
00287 
00288 NAMESPACE_SIMDATA_END
00289 
00290 
00291 #endif // __SIMDATA_HASHUTILITY_H__
00292 

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:38 2003, using Doxygen 1.2.18.

[SF.net]