return key;
}
+static int dict_hash_function_seed = 5381;
+
+void dictSetHashFunctionSeed(unsigned int seed) {
+ dict_hash_function_seed = seed;
+}
+
+unsigned int dictGetHashFunctionSeed(void) {
+ return dict_hash_function_seed;
+}
+
/* Generic hash function (a popular one from Bernstein).
* I tested a few and this was the best. */
unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
- unsigned int hash = 5381;
+ unsigned int hash = dict_hash_function_seed;
while (len--)
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
/* And a case insensitive version */
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
- unsigned int hash = 5381;
+ unsigned int hash = dict_hash_function_seed;
while (len--)
hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */
}
/* Resize the table to the minimal size that contains all the elements,
- * but with the invariant of a USER/BUCKETS ratio near to <= 1 */
+ * but with the invariant of a USED/BUCKETS ratio near to <= 1 */
int dictResize(dict *d)
{
int minimal;
dictEntry *entry = dictAddRaw(d,key);
if (!entry) return DICT_ERR;
- dictSetHashVal(d, entry, val);
+ dictSetVal(d, entry, val);
return DICT_OK;
}
* mainly in order to store non-pointers inside the hash value, example:
*
* entry = dictAddRaw(dict,mykey);
- * if (entry != NULL) dictSetValSignedInteger(entry,1000);
+ * if (entry != NULL) dictSetSignedIntegerVal(entry,1000);
*
* Return values:
*
ht->used++;
/* Set the hash entry fields. */
- dictSetHashKey(d, entry, key);
+ dictSetKey(d, entry, key);
return entry;
}
* you want to increment (set), and then decrement (free), and not the
* reverse. */
auxentry = *entry;
- dictSetHashVal(d, entry, val);
- dictFreeEntryVal(d, &auxentry);
+ dictSetVal(d, entry, val);
+ dictFreeVal(d, &auxentry);
return 0;
}
he = d->ht[table].table[idx];
prevHe = NULL;
while(he) {
- if (dictCompareHashKeys(d, key, he->key)) {
+ if (dictCompareKeys(d, key, he->key)) {
/* Unlink the element from the list */
if (prevHe)
prevHe->next = he->next;
else
d->ht[table].table[idx] = he->next;
if (!nofree) {
- dictFreeEntryKey(d, he);
- dictFreeEntryVal(d, he);
+ dictFreeKey(d, he);
+ dictFreeVal(d, he);
}
zfree(he);
d->ht[table].used--;
if ((he = ht->table[i]) == NULL) continue;
while(he) {
nextHe = he->next;
- dictFreeEntryKey(d, he);
- dictFreeEntryVal(d, he);
+ dictFreeKey(d, he);
+ dictFreeVal(d, he);
zfree(he);
ht->used--;
he = nextHe;
idx = h & d->ht[table].sizemask;
he = d->ht[table].table[idx];
while(he) {
- if (dictCompareHashKeys(d, key, he->key))
+ if (dictCompareKeys(d, key, he->key))
return he;
he = he->next;
}
dictEntry *he;
he = dictFind(d,key);
- return he ? dictGetEntryVal(he) : NULL;
+ return he ? dictGetVal(he) : NULL;
}
dictIterator *dictGetIterator(dict *d)
/* Search if this slot does not already contain the given key */
he = d->ht[table].table[idx];
while(he) {
- if (dictCompareHashKeys(d, key, he->key))
+ if (dictCompareKeys(d, key, he->key))
return -1;
he = he->next;
}
d->iterators = 0;
}
+void dictEnableResize(void) {
+ dict_can_resize = 1;
+}
+
+void dictDisableResize(void) {
+ dict_can_resize = 0;
+}
+
+#if 0
+
+/* The following is code that we don't use for Redis currently, but that is part
+of the library. */
+
+/* ----------------------- Debugging ------------------------*/
+
#define DICT_STATS_VECTLEN 50
static void _dictPrintStatsHt(dictht *ht) {
unsigned long i, slots = 0, chainlen, maxchainlen = 0;
}
}
-void dictEnableResize(void) {
- dict_can_resize = 1;
-}
-
-void dictDisableResize(void) {
- dict_can_resize = 0;
-}
-
-#if 0
-
-/* The following are just example hash table types implementations.
- * Not useful for Redis so they are commented out.
- */
-
/* ----------------------- StringCopy Hash Table Type ------------------------*/
static unsigned int _dictStringCopyHTHashFunction(const void *key)