2010-12-31 11:56:15 +00:00
|
|
|
/* Hash table implementation.
|
2010-12-31 11:46:48 +00:00
|
|
|
*
|
|
|
|
* This file implements in memory hash tables with insert/del/replace/find/
|
|
|
|
* get-random-element operations. Hash tables will auto resize if needed
|
|
|
|
* tables of power of two in size are used, collisions are handled by
|
|
|
|
* chaining. See the source code for more information... :)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fmacros.h"
|
2020-01-28 20:13:05 +00:00
|
|
|
#include "alloc.h"
|
2010-12-31 11:46:48 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "dict.h"
|
|
|
|
|
|
|
|
/* -------------------------- private prototypes ---------------------------- */
|
|
|
|
|
|
|
|
static int _dictExpandIfNeeded(dict *ht);
|
|
|
|
static unsigned long _dictNextPower(unsigned long size);
|
|
|
|
static int _dictKeyIndex(dict *ht, const void *key);
|
|
|
|
static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
|
|
|
|
|
|
|
|
/* -------------------------- hash functions -------------------------------- */
|
|
|
|
|
|
|
|
/* Generic hash function (a popular one from Bernstein).
|
|
|
|
* I tested a few and this was the best. */
|
2011-01-14 11:07:24 +00:00
|
|
|
static unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
|
2010-12-31 11:46:48 +00:00
|
|
|
unsigned int hash = 5381;
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------- API implementation ------------------------- */
|
|
|
|
|
|
|
|
/* Reset an hashtable already initialized with ht_init().
|
|
|
|
* NOTE: This function should only called by ht_destroy(). */
|
2010-12-31 11:56:15 +00:00
|
|
|
static void _dictReset(dict *ht) {
|
2010-12-31 11:46:48 +00:00
|
|
|
ht->table = NULL;
|
|
|
|
ht->size = 0;
|
|
|
|
ht->sizemask = 0;
|
|
|
|
ht->used = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new hash table */
|
2011-01-14 11:07:24 +00:00
|
|
|
static dict *dictCreate(dictType *type, void *privDataPtr) {
|
2020-01-28 20:13:05 +00:00
|
|
|
dict *ht = hi_malloc(sizeof(*ht));
|
2020-05-22 16:27:49 +00:00
|
|
|
if (ht == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-31 11:46:48 +00:00
|
|
|
_dictInit(ht,type,privDataPtr);
|
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the hash table */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int _dictInit(dict *ht, dictType *type, void *privDataPtr) {
|
2010-12-31 11:46:48 +00:00
|
|
|
_dictReset(ht);
|
|
|
|
ht->type = type;
|
|
|
|
ht->privdata = privDataPtr;
|
|
|
|
return DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand or create the hashtable */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int dictExpand(dict *ht, unsigned long size) {
|
2010-12-31 11:46:48 +00:00
|
|
|
dict n; /* the new hashtable */
|
|
|
|
unsigned long realsize = _dictNextPower(size), i;
|
|
|
|
|
|
|
|
/* the size is invalid if it is smaller than the number of
|
|
|
|
* elements already inside the hashtable */
|
|
|
|
if (ht->used > size)
|
|
|
|
return DICT_ERR;
|
|
|
|
|
|
|
|
_dictInit(&n, ht->type, ht->privdata);
|
|
|
|
n.size = realsize;
|
|
|
|
n.sizemask = realsize-1;
|
2020-05-22 16:27:49 +00:00
|
|
|
n.table = hi_calloc(realsize,sizeof(dictEntry*));
|
|
|
|
if (n.table == NULL)
|
|
|
|
return DICT_ERR;
|
2010-12-31 11:46:48 +00:00
|
|
|
|
|
|
|
/* Copy all the elements from the old to the new table:
|
|
|
|
* note that if the old hash table is empty ht->size is zero,
|
|
|
|
* so dictExpand just creates an hash table. */
|
|
|
|
n.used = ht->used;
|
|
|
|
for (i = 0; i < ht->size && ht->used > 0; i++) {
|
|
|
|
dictEntry *he, *nextHe;
|
|
|
|
|
|
|
|
if (ht->table[i] == NULL) continue;
|
2010-12-31 11:51:35 +00:00
|
|
|
|
2010-12-31 11:46:48 +00:00
|
|
|
/* For each hash entry on this slot... */
|
|
|
|
he = ht->table[i];
|
|
|
|
while(he) {
|
|
|
|
unsigned int h;
|
|
|
|
|
|
|
|
nextHe = he->next;
|
|
|
|
/* Get the new element index */
|
|
|
|
h = dictHashKey(ht, he->key) & n.sizemask;
|
|
|
|
he->next = n.table[h];
|
|
|
|
n.table[h] = he;
|
|
|
|
ht->used--;
|
|
|
|
/* Pass to the next element */
|
|
|
|
he = nextHe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(ht->used == 0);
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(ht->table);
|
2010-12-31 11:46:48 +00:00
|
|
|
|
|
|
|
/* Remap the new hashtable in the old */
|
|
|
|
*ht = n;
|
|
|
|
return DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an element to the target hash table */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int dictAdd(dict *ht, void *key, void *val) {
|
2010-12-31 11:46:48 +00:00
|
|
|
int index;
|
|
|
|
dictEntry *entry;
|
|
|
|
|
|
|
|
/* Get the index of the new element, or -1 if
|
|
|
|
* the element already exists. */
|
|
|
|
if ((index = _dictKeyIndex(ht, key)) == -1)
|
|
|
|
return DICT_ERR;
|
|
|
|
|
|
|
|
/* Allocates the memory and stores key */
|
2020-01-28 20:13:05 +00:00
|
|
|
entry = hi_malloc(sizeof(*entry));
|
2020-05-22 16:27:49 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
return DICT_ERR;
|
|
|
|
|
2010-12-31 11:46:48 +00:00
|
|
|
entry->next = ht->table[index];
|
|
|
|
ht->table[index] = entry;
|
|
|
|
|
|
|
|
/* Set the hash entry fields. */
|
|
|
|
dictSetHashKey(ht, entry, key);
|
|
|
|
dictSetHashVal(ht, entry, val);
|
|
|
|
ht->used++;
|
|
|
|
return DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an element, discarding the old if the key already exists.
|
|
|
|
* Return 1 if the key was added from scratch, 0 if there was already an
|
|
|
|
* element with such key and dictReplace() just performed a value update
|
|
|
|
* operation. */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int dictReplace(dict *ht, void *key, void *val) {
|
2010-12-31 11:46:48 +00:00
|
|
|
dictEntry *entry, auxentry;
|
|
|
|
|
|
|
|
/* Try to add the element. If the key
|
2015-10-27 17:19:24 +00:00
|
|
|
* does not exists dictAdd will succeed. */
|
2010-12-31 11:46:48 +00:00
|
|
|
if (dictAdd(ht, key, val) == DICT_OK)
|
|
|
|
return 1;
|
|
|
|
/* It already exists, get the entry */
|
|
|
|
entry = dictFind(ht, key);
|
2020-05-22 16:27:49 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2010-12-31 11:46:48 +00:00
|
|
|
/* Free the old value and set the new one */
|
|
|
|
/* Set the new value and free the old one. Note that it is important
|
|
|
|
* to do that in this order, as the value may just be exactly the same
|
|
|
|
* as the previous one. In this context, think to reference counting,
|
|
|
|
* you want to increment (set), and then decrement (free), and not the
|
|
|
|
* reverse. */
|
|
|
|
auxentry = *entry;
|
|
|
|
dictSetHashVal(ht, entry, val);
|
|
|
|
dictFreeEntryVal(ht, &auxentry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search and remove an element */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int dictDelete(dict *ht, const void *key) {
|
2010-12-31 11:46:48 +00:00
|
|
|
unsigned int h;
|
2011-01-14 11:07:24 +00:00
|
|
|
dictEntry *de, *prevde;
|
2010-12-31 11:46:48 +00:00
|
|
|
|
|
|
|
if (ht->size == 0)
|
|
|
|
return DICT_ERR;
|
|
|
|
h = dictHashKey(ht, key) & ht->sizemask;
|
2011-01-14 11:07:24 +00:00
|
|
|
de = ht->table[h];
|
2010-12-31 11:46:48 +00:00
|
|
|
|
2011-01-14 11:07:24 +00:00
|
|
|
prevde = NULL;
|
|
|
|
while(de) {
|
|
|
|
if (dictCompareHashKeys(ht,key,de->key)) {
|
2010-12-31 11:46:48 +00:00
|
|
|
/* Unlink the element from the list */
|
2011-01-14 11:07:24 +00:00
|
|
|
if (prevde)
|
|
|
|
prevde->next = de->next;
|
2010-12-31 11:46:48 +00:00
|
|
|
else
|
2011-01-14 11:07:24 +00:00
|
|
|
ht->table[h] = de->next;
|
|
|
|
|
|
|
|
dictFreeEntryKey(ht,de);
|
|
|
|
dictFreeEntryVal(ht,de);
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(de);
|
2010-12-31 11:46:48 +00:00
|
|
|
ht->used--;
|
|
|
|
return DICT_OK;
|
|
|
|
}
|
2011-01-14 11:07:24 +00:00
|
|
|
prevde = de;
|
|
|
|
de = de->next;
|
2010-12-31 11:46:48 +00:00
|
|
|
}
|
|
|
|
return DICT_ERR; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy an entire hash table */
|
2011-01-14 11:07:24 +00:00
|
|
|
static int _dictClear(dict *ht) {
|
2010-12-31 11:46:48 +00:00
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
/* Free all the elements */
|
|
|
|
for (i = 0; i < ht->size && ht->used > 0; i++) {
|
|
|
|
dictEntry *he, *nextHe;
|
|
|
|
|
|
|
|
if ((he = ht->table[i]) == NULL) continue;
|
|
|
|
while(he) {
|
|
|
|
nextHe = he->next;
|
|
|
|
dictFreeEntryKey(ht, he);
|
|
|
|
dictFreeEntryVal(ht, he);
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(he);
|
2010-12-31 11:46:48 +00:00
|
|
|
ht->used--;
|
|
|
|
he = nextHe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Free the table and the allocated cache structure */
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(ht->table);
|
2010-12-31 11:46:48 +00:00
|
|
|
/* Re-initialize the table */
|
|
|
|
_dictReset(ht);
|
|
|
|
return DICT_OK; /* never fails */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear & Release the hash table */
|
2011-01-14 11:07:24 +00:00
|
|
|
static void dictRelease(dict *ht) {
|
2010-12-31 11:46:48 +00:00
|
|
|
_dictClear(ht);
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(ht);
|
2010-12-31 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 11:07:24 +00:00
|
|
|
static dictEntry *dictFind(dict *ht, const void *key) {
|
2010-12-31 11:46:48 +00:00
|
|
|
dictEntry *he;
|
|
|
|
unsigned int h;
|
|
|
|
|
|
|
|
if (ht->size == 0) return NULL;
|
|
|
|
h = dictHashKey(ht, key) & ht->sizemask;
|
|
|
|
he = ht->table[h];
|
|
|
|
while(he) {
|
|
|
|
if (dictCompareHashKeys(ht, key, he->key))
|
|
|
|
return he;
|
|
|
|
he = he->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-14 11:07:24 +00:00
|
|
|
static dictIterator *dictGetIterator(dict *ht) {
|
2020-01-28 20:13:05 +00:00
|
|
|
dictIterator *iter = hi_malloc(sizeof(*iter));
|
2020-05-22 16:27:49 +00:00
|
|
|
if (iter == NULL)
|
|
|
|
return NULL;
|
2010-12-31 11:46:48 +00:00
|
|
|
|
|
|
|
iter->ht = ht;
|
|
|
|
iter->index = -1;
|
|
|
|
iter->entry = NULL;
|
|
|
|
iter->nextEntry = NULL;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2011-01-14 11:07:24 +00:00
|
|
|
static dictEntry *dictNext(dictIterator *iter) {
|
2010-12-31 11:46:48 +00:00
|
|
|
while (1) {
|
|
|
|
if (iter->entry == NULL) {
|
|
|
|
iter->index++;
|
|
|
|
if (iter->index >=
|
|
|
|
(signed)iter->ht->size) break;
|
|
|
|
iter->entry = iter->ht->table[iter->index];
|
|
|
|
} else {
|
|
|
|
iter->entry = iter->nextEntry;
|
|
|
|
}
|
|
|
|
if (iter->entry) {
|
|
|
|
/* We need to save the 'next' here, the iterator user
|
|
|
|
* may delete the entry we are returning. */
|
|
|
|
iter->nextEntry = iter->entry->next;
|
|
|
|
return iter->entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-14 11:07:24 +00:00
|
|
|
static void dictReleaseIterator(dictIterator *iter) {
|
2020-05-22 16:27:49 +00:00
|
|
|
hi_free(iter);
|
2010-12-31 11:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------- private functions ------------------------------ */
|
|
|
|
|
|
|
|
/* Expand the hash table if needed */
|
2010-12-31 11:56:15 +00:00
|
|
|
static int _dictExpandIfNeeded(dict *ht) {
|
2015-10-27 17:19:24 +00:00
|
|
|
/* If the hash table is empty expand it to the initial size,
|
2019-11-19 16:00:00 +00:00
|
|
|
* if the table is "full" double its size. */
|
2010-12-31 11:46:48 +00:00
|
|
|
if (ht->size == 0)
|
|
|
|
return dictExpand(ht, DICT_HT_INITIAL_SIZE);
|
|
|
|
if (ht->used == ht->size)
|
|
|
|
return dictExpand(ht, ht->size*2);
|
|
|
|
return DICT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Our hash table capability is a power of two */
|
2010-12-31 11:56:15 +00:00
|
|
|
static unsigned long _dictNextPower(unsigned long size) {
|
2010-12-31 11:46:48 +00:00
|
|
|
unsigned long i = DICT_HT_INITIAL_SIZE;
|
|
|
|
|
|
|
|
if (size >= LONG_MAX) return LONG_MAX;
|
|
|
|
while(1) {
|
|
|
|
if (i >= size)
|
|
|
|
return i;
|
|
|
|
i *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the index of a free slot that can be populated with
|
|
|
|
* an hash entry for the given 'key'.
|
|
|
|
* If the key already exists, -1 is returned. */
|
2010-12-31 11:56:15 +00:00
|
|
|
static int _dictKeyIndex(dict *ht, const void *key) {
|
2010-12-31 11:46:48 +00:00
|
|
|
unsigned int h;
|
|
|
|
dictEntry *he;
|
|
|
|
|
|
|
|
/* Expand the hashtable if needed */
|
|
|
|
if (_dictExpandIfNeeded(ht) == DICT_ERR)
|
|
|
|
return -1;
|
|
|
|
/* Compute the key hash value */
|
|
|
|
h = dictHashKey(ht, key) & ht->sizemask;
|
|
|
|
/* Search if this slot does not already contain the given key */
|
|
|
|
he = ht->table[h];
|
|
|
|
while(he) {
|
|
|
|
if (dictCompareHashKeys(ht, key, he->key))
|
|
|
|
return -1;
|
|
|
|
he = he->next;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|