viewing paste Unknown #6950 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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;
}
 
Viewed 245 times, submitted by unknown.