hash_fn.h

00001 /*  _________________________________________________________________________
00002  *
00003  *  UTILIB: A utility library for developing portable C++ codes.
00004  *  Copyright (c) 2001, Sandia National Laboratories.
00005  *  This software is distributed under the GNU Lesser General Public License.
00006  *  For more information, see the README file in the top UTILIB directory.
00007  *  _________________________________________________________________________
00008  */
00009 
00010 //
00011 // hash_fn.h
00012 //
00013 // Defines commonly used hash functions
00014 //
00015 
00016 #ifndef __hash_fn_h
00017 #define __hash_fn_h
00018 
00019 #include "BasicArray.h"
00020 #include "CharString.h"
00021 
00022 //
00023 // Declarations for the Bob Jenkins hash function (which is quite good!)
00024 //
00025 typedef unsigned long int  ub4; /* unsigned 4-byte quantities */
00026 typedef unsigned      char ub1; /* unsigned 1-byte quantities */
00027 ub4 bjlookup( register ub1* k, register ub4 length, register ub4 level);
00028 //
00029 // Declarations for hash functions based on the bjlookup function
00030 //
00032 inline unsigned long int hash_bj(const double& val, unsigned long int level=0)
00033         {return bjlookup((ub1*)&val,sizeof(double),level);}
00035 inline unsigned long int hash_bj(const int& val, unsigned long int level=0)
00036         {return bjlookup((ub1*)&val,sizeof(int),level);}
00038 inline unsigned long int hash_bj(const BasicArray<int>& array, 
00039                                                 unsigned long int level=0)
00040         {return bjlookup((ub1*)(array.data()),array.size()*sizeof(int),level);}
00042 inline unsigned long int hash_bj(const int* array, const int arraylen, 
00043                                                 unsigned long int level=0)
00044         {return bjlookup((ub1*)(array),arraylen*sizeof(int),level);}
00046 inline unsigned long int hash_bj(const BasicArray<double>& array, 
00047                                                 unsigned long int level=0)
00048         {return bjlookup((ub1*)(array.data()),array.size()*sizeof(double),level);}
00050 inline unsigned long int hash_bj(const double* array, const int arraylen, 
00051                                                 unsigned long int level=0)
00052         {return bjlookup((ub1*)(array),arraylen*sizeof(double),level);}
00053 
00054 
00055 
00057 template <class T>
00058 size_type hash_fn(const T& key, size_type table_size)
00059 {return key.hash(table_size);}
00060 template<class T>
00061 inline size_type hash_fn(const SimpleArray<T>& key, size_type table_size)
00062         {return hash_fn1(*((const BasicArray<T>*)&key),table_size);}
00063 
00064 
00065 
00066 //
00067 // CharString
00068 //
00069 size_type hash_fn1(const CharString& key, size_type table_size);
00070 size_type hash_fn2(const CharString& key, size_type table_size);
00071 size_type hash_fn3(const CharString& key, size_type table_size);
00072 template<>
00073 inline size_type hash_fn(const CharString& key, size_type table_size)
00074         {return hash_fn3(key,table_size);}
00075 
00076 
00077 //
00078 // int
00079 //
00080 size_type hash_fn1(const int& key, size_type table_size);
00081 inline size_type hash_fn2(const int& key, size_type table_size)
00082         {return hash_bj(key) % table_size;}
00083 template<>
00084 inline size_type hash_fn(const int& key, size_type table_size)
00085         {return hash_fn2(key,table_size);}
00086 
00087 
00088 //
00089 // double
00090 //
00091 size_type hash_fn1(const double& key, size_type table_size);
00092 inline size_type hash_fn2(const double& key, size_type table_size)
00093         {return hash_bj(key) % table_size;}
00094 template<>
00095 inline size_type hash_fn(const double& key, size_type table_size)
00096         {return hash_fn2(key,table_size);}
00097 
00098 
00099 //
00100 // BasicArray<int>
00101 //
00102 size_type hash_fn1(const BasicArray<int>& key, size_type table_size);
00103 inline size_type hash_fn2(const BasicArray<int>& key, size_type table_size)
00104         {return hash_bj(key) % table_size;}
00105 template<>
00106 inline size_type hash_fn(const BasicArray<int>& key, size_type table_size)
00107         {return hash_fn2(key,table_size);}
00108 
00109 
00110 //
00111 // BasicArray<double>
00112 //
00113 size_type hash_fn1(const BasicArray<double>& key, size_type table_size);
00114 size_type hash_fn2(const BasicArray<double>& key, size_type table_size);
00115 inline size_type hash_fn3(const BasicArray<double>& key, size_type table_size)
00116         {return hash_bj(key) % table_size;}
00117 template<>
00118 inline size_type hash_fn(const BasicArray<double>& key, size_type table_size)
00119         {return hash_fn3(key,table_size);}
00120 
00121 //
00122 // SimpleArray<double>
00123 //
00124 inline size_type hash_fn1(const SimpleArray<double>& key, size_type table_size)
00125                 {return hash_fn1(*((const BasicArray<double>*)&key),table_size);}
00126 inline size_type hash_fn2(const SimpleArray<double>& key, size_type table_size)
00127                 {return hash_fn2(*((const BasicArray<double>*)&key),table_size);}
00128 inline size_type hash_fn3(const SimpleArray<double>& key, size_type table_size)
00129                 {return hash_fn3(*((const BasicArray<double>*)&key),table_size);}
00130 template<>
00131 inline size_type hash_fn(const SimpleArray<double>& key, size_type table_size)
00132         {return hash_fn3(*((const BasicArray<double>*)&key),table_size);}
00133 
00134 //
00135 // SimpleArray<int>
00136 //
00137 inline size_type hash_fn1(const SimpleArray<int>& key, size_type table_size)
00138                 {return hash_fn1(*((const BasicArray<int>*)&key),table_size);}
00139 inline size_type hash_fn2(const SimpleArray<int>& key, size_type table_size)
00140                 {return hash_fn2(*((const BasicArray<int>*)&key),table_size);}
00141 template<>
00142 inline size_type hash_fn(const SimpleArray<int>& key, size_type table_size)
00143         {return hash_fn2(*((const BasicArray<int>*)&key),table_size);}
00144 
00145 #endif