X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fhashtable.h;h=2a645a1714f30d8f4c8b9e26c82cc3118732e1b8;hb=09395973670d02710885cce266a8b4da943bb423;hp=4f8ca1a3af463f35d67bea68b1ed0d71f4d2648d;hpb=c792c5e657737a794893ae3505fba633b93e44b7;p=libucw.git diff --git a/ucw/hashtable.h b/ucw/hashtable.h index 4f8ca1a3..2a645a17 100644 --- a/ucw/hashtable.h +++ b/ucw/hashtable.h @@ -3,6 +3,8 @@ * * (c) 2002--2004 Martin Mares * (c) 2002--2005 Robert Spalek + * (c) 2010 Pavel Charvat + * (c) 2012 Tomas Valla * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -40,7 +42,7 @@ * With complex keys, HASH_GIVE_HASHFN and HASH_GIVE_EQ * are mandatory. * | HASH_KEY_MEMORY=f use node->f as a raw data key, compared using - * memcmp + * memcmp * HASH_KEY_SIZE the length of the key block * * Then specify what operations you request (all names are automatically @@ -57,13 +59,15 @@ * HASH_WANT_LOOKUP node *lookup(key) -- find node with given key, * if it doesn't exist, create it. Defining * HASH_GIVE_INIT_DATA is strongly recommended. + * Use HASH_LOOKUP_DETECT_NEW if you want to know + * whether the node was newly created or not. * HASH_WANT_DELETE int delete(key) -- delete and deallocate node * with given key. Returns success. * HASH_WANT_REMOVE remove(node *) -- delete and deallocate given node. * * You can also supply several functions: * - * HASH_GIVE_HASHFN unsigned int hash(key) -- calculate hash value of key. + * HASH_GIVE_HASHFN uint hash(key) -- calculate hash value of key. * We have sensible default hash functions for strings * and integers. * HASH_GIVE_EQ int eq(key1, key2) -- return whether keys are equal. @@ -77,10 +81,13 @@ * and static strings, strcpy for end-allocated strings. * HASH_GIVE_INIT_DATA void init_data(node *) -- initialize data fields in a * newly created node. Very useful for lookup operations. - * HASH_GIVE_ALLOC void *alloc(unsigned int size) -- allocate space for + * HASH_GIVE_ALLOC void *alloc(uint size) -- allocate space for * a node. Default is xmalloc() or pooled allocation, depending - * on HASH_USE_POOL and HASH_AUTO_POOL switches. - * void free(void *) -- the converse. + * on HASH_USE_POOL, HASH_AUTO_POOL, HASH_USE_ELTPOOL + * and HASH_AUTO_ELTPOOL switches. void free(void *) -- the converse. + * HASH_GIVE_TABLE_ALLOC void *table_alloc(uint size), void *table_free(void *) + * Allocate or free space for the table itself. Default is xmalloc() + * or the functions defined by HASH_GIVE_ALLOC if HASH_TABLE_ALLOC is set. * * ... and a couple of extra parameters: * @@ -93,12 +100,20 @@ * deallocation is not supported by mempools, so delete/remove * will leak pool memory. * HASH_AUTO_POOL=size Create a pool of the given block size automatically. + * HASH_USE_ELTPOOL=pool Allocate all nodes from given eltpool. + * HASH_AUTO_ELTPOOL=count Create an eltpool of the given number of elements in each chunk. * HASH_ZERO_FILL New entries should be initialized to all zeroes. * HASH_TABLE_ALLOC The hash table itself will be allocated and freed using * the same allocation functions as the nodes instead of * the default xmalloc(). + * HASH_TABLE_GROWING Never decrease the size of the hash table itself * HASH_TABLE_DYNAMIC Support multiple hash tables; the first parameter of all * hash table operations is struct HASH_PREFIX(table) *. + * HASH_TABLE_VARS Extra variables to be defined in table structure + * HASH_LOOKUP_DETECT_NEW + * the prototype for lookup is changed to node *lookup(key, int *new_item) + * new_item must not be NULL and returns 1 whether lookup + * just created a new item in the hashtable or 0 otherwise. * * You also get a iterator macro at no extra charge: * @@ -113,7 +128,7 @@ * * (For dynamic tables, use HASH_FOR_ALL_DYNAMIC(hash_prefix, hash_table, variable) instead.) * - * Then include "ucw/hashtable.h" and voila, you have a hash table + * Then include and voila, you have a hash table * suiting all your needs (at least those which you've revealed :) ). * * After including this file, all parameter macros are automatically @@ -121,10 +136,10 @@ */ #ifndef _UCW_HASHFUNC_H -#include "ucw/hashfunc.h" +#include #endif -#include "ucw/prime.h" +#include #include @@ -147,18 +162,24 @@ typedef HASH_NODE P(node); typedef struct P(bucket) { struct P(bucket) *next; #ifndef HASH_CONSERVE_SPACE - uns hash; + uint hash; #endif P(node) n; } P(bucket); struct P(table) { - uns hash_size; - uns hash_count, hash_max, hash_min, hash_hard_max; +#ifdef HASH_TABLE_VARS + HASH_TABLE_VARS +#endif + uint hash_size; + uint hash_count, hash_max, hash_min, hash_hard_max; P(bucket) **ht; #ifdef HASH_AUTO_POOL struct mempool *pool; #endif +#ifdef HASH_AUTO_ELTPOOL + struct eltpool *eltpool; +#endif }; #ifdef HASH_TABLE_DYNAMIC @@ -257,7 +278,7 @@ struct P(table) P(table); #ifndef HASH_GIVE_HASHFN #define HASH_GIVE_HASHFN - static inline uns P(hash) (TAUC char *k) + static inline uint P(hash) (TAUC char *k) { # ifdef HASH_NOCASE return hash_string_nocase(k); @@ -326,38 +347,68 @@ static inline void P(cleanup_alloc) (TAU) { } #elif defined(HASH_USE_POOL) /* If the caller has requested to use his mempool, do so */ -#include "ucw/mempool.h" -static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(HASH_USE_POOL, size); } +#include +static inline void * P(alloc) (TAUC uint size) { return mp_alloc_fast(HASH_USE_POOL, size); } static inline void P(free) (TAUC void *x UNUSED) { } static inline void P(init_alloc) (TAU) { } static inline void P(cleanup_alloc) (TAU) { } #elif defined(HASH_AUTO_POOL) /* Use our own pools */ -#include "ucw/mempool.h" -static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(T.pool, size); } +#include +static inline void * P(alloc) (TAUC uint size) { return mp_alloc_fast(T.pool, size); } static inline void P(free) (TAUC void *x UNUSED) { } static inline void P(init_alloc) (TAU) { T.pool = mp_new(HASH_AUTO_POOL); } static inline void P(cleanup_alloc) (TAU) { mp_delete(T.pool); } #define HASH_USE_POOL +#elif defined(HASH_USE_ELTPOOL) +/* If the caller has requested to use his eltpool, do so */ +#include +static inline void * P(alloc) (TAUC uint size UNUSED) { ASSERT(size <= (HASH_USE_ELTPOOL)->elt_size); return ep_alloc(HASH_USE_ELTPOOL); } +static inline void P(free) (TAUC void *x) { ep_free(HASH_USE_ELTPOOL, x); } +static inline void P(init_alloc) (TAU) { } +static inline void P(cleanup_alloc) (TAU) { } + +#elif defined(HASH_AUTO_ELTPOOL) +/* Use our own eltpools */ +#include +static inline void * P(alloc) (TAUC uint size UNUSED) { return ep_alloc(T.eltpool); } +static inline void P(free) (TAUC void *x) { ep_free(T.eltpool, x); } +static inline void P(init_alloc) (TAU) { T.eltpool = ep_new(sizeof(P(bucket)), HASH_AUTO_ELTPOOL); } +static inline void P(cleanup_alloc) (TAU) { ep_delete(T.eltpool); } +#define HASH_USE_ELTPOOL + #else /* The default allocation method */ -static inline void * P(alloc) (TAUC unsigned int size) { return xmalloc(size); } +static inline void * P(alloc) (TAUC uint size) { return xmalloc(size); } static inline void P(free) (TAUC void *x) { xfree(x); } static inline void P(init_alloc) (TAU) { } static inline void P(cleanup_alloc) (TAU) { } #endif -#ifdef HASH_TABLE_ALLOC -static inline void * P(table_alloc) (TAUC unsigned int size) { return P(alloc)(TTC size); } +#if defined(HASH_USE_ELTPOOL) && defined(HASH_GIVE_EXTRA_SIZE) +#error Eltpools not supported in combination with variable-sized nodes +#endif + +#ifdef HASH_GIVE_TABLE_ALLOC +/* If the caller has requested to use his own allocation functions, do so */ +#elif defined(HASH_TABLE_ALLOC) +#ifdef HASH_USE_ELTPOOL +#error HASH_TABLE_ALLOC not supported in combination with eltpools +#endif +static inline void * P(table_alloc) (TAUC uint size) { return P(alloc)(TTC size); } static inline void P(table_free) (TAUC void *x) { P(free)(TTC x); } #else -static inline void * P(table_alloc) (TAUC unsigned int size) { return xmalloc(size); } +static inline void * P(table_alloc) (TAUC uint size) { return xmalloc(size); } static inline void P(table_free) (TAUC void *x) { xfree(x); } #endif +#if defined(HASH_USE_POOL) && defined(HASH_TABLE_ALLOC) && !defined(HASH_TABLE_GROWING) +#define HASH_TABLE_GROWING +#endif + #ifndef HASH_DEFAULT_SIZE #define HASH_DEFAULT_SIZE 32 #endif @@ -367,14 +418,14 @@ static inline void P(table_free) (TAUC void *x) { xfree(x); } #endif #ifdef HASH_ZERO_FILL -static inline void * P(new_bucket)(TAUC uns size) +static inline void * P(new_bucket)(TAUC uint size) { byte *buck = P(alloc)(TTC size); bzero(buck, size); return buck; } #else -static inline void * P(new_bucket)(TAUC uns size) { return P(alloc)(TTC size); } +static inline void * P(new_bucket)(TAUC uint size) { return P(alloc)(TTC size); } #endif /* Now the operations */ @@ -388,9 +439,11 @@ static void P(alloc_table) (TAU) T.hash_max = 2*T.hash_size; else T.hash_max = ~0U; +#ifndef HASH_TABLE_GROWING if (T.hash_size/2 > HASH_DEFAULT_SIZE) T.hash_min = T.hash_size/4; else +#endif T.hash_min = 0; } @@ -419,7 +472,7 @@ static void HASH_PREFIX(init)(TA) static void HASH_PREFIX(cleanup)(TA) { #ifndef HASH_USE_POOL - uns i; + uint i; P(bucket) *b, *bb; for (i=0; in.)); @@ -443,12 +496,12 @@ static inline uns P(bucket_hash) (TAUC P(bucket) *b) #endif } -static void P(rehash) (TAC uns size) +static void P(rehash) (TAC uint size) { P(bucket) *b, *nb; P(bucket) **oldt = T.ht, **newt; - uns oldsize = T.hash_size; - uns i, h; + uint oldsize = T.hash_size; + uint i, h; DBG("Rehashing %d->%d at count %d", oldsize, size, T.hash_count); T.hash_size = size; @@ -478,8 +531,8 @@ static void P(rehash) (TAC uns size) **/ static HASH_NODE* HASH_PREFIX(find)(TAC HASH_KEY_DECL) { - uns h0 = P(hash) (TTC HASH_KEY( )); - uns h = h0 % T.hash_size; + uint h0 = P(hash) (TTC HASH_KEY( )); + uint h = h0 % T.hash_size; P(bucket) *b; for (b=T.ht[h]; b; b=b->next) @@ -504,7 +557,7 @@ static HASH_NODE* HASH_PREFIX(find)(TAC HASH_KEY_DECL) static HASH_NODE* HASH_PREFIX(find_next)(TAC P(node) *start) { #ifndef HASH_CONSERVE_SPACE - uns h0 = P(hash) (TTC HASH_KEY(start->)); + uint h0 = P(hash) (TTC HASH_KEY(start->)); #endif P(bucket) *b = SKIP_BACK(P(bucket), n, start); @@ -529,7 +582,7 @@ static HASH_NODE* HASH_PREFIX(find_next)(TAC P(node) *start) **/ static HASH_NODE * HASH_PREFIX(new)(TAC HASH_KEY_DECL) { - uns h0, h; + uint h0, h; P(bucket) *b; h0 = P(hash) (TTC HASH_KEY( )); @@ -549,16 +602,21 @@ static HASH_NODE * HASH_PREFIX(new)(TAC HASH_KEY_DECL) #endif #ifdef HASH_WANT_LOOKUP +#ifdef HASH_LOOKUP_DETECT_NEW /** * Finds a node with a given key. If it does not exist, a new one is created. * It is strongly recommended to use <>. * * This one is enabled by the <> macro. + * The @new_item argument is available only if <> was given. **/ +static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL, int *new_item) +#else static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL) +#endif { - uns h0 = P(hash) (TTC HASH_KEY( )); - uns h = h0 % T.hash_size; + uint h0 = P(hash) (TTC HASH_KEY( )); + uint h = h0 % T.hash_size; P(bucket) *b; for (b=T.ht[h]; b; b=b->next) @@ -567,8 +625,12 @@ static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL) #ifndef HASH_CONSERVE_SPACE b->hash == h0 && #endif - P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.))) + P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.))) { +#ifdef HASH_LOOKUP_DETECT_NEW + *new_item = 0; +#endif return &b->n; + } } b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( ))); @@ -581,6 +643,9 @@ static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL) P(init_data)(TTC &b->n); if (T.hash_count++ >= T.hash_max) P(rehash)(TTC 2*T.hash_size); +#ifdef HASH_LOOKUP_DETECT_NEW + *new_item = 1; +#endif return &b->n; } #endif @@ -595,8 +660,8 @@ static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL) **/ static int HASH_PREFIX(delete)(TAC HASH_KEY_DECL) { - uns h0 = P(hash) (TTC HASH_KEY( )); - uns h = h0 % T.hash_size; + uint h0 = P(hash) (TTC HASH_KEY( )); + uint h = h0 % T.hash_size; P(bucket) *b, **bb; for (bb=&T.ht[h]; b=*bb; bb=&b->next) @@ -609,8 +674,11 @@ static int HASH_PREFIX(delete)(TAC HASH_KEY_DECL) { *bb = b->next; P(free)(TTC b); - if (--T.hash_count < T.hash_min) + T.hash_count--; +#ifndef HASH_TABLE_GROWING + if (T.hash_count < T.hash_min) P(rehash)(TTC T.hash_size/2); +#endif return 1; } } @@ -629,8 +697,8 @@ static int HASH_PREFIX(delete)(TAC HASH_KEY_DECL) static void HASH_PREFIX(remove)(TAC HASH_NODE *n) { P(bucket) *x = SKIP_BACK(struct P(bucket), n, n); - uns h0 = P(bucket_hash)(TTC x); - uns h = h0 % T.hash_size; + uint h0 = P(bucket_hash)(TTC x); + uint h = h0 % T.hash_size; P(bucket) *b, **bb; for (bb=&T.ht[h]; (b=*bb) && b != x; bb=&b->next) @@ -638,8 +706,11 @@ static void HASH_PREFIX(remove)(TAC HASH_NODE *n) ASSERT(b); *bb = b->next; P(free)(TTC b); - if (--T.hash_count < T.hash_min) + T.hash_count--; +#ifndef HASH_TABLE_GROWING + if (T.hash_count < T.hash_min) P(rehash)(TTC T.hash_size/2); +#endif } #endif @@ -649,7 +720,7 @@ static void HASH_PREFIX(remove)(TAC HASH_NODE *n) #define HASH_FOR_ALL_DYNAMIC(h_px, h_table, h_var) \ do { \ - uns h_slot; \ + uint h_slot; \ struct GLUE_(h_px,bucket) *h_buck; \ for (h_slot=0; h_slot < (h_table)->hash_size; h_slot++) \ for (h_buck = (h_table)->ht[h_slot]; h_buck; h_buck = h_buck->next) \ @@ -679,6 +750,7 @@ do { \ #undef HASH_EXTRA_SIZE #undef HASH_FN_BITS #undef HASH_GIVE_ALLOC +#undef HASH_GIVE_TABLE_ALLOC #undef HASH_GIVE_EQ #undef HASH_GIVE_EXTRA_SIZE #undef HASH_GIVE_HASHFN @@ -697,6 +769,8 @@ do { \ #undef HASH_PREFIX #undef HASH_USE_POOL #undef HASH_AUTO_POOL +#undef HASH_USE_ELTPOOL +#undef HASH_AUTO_ELTPOOL #undef HASH_WANT_CLEANUP #undef HASH_WANT_DELETE #undef HASH_WANT_FIND @@ -705,5 +779,8 @@ do { \ #undef HASH_WANT_NEW #undef HASH_WANT_REMOVE #undef HASH_TABLE_ALLOC +#undef HASH_TABLE_GROWING #undef HASH_TABLE_DYNAMIC +#undef HASH_TABLE_VARS #undef HASH_ZERO_FILL +#undef HASH_LOOKUP_DETECT_NEW