|
|
@ -22,7 +22,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#ifndef UTHASH_H |
|
|
|
#ifndef UTHASH_H |
|
|
|
#define UTHASH_H |
|
|
|
#define UTHASH_H |
|
|
|
|
|
|
|
|
|
|
|
#include <string.h> /* memcmp,strlen */ |
|
|
|
#include <string.h> /* memcmp,strlen */ |
|
|
|
#include <stddef.h> /* ptrdiff_t */ |
|
|
|
#include <stddef.h> /* ptrdiff_t */ |
|
|
@ -49,7 +49,7 @@ do { |
|
|
|
char **_da_dst = (char**)(&(dst)); \ |
|
|
|
char **_da_dst = (char**)(&(dst)); \ |
|
|
|
*_da_dst = (char*)(src); \ |
|
|
|
*_da_dst = (char*)(src); \ |
|
|
|
} while(0) |
|
|
|
} while(0) |
|
|
|
#else |
|
|
|
#else |
|
|
|
#define DECLTYPE_ASSIGN(dst,src) \ |
|
|
|
#define DECLTYPE_ASSIGN(dst,src) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
|
(dst) = DECLTYPE(dst)(src); \ |
|
|
|
(dst) = DECLTYPE(dst)(src); \ |
|
|
@ -121,9 +121,9 @@ do { |
|
|
|
HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) |
|
|
|
HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) |
|
|
|
|
|
|
|
|
|
|
|
#else |
|
|
|
#else |
|
|
|
#define HASH_BLOOM_MAKE(tbl) |
|
|
|
#define HASH_BLOOM_MAKE(tbl) |
|
|
|
#define HASH_BLOOM_FREE(tbl) |
|
|
|
#define HASH_BLOOM_FREE(tbl) |
|
|
|
#define HASH_BLOOM_ADD(tbl,hashv) |
|
|
|
#define HASH_BLOOM_ADD(tbl,hashv) |
|
|
|
#define HASH_BLOOM_TEST(tbl,hashv) (1) |
|
|
|
#define HASH_BLOOM_TEST(tbl,hashv) (1) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
@ -148,7 +148,7 @@ do { |
|
|
|
|
|
|
|
|
|
|
|
#define HASH_ADD(hh,head,fieldname,keylen_in,add) \ |
|
|
|
#define HASH_ADD(hh,head,fieldname,keylen_in,add) \ |
|
|
|
HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add) |
|
|
|
HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add) |
|
|
|
|
|
|
|
|
|
|
|
#define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ |
|
|
|
#define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
|
unsigned _ha_bkt; \ |
|
|
|
unsigned _ha_bkt; \ |
|
|
@ -300,10 +300,10 @@ do { |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
} while (0) |
|
|
|
} while (0) |
|
|
|
#else |
|
|
|
#else |
|
|
|
#define HASH_FSCK(hh,head) |
|
|
|
#define HASH_FSCK(hh,head) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
|
|
|
|
/* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
|
|
|
|
* the descriptor to which this macro is defined for tuning the hash function. |
|
|
|
* the descriptor to which this macro is defined for tuning the hash function. |
|
|
|
* The app can #include <unistd.h> to get the prototype for write(2). */ |
|
|
|
* The app can #include <unistd.h> to get the prototype for write(2). */ |
|
|
|
#ifdef HASH_EMIT_KEYS |
|
|
|
#ifdef HASH_EMIT_KEYS |
|
|
@ -313,12 +313,12 @@ do { |
|
|
|
write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ |
|
|
|
write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ |
|
|
|
write(HASH_EMIT_KEYS, keyptr, fieldlen); \ |
|
|
|
write(HASH_EMIT_KEYS, keyptr, fieldlen); \ |
|
|
|
} while (0) |
|
|
|
} while (0) |
|
|
|
#else |
|
|
|
#else |
|
|
|
#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) |
|
|
|
#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ |
|
|
|
/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ |
|
|
|
#ifdef HASH_FUNCTION |
|
|
|
#ifdef HASH_FUNCTION |
|
|
|
#define HASH_FCN HASH_FUNCTION |
|
|
|
#define HASH_FCN HASH_FUNCTION |
|
|
|
#else |
|
|
|
#else |
|
|
|
#define HASH_FCN HASH_JEN |
|
|
|
#define HASH_FCN HASH_JEN |
|
|
@ -335,7 +335,7 @@ do { |
|
|
|
} while (0) |
|
|
|
} while (0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at
|
|
|
|
/* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at
|
|
|
|
* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */
|
|
|
|
* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */
|
|
|
|
#define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \ |
|
|
|
#define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
@ -356,7 +356,7 @@ do { |
|
|
|
hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \ |
|
|
|
hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \ |
|
|
|
bkt = hashv & (num_bkts-1); \ |
|
|
|
bkt = hashv & (num_bkts-1); \ |
|
|
|
} while(0); |
|
|
|
} while(0); |
|
|
|
|
|
|
|
|
|
|
|
#define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \ |
|
|
|
#define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
|
unsigned _ho_i; \ |
|
|
|
unsigned _ho_i; \ |
|
|
@ -485,14 +485,14 @@ do { |
|
|
|
#ifdef HASH_USING_NO_STRICT_ALIASING |
|
|
|
#ifdef HASH_USING_NO_STRICT_ALIASING |
|
|
|
/* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads.
|
|
|
|
/* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads.
|
|
|
|
* For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. |
|
|
|
* For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. |
|
|
|
* MurmurHash uses the faster approach only on CPU's where we know it's safe. |
|
|
|
* MurmurHash uses the faster approach only on CPU's where we know it's safe. |
|
|
|
* |
|
|
|
* |
|
|
|
* Note the preprocessor built-in defines can be emitted using: |
|
|
|
* Note the preprocessor built-in defines can be emitted using: |
|
|
|
* |
|
|
|
* |
|
|
|
* gcc -m64 -dM -E - < /dev/null (on gcc) |
|
|
|
* gcc -m64 -dM -E - < /dev/null (on gcc) |
|
|
|
* cc -## a.c (where a.c is a simple test file) (Sun Studio) |
|
|
|
* cc -## a.c (where a.c is a simple test file) (Sun Studio) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
#if (defined(__i386__) || defined(__x86_64__)) |
|
|
|
#if (defined(__i386__) || defined(__x86_64__)) |
|
|
|
#define MUR_GETBLOCK(p,i) p[i] |
|
|
|
#define MUR_GETBLOCK(p,i) p[i] |
|
|
|
#else /* non intel */ |
|
|
|
#else /* non intel */ |
|
|
|
#define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0) |
|
|
|
#define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0) |
|
|
@ -562,7 +562,7 @@ do { \ |
|
|
|
#endif /* HASH_USING_NO_STRICT_ALIASING */ |
|
|
|
#endif /* HASH_USING_NO_STRICT_ALIASING */ |
|
|
|
|
|
|
|
|
|
|
|
/* key comparison function; return 0 if keys equal */ |
|
|
|
/* key comparison function; return 0 if keys equal */ |
|
|
|
#define HASH_KEYCMP(a,b,len) memcmp(a,b,len) |
|
|
|
#define HASH_KEYCMP(a,b,len) memcmp(a,b,len) |
|
|
|
|
|
|
|
|
|
|
|
/* iterate over items in a known bucket to find desired item */ |
|
|
|
/* iterate over items in a known bucket to find desired item */ |
|
|
|
#define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \ |
|
|
|
#define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \ |
|
|
@ -603,36 +603,36 @@ do { |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
if (hh_del->hh_next) { \ |
|
|
|
if (hh_del->hh_next) { \ |
|
|
|
hh_del->hh_next->hh_prev = hh_del->hh_prev; \ |
|
|
|
hh_del->hh_next->hh_prev = hh_del->hh_prev; \ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Bucket expansion has the effect of doubling the number of buckets
|
|
|
|
/* Bucket expansion has the effect of doubling the number of buckets
|
|
|
|
* and redistributing the items into the new buckets. Ideally the |
|
|
|
* and redistributing the items into the new buckets. Ideally the |
|
|
|
* items will distribute more or less evenly into the new buckets |
|
|
|
* items will distribute more or less evenly into the new buckets |
|
|
|
* (the extent to which this is true is a measure of the quality of |
|
|
|
* (the extent to which this is true is a measure of the quality of |
|
|
|
* the hash function as it applies to the key domain). |
|
|
|
* the hash function as it applies to the key domain). |
|
|
|
* |
|
|
|
* |
|
|
|
* With the items distributed into more buckets, the chain length |
|
|
|
* With the items distributed into more buckets, the chain length |
|
|
|
* (item count) in each bucket is reduced. Thus by expanding buckets |
|
|
|
* (item count) in each bucket is reduced. Thus by expanding buckets |
|
|
|
* the hash keeps a bound on the chain length. This bounded chain |
|
|
|
* the hash keeps a bound on the chain length. This bounded chain |
|
|
|
* length is the essence of how a hash provides constant time lookup. |
|
|
|
* length is the essence of how a hash provides constant time lookup. |
|
|
|
* |
|
|
|
* |
|
|
|
* The calculation of tbl->ideal_chain_maxlen below deserves some |
|
|
|
* The calculation of tbl->ideal_chain_maxlen below deserves some |
|
|
|
* explanation. First, keep in mind that we're calculating the ideal |
|
|
|
* explanation. First, keep in mind that we're calculating the ideal |
|
|
|
* maximum chain length based on the *new* (doubled) bucket count. |
|
|
|
* maximum chain length based on the *new* (doubled) bucket count. |
|
|
|
* In fractions this is just n/b (n=number of items,b=new num buckets). |
|
|
|
* In fractions this is just n/b (n=number of items,b=new num buckets). |
|
|
|
* Since the ideal chain length is an integer, we want to calculate |
|
|
|
* Since the ideal chain length is an integer, we want to calculate |
|
|
|
* ceil(n/b). We don't depend on floating point arithmetic in this |
|
|
|
* ceil(n/b). We don't depend on floating point arithmetic in this |
|
|
|
* hash, so to calculate ceil(n/b) with integers we could write |
|
|
|
* hash, so to calculate ceil(n/b) with integers we could write |
|
|
|
* |
|
|
|
* |
|
|
|
* ceil(n/b) = (n/b) + ((n%b)?1:0) |
|
|
|
* ceil(n/b) = (n/b) + ((n%b)?1:0) |
|
|
|
* |
|
|
|
* |
|
|
|
* and in fact a previous version of this hash did just that. |
|
|
|
* and in fact a previous version of this hash did just that. |
|
|
|
* But now we have improved things a bit by recognizing that b is |
|
|
|
* But now we have improved things a bit by recognizing that b is |
|
|
|
* always a power of two. We keep its base 2 log handy (call it lb), |
|
|
|
* always a power of two. We keep its base 2 log handy (call it lb), |
|
|
|
* so now we can write this with a bit shift and logical AND: |
|
|
|
* so now we can write this with a bit shift and logical AND: |
|
|
|
* |
|
|
|
* |
|
|
|
* ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) |
|
|
|
* ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
#define HASH_EXPAND_BUCKETS(tbl) \ |
|
|
|
#define HASH_EXPAND_BUCKETS(tbl) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
@ -684,7 +684,7 @@ do { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ |
|
|
|
/* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ |
|
|
|
/* Note that HASH_SORT assumes the hash handle name to be hh.
|
|
|
|
/* Note that HASH_SORT assumes the hash handle name to be hh.
|
|
|
|
* HASH_SRT was added to allow the hash handle name to be passed in. */ |
|
|
|
* HASH_SRT was added to allow the hash handle name to be passed in. */ |
|
|
|
#define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) |
|
|
|
#define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) |
|
|
|
#define HASH_SRT(hh,head,cmpfcn) \ |
|
|
|
#define HASH_SRT(hh,head,cmpfcn) \ |
|
|
@ -766,10 +766,10 @@ do { |
|
|
|
} \ |
|
|
|
} \ |
|
|
|
} while (0) |
|
|
|
} while (0) |
|
|
|
|
|
|
|
|
|
|
|
/* This function selects items from one hash into another hash.
|
|
|
|
/* This function selects items from one hash into another hash.
|
|
|
|
* The end result is that the selected items have dual presence |
|
|
|
* The end result is that the selected items have dual presence |
|
|
|
* in both hashes. There is no copy of the items made; rather |
|
|
|
* in both hashes. There is no copy of the items made; rather |
|
|
|
* they are added into the new hash through a secondary hash |
|
|
|
* they are added into the new hash through a secondary hash |
|
|
|
* hash handle that must be present in the structure. */ |
|
|
|
* hash handle that must be present in the structure. */ |
|
|
|
#define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ |
|
|
|
#define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ |
|
|
|
do { \ |
|
|
|
do { \ |
|
|
@ -822,7 +822,7 @@ do { |
|
|
|
#ifdef NO_DECLTYPE |
|
|
|
#ifdef NO_DECLTYPE |
|
|
|
#define HASH_ITER(hh,head,el,tmp) \ |
|
|
|
#define HASH_ITER(hh,head,el,tmp) \ |
|
|
|
for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \ |
|
|
|
for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \ |
|
|
|
el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL)) |
|
|
|
el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL)) |
|
|
|
#else |
|
|
|
#else |
|
|
|
#define HASH_ITER(hh,head,el,tmp) \ |
|
|
|
#define HASH_ITER(hh,head,el,tmp) \ |
|
|
|
for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \ |
|
|
|
for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \ |
|
|
@ -830,7 +830,7 @@ for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
/* obtain a count of items in the hash */ |
|
|
|
/* obtain a count of items in the hash */ |
|
|
|
#define HASH_COUNT(head) HASH_CNT(hh,head) |
|
|
|
#define HASH_COUNT(head) HASH_CNT(hh,head) |
|
|
|
#define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0) |
|
|
|
#define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0) |
|
|
|
|
|
|
|
|
|
|
|
typedef struct UT_hash_bucket { |
|
|
|
typedef struct UT_hash_bucket { |
|
|
@ -839,7 +839,7 @@ typedef struct UT_hash_bucket { |
|
|
|
|
|
|
|
|
|
|
|
/* expand_mult is normally set to 0. In this situation, the max chain length
|
|
|
|
/* expand_mult is normally set to 0. In this situation, the max chain length
|
|
|
|
* threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If |
|
|
|
* threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If |
|
|
|
* the bucket's chain exceeds this length, bucket expansion is triggered). |
|
|
|
* the bucket's chain exceeds this length, bucket expansion is triggered). |
|
|
|
* However, setting expand_mult to a non-zero value delays bucket expansion |
|
|
|
* However, setting expand_mult to a non-zero value delays bucket expansion |
|
|
|
* (that would be triggered by additions to this particular bucket) |
|
|
|
* (that would be triggered by additions to this particular bucket) |
|
|
|
* until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. |
|
|
|
* until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. |
|
|
@ -847,7 +847,7 @@ typedef struct UT_hash_bucket { |
|
|
|
* multiplier is to reduce bucket expansions, since they are expensive, in |
|
|
|
* multiplier is to reduce bucket expansions, since they are expensive, in |
|
|
|
* situations where we know that a particular bucket tends to be overused. |
|
|
|
* situations where we know that a particular bucket tends to be overused. |
|
|
|
* It is better to let its chain length grow to a longer yet-still-bounded |
|
|
|
* It is better to let its chain length grow to a longer yet-still-bounded |
|
|
|
* value, than to do an O(n) bucket expansion too often. |
|
|
|
* value, than to do an O(n) bucket expansion too often. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
unsigned expand_mult; |
|
|
|
unsigned expand_mult; |
|
|
|
|
|
|
|
|
|
|
@ -873,7 +873,7 @@ typedef struct UT_hash_table { |
|
|
|
* hash distribution; reaching them in a chain traversal takes >ideal steps */ |
|
|
|
* hash distribution; reaching them in a chain traversal takes >ideal steps */ |
|
|
|
unsigned nonideal_items; |
|
|
|
unsigned nonideal_items; |
|
|
|
|
|
|
|
|
|
|
|
/* ineffective expands occur when a bucket doubling was performed, but
|
|
|
|
/* ineffective expands occur when a bucket doubling was performed, but
|
|
|
|
* afterward, more than half the items in the hash had nonideal chain |
|
|
|
* afterward, more than half the items in the hash had nonideal chain |
|
|
|
* positions. If this happens on two consecutive expansions we inhibit any |
|
|
|
* positions. If this happens on two consecutive expansions we inhibit any |
|
|
|
* further expansion, as it's not helping; this happens when the hash |
|
|
|
* further expansion, as it's not helping; this happens when the hash |
|
|
|