Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <iostream> 00003 using namespace std; 00004 00005 #include <stringhash.h> 00006 00007 00008 00009 stringhash::stringhash(uintc cyclelength_, uintc loops_) 00010 : hashfn(loops_), cyclelength(cyclelength_) {} 00011 00012 00013 uintc stringhash::operator ()(stringc & key) const 00014 { 00015 char * i; 00016 char * iend; 00017 assert(cyclelength!=0); 00018 assert(key.empty()==false); 00019 uintc keylen = key.length(); 00020 if (keylen>=cyclelength) 00021 { 00022 cout << "error: keylen=" << keylen << " cyclelength=" << cyclelength << endl; 00023 cout << "error: key=" << key.c_str() << endl; 00024 } 00025 assert(keylen<cyclelength); 00026 uint hi; 00027 00028 uint v[cyclelength]; 00029 uint * vend = v + cyclelength; 00030 uint * pv; 00031 00032 i = (char *)key.c_str(); 00033 iend = i + keylen; 00034 00035 pv = v; 00036 for ( ; i != iend; ) 00037 { 00038 *pv = *i; 00039 ++pv; 00040 ++i; 00041 } 00042 00043 for ( ; pv != vend; ++pv ) 00044 *pv = *(pv-keylen); 00045 00046 00047 // A rotating hash. 00048 hi = 0; 00049 for ( pv=v; pv!=vend; ++pv ) 00050 hi ^= ( *pv + (hi << 6 ) + (hi >> 2) ); 00051 00052 return hashfn(hi); 00053 } 00054 00055 00056 void stringhash::construct( uintc cyclelength_, uintc loops_ ) 00057 { 00058 cyclelength=cyclelength_; 00059 hashfn.construct(loops_); 00060 } 00061 00062 00063
1.5.8