X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fhash.h;h=8fe6f7fabbd4ab0c8861ba9f077c4a6f4c3d6064;hb=afba55b62615e9b162159c0f7b0df602f27f69a7;hp=ff76f01ac2f513701562beb41e1af08f4b8a23eb;hpb=30eb586d06bc4d1ee7388dced1c20e530292aa93;p=rocksndiamonds.git diff --git a/src/libgame/hash.h b/src/libgame/hash.h index ff76f01a..8fe6f7fa 100644 --- a/src/libgame/hash.h +++ b/src/libgame/hash.h @@ -4,7 +4,7 @@ // (c) 1995-2014 by Artsoft Entertainment // Holger Schemel // info@artsoft.org -// http://www.artsoft.org/ +// https://www.artsoft.org/ // ---------------------------------------------------------------------------- // hash.h // ============================================================================ @@ -45,7 +45,7 @@ * static unsigned int hash_from_key_fn( void *k ); * static int keys_equal_fn ( void *key1, void *key2 ); * - * h = create_hashtable(16, 0.75, hash_from_key_fn, keys_equal_fn); + * h = create_hashtable(16, 0.75, hash_from_key_fn, keys_equal_fn, free, free); * k = (struct some_key *) malloc(sizeof(struct some_key)); * v = (struct some_value *) malloc(sizeof(struct some_value)); * @@ -109,6 +109,8 @@ struct hashtable unsigned int loadlimit; unsigned int (*hashfn) (void *k); int (*eqfn) (void *k1, void *k2); + void (*freekfn) (void *k); + void (*freevfn) (void *v); }; /*****************************************************************************/ @@ -121,28 +123,39 @@ struct hashtable_itr /***************************************************************************** - * create_hashtable + * create_hashtable_ext * @name create_hashtable * @param minsize minimum initial size of hashtable * @param maxloadfactor maximum ratio entries / tablesize * @param hashfunction function for hashing keys * @param key_eq_fn function for determining key equality + * @param key_free_fn function for freeing keys + * @param value_free_fn function for freeing values * @return newly created hashtable or NULL on failure */ struct hashtable * -create_hashtable(unsigned int minsize, float maxloadfactor, - unsigned int (*hashfunction) (void*), - int (*key_eq_fn) (void*,void*)); +create_hashtable_ext(unsigned int minsize, float maxloadfactor, + unsigned int (*hashfunction) (void*), + int (*key_eq_fn) (void*, void*), + void (*key_free_fn) (void*), + void (*value_free_fn) (void*)); + +/* wrapper function using reasonable default values for some parameters */ +struct hashtable * +create_hashtable(unsigned int (*hashfunction) (void*), + int (*key_eq_fn) (void*, void*), + void (*key_free_fn) (void*), + void (*value_free_fn) (void*)); /***************************************************************************** * hashtable_insert * @name hashtable_insert * @param h the hashtable to insert into - * @param k the key - hashtable claims ownership and will free on removal - * @param v the value - does not claim ownership + * @param k the key - will be freed on removal if free function defined + * @param v the value - will be freed on removal if free function defined * @return non-zero for successful insertion * * This function will cause the table to expand if the insertion would take @@ -161,7 +174,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v); #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ static int fnname (struct hashtable *h, keytype *k, valuetype *v) \ { \ - return hashtable_insert(h,k,v); \ + return hashtable_insert(h, k, v); \ } /***************************************************************************** @@ -180,7 +193,25 @@ hashtable_change(struct hashtable *h, void *k, void *v); #define DEFINE_HASHTABLE_CHANGE(fnname, keytype, valuetype) \ static int fnname (struct hashtable *h, keytype *k, valuetype *v) \ { \ - return hashtable_change(h,k,v); \ + return hashtable_change(h, k, v); \ +} + +/***************************************************************************** + * hashtable_exists + + * @name hashtable_exists + * @param h the hashtable to search + * @param k the key to search for + * @return non-zero if key exists, else zero + */ + +int +hashtable_exists(struct hashtable *h, void *k); + +#define DEFINE_HASHTABLE_EXISTS(fnname, keytype, valuetype) \ +static int fnname (struct hashtable *h, keytype *k) \ +{ \ + return hashtable_exists(h, k); \ } /***************************************************************************** @@ -188,7 +219,7 @@ static int fnname (struct hashtable *h, keytype *k, valuetype *v) \ * @name hashtable_search * @param h the hashtable to search - * @param k the key to search for - does not claim ownership + * @param k the key to search for * @return the value associated with the key, or NULL if none found */ @@ -198,7 +229,7 @@ hashtable_search(struct hashtable *h, void *k); #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ static valuetype * fnname (struct hashtable *h, keytype *k) \ { \ - return (valuetype *) (hashtable_search(h,k)); \ + return (valuetype *) (hashtable_search(h, k)); \ } /***************************************************************************** @@ -206,7 +237,7 @@ static valuetype * fnname (struct hashtable *h, keytype *k) \ * @name hashtable_remove * @param h the hashtable to remove the item from - * @param k the key to search for - does not claim ownership + * @param k the key to search for * @return the value associated with the key, or NULL if none found */ @@ -216,7 +247,7 @@ hashtable_remove(struct hashtable *h, void *k); #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ static valuetype * fnname (struct hashtable *h, keytype *k) \ { \ - return (valuetype *) (hashtable_remove(h,k)); \ + return (valuetype *) (hashtable_remove(h, k)); \ } @@ -234,11 +265,10 @@ hashtable_count(struct hashtable *h); * hashtable_destroy * @name hashtable_destroy - * @param free_values whether to call 'free' on the remaining values */ void -hashtable_destroy(struct hashtable *h, int free_values); +hashtable_destroy(struct hashtable *h); /*****************************************************************************/ @@ -249,13 +279,13 @@ struct hashtable_itr * hashtable_iterator(struct hashtable *h); /*****************************************************************************/ -/* key - return the key of the (key,value) pair at the current position */ +/* key - return the key of the (key, value) pair at the current position */ void * hashtable_iterator_key(struct hashtable_itr *i); /*****************************************************************************/ -/* value - return the value of the (key,value) pair at the current position */ +/* value - return the value of the (key, value) pair at the current position */ void * hashtable_iterator_value(struct hashtable_itr *i);