compile fix
[blender.git] / source / blender / blenlib / BLI_ghash.h
1 /**
2  * A general (pointer -> pointer) hash table ADT
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31  
32 #ifndef BLI_GHASH_H
33 #define BLI_GHASH_H
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #include "stdio.h"
40 #include "stdlib.h"
41 #include "string.h"
42
43 #include "BKE_utildefines.h"
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_mempool.h"
47 #include "BLI_blenlib.h"
48
49 typedef unsigned int    (*GHashHashFP)          (void *key);
50 typedef int                             (*GHashCmpFP)           (void *a, void *b);
51 typedef void                    (*GHashKeyFreeFP)       (void *key);
52 typedef void                    (*GHashValFreeFP)       (void *val);
53
54 typedef struct Entry {
55         struct Entry *next;
56         
57         void *key, *val;
58 } Entry;
59
60 typedef struct GHash {
61         GHashHashFP     hashfp;
62         GHashCmpFP      cmpfp;
63         
64         Entry **buckets;
65         struct BLI_mempool *entrypool;
66         int nbuckets, nentries, cursize;
67 } GHash;
68
69 typedef struct GHashIterator {
70         GHash *gh;
71         int curBucket;
72         struct Entry *curEntry;
73 } GHashIterator;
74
75 GHash*  BLI_ghash_new           (GHashHashFP hashfp, GHashCmpFP cmpfp);
76 void    BLI_ghash_free          (GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
77
78 //BM_INLINE void        BLI_ghash_insert        (GHash *gh, void *key, void *val);
79 //BM_INLINE int         BLI_ghash_remove        (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
80 //BM_INLINE void*       BLI_ghash_lookup        (GHash *gh, void *key);
81 //BM_INLINE int         BLI_ghash_haskey        (GHash *gh, void *key);
82
83 int             BLI_ghash_size          (GHash *gh);
84
85 /* *** */
86
87         /**
88          * Create a new GHashIterator. The hash table must not be mutated
89          * while the iterator is in use, and the iterator will step exactly
90          * BLI_ghash_size(gh) times before becoming done.
91          * 
92          * @param gh The GHash to iterate over.
93          * @return Pointer to a new DynStr.
94          */
95 GHashIterator*  BLI_ghashIterator_new           (GHash *gh);
96         /**
97          * Init an already allocated GHashIterator. The hash table must not
98          * be mutated while the iterator is in use, and the iterator will
99          * step exactly BLI_ghash_size(gh) times before becoming done.
100          * 
101          * @param ghi The GHashIterator to initialize.
102          * @param gh The GHash to iterate over.
103          */
104 void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh);
105         /**
106          * Free a GHashIterator.
107          *
108          * @param ghi The iterator to free.
109          */
110 void                    BLI_ghashIterator_free          (GHashIterator *ghi);
111
112         /**
113          * Retrieve the key from an iterator.
114          *
115          * @param ghi The iterator.
116          * @return The key at the current index, or NULL if the 
117          * iterator is done.
118          */
119 void*                   BLI_ghashIterator_getKey        (GHashIterator *ghi);
120         /**
121          * Retrieve the value from an iterator.
122          *
123          * @param ghi The iterator.
124          * @return The value at the current index, or NULL if the 
125          * iterator is done.
126          */
127 void*                   BLI_ghashIterator_getValue      (GHashIterator *ghi);
128         /**
129          * Steps the iterator to the next index.
130          *
131          * @param ghi The iterator.
132          */
133 void                    BLI_ghashIterator_step          (GHashIterator *ghi);
134         /**
135          * Determine if an iterator is done (has reached the end of
136          * the hash table).
137          *
138          * @param ghi The iterator.
139          * @return True if done, False otherwise.
140          */
141 int                             BLI_ghashIterator_isDone        (GHashIterator *ghi);
142
143 /* *** */
144
145 unsigned int    BLI_ghashutil_ptrhash   (void *key);
146 int                             BLI_ghashutil_ptrcmp    (void *a, void *b);
147
148 unsigned int    BLI_ghashutil_strhash   (void *key);
149 int                             BLI_ghashutil_strcmp    (void *a, void *b);
150
151 unsigned int    BLI_ghashutil_inthash   (void *ptr);
152 int                             BLI_ghashutil_intcmp(void *a, void *b);
153
154 /*begin of macro-inlined functions*/
155 extern unsigned int hashsizes[];
156
157 #if 0
158 #define BLI_ghash_insert(gh, _k, _v){\
159         unsigned int _hash= (gh)->hashfp(_k)%gh->nbuckets;\
160         Entry *_e= BLI_mempool_alloc((gh)->entrypool);\
161         _e->key= _k;\
162         _e->val= _v;\
163         _e->next= (gh)->buckets[_hash];\
164         (gh)->buckets[_hash]= _e;\
165         if (++(gh)->nentries>(gh)->nbuckets*3) {\
166                 Entry *_e, **_old= (gh)->buckets;\
167                 int _i, _nold= (gh)->nbuckets;\
168                 (gh)->nbuckets= hashsizes[++(gh)->cursize];\
169                 (gh)->buckets= malloc((gh)->nbuckets*sizeof(*(gh)->buckets));\
170                 memset((gh)->buckets, 0, (gh)->nbuckets*sizeof(*(gh)->buckets));\
171                 for (_i=0; _i<_nold; _i++) {\
172                         for (_e= _old[_i]; _e;) {\
173                                 Entry *_n= _e->next;\
174                                 _hash= (gh)->hashfp(_e->key)%(gh)->nbuckets;\
175                                 _e->next= (gh)->buckets[_hash];\
176                                 (gh)->buckets[_hash]= _e;\
177                                 _e= _n;\
178                         }\
179                 }\
180                 free(_old); } }
181 #endif
182
183 /*---------inlined functions---------*/
184 BM_INLINE void BLI_ghash_insert(GHash *gh, void *key, void *val) {
185         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
186         Entry *e= (Entry*) BLI_mempool_alloc(gh->entrypool);
187
188         e->key= key;
189         e->val= val;
190         e->next= gh->buckets[hash];
191         gh->buckets[hash]= e;
192         
193         if (++gh->nentries>(float)gh->nbuckets/2) {
194                 Entry *e, **old= gh->buckets;
195                 int i, nold= gh->nbuckets;
196                 
197                 gh->nbuckets= hashsizes[++gh->cursize];
198                 gh->buckets= (Entry**)MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
199                 memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
200                 
201                 for (i=0; i<nold; i++) {
202                         for (e= old[i]; e;) {
203                                 Entry *n= e->next;
204                                 
205                                 hash= gh->hashfp(e->key)%gh->nbuckets;
206                                 e->next= gh->buckets[hash];
207                                 gh->buckets[hash]= e;
208                                 
209                                 e= n;
210                         }
211                 }
212                 
213                 MEM_freeN(old);
214         }
215 }
216
217 BM_INLINE void* BLI_ghash_lookup(GHash *gh, void *key) 
218 {
219         if(gh) {
220                 unsigned int hash= gh->hashfp(key)%gh->nbuckets;
221                 Entry *e;
222                 
223                 for (e= gh->buckets[hash]; e; e= e->next)
224                         if (gh->cmpfp(key, e->key)==0)
225                                 return e->val;
226         }       
227         return NULL;
228 }
229
230 BM_INLINE int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
231 {
232         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
233         Entry *e;
234         Entry *p = 0;
235
236         for (e= gh->buckets[hash]; e; e= e->next) {
237                 if (gh->cmpfp(key, e->key)==0) {
238                         Entry *n= e->next;
239
240                         if (keyfreefp) keyfreefp(e->key);
241                         if (valfreefp) valfreefp(e->val);
242                         BLI_mempool_free(gh->entrypool, e);
243
244
245                         e= n;
246                         if (p)
247                                 p->next = n;
248                         else
249                                 gh->buckets[hash] = n;
250
251                         --gh->nentries;
252                         return 1;
253                 }
254                 p = e;
255         }
256  
257         return 0;
258 }
259
260 BM_INLINE int BLI_ghash_haskey(GHash *gh, void *key) {
261         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
262         Entry *e;
263         
264         for (e= gh->buckets[hash]; e; e= e->next)
265                 if (gh->cmpfp(key, e->key)==0)
266                         return 1;
267         
268         return 0;
269 }
270
271 #ifdef __cplusplus
272 }
273 #endif
274
275 #endif