Blender  V2.59
BLI_ghash.h
Go to the documentation of this file.
00001 /*
00002  * $Id: BLI_ghash.h 34966 2011-02-18 13:58:08Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029  
00030 #ifndef BLI_GHASH_H
00031 #define BLI_GHASH_H
00032 
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041 
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 
00046 #include "BLI_mempool.h"
00047 #include "BLI_blenlib.h"
00048 
00049 typedef unsigned int    (*GHashHashFP)          (const void *key);
00050 typedef int                             (*GHashCmpFP)           (const void *a, const void *b);
00051 typedef void                    (*GHashKeyFreeFP)       (void *key);
00052 typedef void                    (*GHashValFreeFP)       (void *val);
00053 
00054 typedef struct Entry {
00055         struct Entry *next;
00056         
00057         void *key, *val;
00058 } Entry;
00059 
00060 typedef struct GHash {
00061         GHashHashFP     hashfp;
00062         GHashCmpFP      cmpfp;
00063         
00064         Entry **buckets;
00065         struct BLI_mempool *entrypool;
00066         int nbuckets, nentries, cursize;
00067 } GHash;
00068 
00069 typedef struct GHashIterator {
00070         GHash *gh;
00071         int curBucket;
00072         struct Entry *curEntry;
00073 } GHashIterator;
00074 
00075 GHash*  BLI_ghash_new           (GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info);
00076 void    BLI_ghash_free          (GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
00077 
00078 //BM_INLINE void        BLI_ghash_insert        (GHash *gh, void *key, void *val);
00079 //BM_INLINE int         BLI_ghash_remove        (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
00080 //BM_INLINE void*       BLI_ghash_lookup        (GHash *gh, void *key);
00081 //BM_INLINE int         BLI_ghash_haskey        (GHash *gh, void *key);
00082 
00083 int             BLI_ghash_size          (GHash *gh);
00084 
00085 /* *** */
00086 
00095 GHashIterator*  BLI_ghashIterator_new           (GHash *gh);
00104 void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh);
00110 void                    BLI_ghashIterator_free          (GHashIterator *ghi);
00111 
00119 void*                   BLI_ghashIterator_getKey        (GHashIterator *ghi);
00127 void*                   BLI_ghashIterator_getValue      (GHashIterator *ghi);
00133 void                    BLI_ghashIterator_step          (GHashIterator *ghi);
00141 int                             BLI_ghashIterator_isDone        (GHashIterator *ghi);
00142 
00143 /* *** */
00144 
00145 unsigned int    BLI_ghashutil_ptrhash   (const void *key);
00146 int                             BLI_ghashutil_ptrcmp    (const void *a, const void *b);
00147 
00148 unsigned int    BLI_ghashutil_strhash   (const void *key);
00149 int                             BLI_ghashutil_strcmp    (const void *a, const void *b);
00150 
00151 unsigned int    BLI_ghashutil_inthash   (const void *ptr);
00152 int                             BLI_ghashutil_intcmp(const void *a, const void *b);
00153 
00154 /*begin of macro-inlined functions*/
00155 extern unsigned int hashsizes[];
00156 
00157 #if 0
00158 #define BLI_ghash_insert(gh, _k, _v){\
00159         unsigned int _hash= (gh)->hashfp(_k)%gh->nbuckets;\
00160         Entry *_e= BLI_mempool_alloc((gh)->entrypool);\
00161         _e->key= _k;\
00162         _e->val= _v;\
00163         _e->next= (gh)->buckets[_hash];\
00164         (gh)->buckets[_hash]= _e;\
00165         if (++(gh)->nentries>(gh)->nbuckets*3) {\
00166                 Entry *_e, **_old= (gh)->buckets;\
00167                 int _i, _nold= (gh)->nbuckets;\
00168                 (gh)->nbuckets= hashsizes[++(gh)->cursize];\
00169                 (gh)->buckets= malloc((gh)->nbuckets*sizeof(*(gh)->buckets));\
00170                 memset((gh)->buckets, 0, (gh)->nbuckets*sizeof(*(gh)->buckets));\
00171                 for (_i=0; _i<_nold; _i++) {\
00172                         for (_e= _old[_i]; _e;) {\
00173                                 Entry *_n= _e->next;\
00174                                 _hash= (gh)->hashfp(_e->key)%(gh)->nbuckets;\
00175                                 _e->next= (gh)->buckets[_hash];\
00176                                 (gh)->buckets[_hash]= _e;\
00177                                 _e= _n;\
00178                         }\
00179                 }\
00180                 free(_old); } }
00181 #endif
00182 
00183 /*---------inlined functions---------*/
00184 BM_INLINE void BLI_ghash_insert(GHash *gh, void *key, void *val) {
00185         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
00186         Entry *e= (Entry*) BLI_mempool_alloc(gh->entrypool);
00187 
00188         e->key= key;
00189         e->val= val;
00190         e->next= gh->buckets[hash];
00191         gh->buckets[hash]= e;
00192         
00193         if (++gh->nentries>(float)gh->nbuckets/2) {
00194                 Entry **old= gh->buckets;
00195                 int i, nold= gh->nbuckets;
00196                 
00197                 gh->nbuckets= hashsizes[++gh->cursize];
00198                 gh->buckets= (Entry**)MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
00199                 memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
00200                 
00201                 for (i=0; i<nold; i++) {
00202                         for (e= old[i]; e;) {
00203                                 Entry *n= e->next;
00204                                 
00205                                 hash= gh->hashfp(e->key)%gh->nbuckets;
00206                                 e->next= gh->buckets[hash];
00207                                 gh->buckets[hash]= e;
00208                                 
00209                                 e= n;
00210                         }
00211                 }
00212                 
00213                 MEM_freeN(old);
00214         }
00215 }
00216 
00217 BM_INLINE void* BLI_ghash_lookup(GHash *gh, const void *key) 
00218 {
00219         if(gh) {
00220                 unsigned int hash= gh->hashfp(key)%gh->nbuckets;
00221                 Entry *e;
00222                 
00223                 for (e= gh->buckets[hash]; e; e= e->next)
00224                         if (gh->cmpfp(key, e->key)==0)
00225                                 return e->val;
00226         }       
00227         return NULL;
00228 }
00229 
00230 BM_INLINE int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
00231 {
00232         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
00233         Entry *e;
00234         Entry *p = NULL;
00235 
00236         for (e= gh->buckets[hash]; e; e= e->next) {
00237                 if (gh->cmpfp(key, e->key)==0) {
00238                         Entry *n= e->next;
00239 
00240                         if (keyfreefp) keyfreefp(e->key);
00241                         if (valfreefp) valfreefp(e->val);
00242                         BLI_mempool_free(gh->entrypool, e);
00243 
00244 
00245                         e= n;
00246                         if (p)
00247                                 p->next = n;
00248                         else
00249                                 gh->buckets[hash] = n;
00250 
00251                         --gh->nentries;
00252                         return 1;
00253                 }
00254                 p = e;
00255         }
00256  
00257         return 0;
00258 }
00259 
00260 BM_INLINE int BLI_ghash_haskey(GHash *gh, void *key) {
00261         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
00262         Entry *e;
00263         
00264         for (e= gh->buckets[hash]; e; e= e->next)
00265                 if (gh->cmpfp(key, e->key)==0)
00266                         return 1;
00267         
00268         return 0;
00269 }
00270 
00271 #ifdef __cplusplus
00272 }
00273 #endif
00274 
00275 #endif