|
|
@ -39,6 +39,12 @@
|
|
|
|
#include <limits.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "dict.h"
|
|
|
|
#include "dict.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
|
|
|
#define __UNUSED_FUNCTION__ __attribute__ ((unused))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define __UNUSED_FUNCTION__
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------- private prototypes ---------------------------- */
|
|
|
|
/* -------------------------- private prototypes ---------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static int _dictExpandIfNeeded(dict *ht);
|
|
|
|
static int _dictExpandIfNeeded(dict *ht);
|
|
|
@ -50,6 +56,7 @@ static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
|
|
|
|
|
|
|
|
|
|
|
|
/* Generic hash function (a popular one from Bernstein).
|
|
|
|
/* Generic hash function (a popular one from Bernstein).
|
|
|
|
* I tested a few and this was the best. */
|
|
|
|
* I tested a few and this was the best. */
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
|
|
|
|
static unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
|
|
|
|
unsigned int hash = 5381;
|
|
|
|
unsigned int hash = 5381;
|
|
|
|
|
|
|
|
|
|
|
@ -70,6 +77,7 @@ static void _dictReset(dict *ht) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a new hash table */
|
|
|
|
/* Create a new hash table */
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static dict *dictCreate(dictType *type, void *privDataPtr) {
|
|
|
|
static dict *dictCreate(dictType *type, void *privDataPtr) {
|
|
|
|
dict *ht = malloc(sizeof(*ht));
|
|
|
|
dict *ht = malloc(sizeof(*ht));
|
|
|
|
_dictInit(ht,type,privDataPtr);
|
|
|
|
_dictInit(ht,type,privDataPtr);
|
|
|
@ -157,6 +165,7 @@ static int dictAdd(dict *ht, void *key, void *val) {
|
|
|
|
* Return 1 if the key was added from scratch, 0 if there was already an
|
|
|
|
* 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
|
|
|
|
* element with such key and dictReplace() just performed a value update
|
|
|
|
* operation. */
|
|
|
|
* operation. */
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static int dictReplace(dict *ht, void *key, void *val) {
|
|
|
|
static int dictReplace(dict *ht, void *key, void *val) {
|
|
|
|
dictEntry *entry, auxentry;
|
|
|
|
dictEntry *entry, auxentry;
|
|
|
|
|
|
|
|
|
|
|
@ -179,6 +188,7 @@ static int dictReplace(dict *ht, void *key, void *val) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Search and remove an element */
|
|
|
|
/* Search and remove an element */
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static int dictDelete(dict *ht, const void *key) {
|
|
|
|
static int dictDelete(dict *ht, const void *key) {
|
|
|
|
unsigned int h;
|
|
|
|
unsigned int h;
|
|
|
|
dictEntry *de, *prevde;
|
|
|
|
dictEntry *de, *prevde;
|
|
|
@ -235,6 +245,7 @@ static int _dictClear(dict *ht) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear & Release the hash table */
|
|
|
|
/* Clear & Release the hash table */
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static void dictRelease(dict *ht) {
|
|
|
|
static void dictRelease(dict *ht) {
|
|
|
|
_dictClear(ht);
|
|
|
|
_dictClear(ht);
|
|
|
|
free(ht);
|
|
|
|
free(ht);
|
|
|
@ -255,6 +266,7 @@ static dictEntry *dictFind(dict *ht, const void *key) {
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static dictIterator *dictGetIterator(dict *ht) {
|
|
|
|
static dictIterator *dictGetIterator(dict *ht) {
|
|
|
|
dictIterator *iter = malloc(sizeof(*iter));
|
|
|
|
dictIterator *iter = malloc(sizeof(*iter));
|
|
|
|
|
|
|
|
|
|
|
@ -265,6 +277,7 @@ static dictIterator *dictGetIterator(dict *ht) {
|
|
|
|
return iter;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static dictEntry *dictNext(dictIterator *iter) {
|
|
|
|
static dictEntry *dictNext(dictIterator *iter) {
|
|
|
|
while (1) {
|
|
|
|
while (1) {
|
|
|
|
if (iter->entry == NULL) {
|
|
|
|
if (iter->entry == NULL) {
|
|
|
@ -285,6 +298,7 @@ static dictEntry *dictNext(dictIterator *iter) {
|
|
|
|
return NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__UNUSED_FUNCTION__
|
|
|
|
static void dictReleaseIterator(dictIterator *iter) {
|
|
|
|
static void dictReleaseIterator(dictIterator *iter) {
|
|
|
|
free(iter);
|
|
|
|
free(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|