pkgcache.h: improved string hashing

This commit is contained in:
Alexey Tourbin 2011-01-27 00:37:00 +03:00
parent 31e94207f5
commit 8fbf622172

View File

@ -110,7 +110,7 @@ class pkgCache
// CNC:2003-02-16 - Inlined here. // CNC:2003-02-16 - Inlined here.
inline unsigned long sHash(const char *S) const; inline unsigned long sHash(const char *S) const;
inline unsigned long sHash(string S) const {return sHash(S.c_str());}; inline unsigned long sHash(const string &S) const {return sHash(S.c_str());};
public: public:
@ -131,8 +131,8 @@ class pkgCache
inline void *DataEnd() {return ((unsigned char *)Map.Data()) + Map.Size();}; inline void *DataEnd() {return ((unsigned char *)Map.Data()) + Map.Size();};
// String hashing function (512 range) // String hashing function (512 range)
inline unsigned long Hash(string S) const {return sHash(S);}; inline unsigned long Hash(const char *S) const;
inline unsigned long Hash(const char *S) const {return sHash(S);}; inline unsigned long Hash(const string &S) const {return Hash(S.c_str());};
// Usefull transformation things // Usefull transformation things
const char *Priority(unsigned char Priority); const char *Priority(unsigned char Priority);
@ -312,15 +312,18 @@ struct pkgCache::StringItem
#include <apt-pkg/cacheiterators.h> #include <apt-pkg/cacheiterators.h>
// CNC:2003-02-16 - Inlined here. // CNC:2003-02-16 - Inlined here.
#include <ctype.h> inline unsigned long pkgCache::sHash(const char *S) const
#define hash_count(a) (sizeof(a)/sizeof(a[0]))
inline unsigned long pkgCache::sHash(const char *Str) const
{ {
unsigned long Hash = 0; unsigned long h = 5381;
for (const char *I = Str; *I != 0; I++) unsigned char c;
//Hash = 5*Hash + tolower(*I); for (c = *S; c != '\0'; c = *++S)
Hash = 5*Hash + *I; h = h * 33 + c;
return Hash % hash_count(HeaderP->HashTable); return h;
}
#define hash_count(a) (sizeof(a)/sizeof(a[0]))
inline unsigned long pkgCache::Hash(const char *S) const
{
return sHash(S) % hash_count(HeaderP->HashTable);
} }
#undef hash_count #undef hash_count