used like:
node = db->ht[db->hash(key, db->maxlen)%HASH_SIZE];
while (node) {
c = db->cmp(key, node->key, db->maxlen);
if (c == 0) {
if (!(node->deleted)) {
db->cache = node;
found = true;
}
break;
}
if (c < 0)
node = node->left;
else
node = node->right;
}
---
/**
* Default hasher for DB_INT databases.
* Returns the value of the key as an unsigned int.
* <code>maxlen</code> is ignored.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_INT
* @see #DBHasher
* @see #db_default_hash(DBType)
*/
static unsigned int db_int_hash(DBKey key, unsigned short maxlen)
{
(void)maxlen;//not used
DB_COUNTSTAT(db_int_hash);
return (unsigned int)key.i;
}
/**
* Default hasher for DB_UINT databases.
* Just returns the value of the key.
* <code>maxlen</code> is ignored.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_UINT
* @see #DBHasher
* @see #db_default_hash(DBType)
*/
static unsigned int db_uint_hash(DBKey key, unsigned short maxlen)
{
(void)maxlen;//not used
DB_COUNTSTAT(db_uint_hash);
return key.ui;
}
/**
* Default hasher for DB_STRING databases.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_STRING
* @see #DBHasher
* @see #db_default_hash(DBType)
*/
static unsigned int db_string_hash(DBKey key, unsigned short maxlen)
{
const char *k = key.str;
unsigned int hash = 0;
unsigned short i;
DB_COUNTSTAT(db_string_hash);
for (i = 0; *k; ++i) {
hash = (hash*33 + ((unsigned char)*k))^(hash>>24);
k++;
if (i == maxlen)
break;
}
return hash;
}
/**
* Default hasher for DB_ISTRING databases.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_ISTRING
* @see #db_default_hash(DBType)
*/
static unsigned int db_istring_hash(DBKey key, unsigned short maxlen)
{
const char *k = key.str;
unsigned int hash = 0;
unsigned short i;
DB_COUNTSTAT(db_istring_hash);
for (i = 0; *k; i++) {
hash = (hash*33 + ((unsigned char)TOLOWER(*k)))^(hash>>24);
k++;
if (i == maxlen)
break;
}
return hash;
}
/**
* Default hasher for DB_INT64 databases.
* Returns the value of the key as an unsigned int.
* <code>maxlen</code> is ignored.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_INT64
* @see #DBHasher
* @see #db_default_hash(DBType)
*/
static unsigned int db_int64_hash(DBKey key, unsigned short maxlen)
{
(void)maxlen;//not used
DB_COUNTSTAT(db_int64_hash);
return (unsigned int)key.i64;
}
/**
* Default hasher for DB_UINT64 databases.
* Just returns the value of the key.
* <code>maxlen</code> is ignored.
* @param key Key to be hashed
* @param maxlen Maximum length of the key to hash
* @return hash of the key
* @see DBType#DB_UINT64
* @see #DBHasher
* @see #db_default_hash(DBType)
*/
static unsigned int db_uint64_hash(DBKey key, unsigned short maxlen)
{
(void)maxlen;//not used
DB_COUNTSTAT(db_uint64_hash);
return (unsigned int)key.ui64;
}