- added GCC warning -Wstrict-prototypes
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
45 #include "BLI_mempool.h"
46 #include "BLI_blenlib.h"
47
48 typedef unsigned int    (*GHashHashFP)          (void *key);
49 typedef int                             (*GHashCmpFP)           (void *a, void *b);
50 typedef void                    (*GHashKeyFreeFP)       (void *key);
51 typedef void                    (*GHashValFreeFP)       (void *val);
52
53 typedef struct Entry {
54         struct Entry *next;
55         
56         void *key, *val;
57 } Entry;
58
59 typedef struct GHash {
60         GHashHashFP     hashfp;
61         GHashCmpFP      cmpfp;
62         
63         Entry **buckets;
64         struct BLI_mempool *entrypool;
65         int nbuckets, nentries, cursize;
66 } GHash;
67
68 typedef struct GHashIterator {
69         GHash *gh;
70         int curBucket;
71         struct Entry *curEntry;
72 } GHashIterator;
73
74 GHash*  BLI_ghash_new           (GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info);
75 void    BLI_ghash_free          (GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
76
77 //BM_INLINE void        BLI_ghash_insert        (GHash *gh, void *key, void *val);
78 //BM_INLINE int         BLI_ghash_remove        (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
79 //BM_INLINE void*       BLI_ghash_lookup        (GHash *gh, void *key);
80 //BM_INLINE int         BLI_ghash_haskey        (GHash *gh, void *key);
81
82 int             BLI_ghash_size          (GHash *gh);
83
84 /* *** */
85
86         /**
87          * Create a new GHashIterator. The hash table must not be mutated
88          * while the iterator is in use, and the iterator will step exactly
89          * BLI_ghash_size(gh) times before becoming done.
90          * 
91          * @param gh The GHash to iterate over.
92          * @return Pointer to a new DynStr.
93          */
94 GHashIterator*  BLI_ghashIterator_new           (GHash *gh);
95         /**
96          * Init an already allocated GHashIterator. The hash table must not
97          * be mutated while the iterator is in use, and the iterator will
98          * step exactly BLI_ghash_size(gh) times before becoming done.
99          * 
100          * @param ghi The GHashIterator to initialize.
101          * @param gh The GHash to iterate over.
102          */
103 void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh);
104         /**
105          * Free a GHashIterator.
106          *
107          * @param ghi The iterator to free.
108          */
109 void                    BLI_ghashIterator_free          (GHashIterator *ghi);
110
111         /**
112          * Retrieve the key from an iterator.
113          *
114          * @param ghi The iterator.
115          * @return The key at the current index, or NULL if the 
116          * iterator is done.
117          */
118 void*                   BLI_ghashIterator_getKey        (GHashIterator *ghi);
119         /**
120          * Retrieve the value from an iterator.
121          *
122          * @param ghi The iterator.
123          * @return The value at the current index, or NULL if the 
124          * iterator is done.
125          */
126 void*                   BLI_ghashIterator_getValue      (GHashIterator *ghi);
127         /**
128          * Steps the iterator to the next index.
129          *
130          * @param ghi The iterator.
131          */
132 void                    BLI_ghashIterator_step          (GHashIterator *ghi);
133         /**
134          * Determine if an iterator is done (has reached the end of
135          * the hash table).
136          *
137          * @param ghi The iterator.
138          * @return True if done, False otherwise.
139          */
140 int                             BLI_ghashIterator_isDone        (GHashIterator *ghi);
141
142 /* *** */
143
144 unsigned int    BLI_ghashutil_ptrhash   (void *key);
145 int                             BLI_ghashutil_ptrcmp    (void *a, void *b);
146
147 unsigned int    BLI_ghashutil_strhash   (void *key);
148 int                             BLI_ghashutil_strcmp    (void *a, void *b);
149
150 unsigned int    BLI_ghashutil_inthash   (void *ptr);
151 int                             BLI_ghashutil_intcmp(void *a, void *b);
152
153 /*begin of macro-inlined functions*/
154 extern unsigned int hashsizes[];
155
156 #if 0
157 #define BLI_ghash_insert(gh, _k, _v){\
158         unsigned int _hash= (gh)->hashfp(_k)%gh->nbuckets;\
159         Entry *_e= BLI_mempool_alloc((gh)->entrypool);\
160         _e->key= _k;\
161         _e->val= _v;\
162         _e->next= (gh)->buckets[_hash];\
163         (gh)->buckets[_hash]= _e;\
164         if (++(gh)->nentries>(gh)->nbuckets*3) {\
165                 Entry *_e, **_old= (gh)->buckets;\
166                 int _i, _nold= (gh)->nbuckets;\
167                 (gh)->nbuckets= hashsizes[++(gh)->cursize];\
168                 (gh)->buckets= malloc((gh)->nbuckets*sizeof(*(gh)->buckets));\
169                 memset((gh)->buckets, 0, (gh)->nbuckets*sizeof(*(gh)->buckets));\
170                 for (_i=0; _i<_nold; _i++) {\
171                         for (_e= _old[_i]; _e;) {\
172                                 Entry *_n= _e->next;\
173                                 _hash= (gh)->hashfp(_e->key)%(gh)->nbuckets;\
174                                 _e->next= (gh)->buckets[_hash];\
175                                 (gh)->buckets[_hash]= _e;\
176                                 _e= _n;\
177                         }\
178                 }\
179                 free(_old); } }
180 #endif
181
182 /*---------inlined functions---------*/
183 BM_INLINE void BLI_ghash_insert(GHash *gh, void *key, void *val) {
184         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
185         Entry *e= (Entry*) BLI_mempool_alloc(gh->entrypool);
186
187         e->key= key;
188         e->val= val;
189         e->next= gh->buckets[hash];
190         gh->buckets[hash]= e;
191         
192         if (++gh->nentries>(float)gh->nbuckets/2) {
193                 Entry **old= gh->buckets;
194                 int i, nold= gh->nbuckets;
195                 
196                 gh->nbuckets= hashsizes[++gh->cursize];
197                 gh->buckets= (Entry**)MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
198                 memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
199                 
200                 for (i=0; i<nold; i++) {
201                         for (e= old[i]; e;) {
202                                 Entry *n= e->next;
203                                 
204                                 hash= gh->hashfp(e->key)%gh->nbuckets;
205                                 e->next= gh->buckets[hash];
206                                 gh->buckets[hash]= e;
207                                 
208                                 e= n;
209                         }
210                 }
211                 
212                 MEM_freeN(old);
213         }
214 }
215
216 BM_INLINE void* BLI_ghash_lookup(GHash *gh, void *key) 
217 {
218         if(gh) {
219                 unsigned int hash= gh->hashfp(key)%gh->nbuckets;
220                 Entry *e;
221                 
222                 for (e= gh->buckets[hash]; e; e= e->next)
223                         if (gh->cmpfp(key, e->key)==0)
224                                 return e->val;
225         }       
226         return NULL;
227 }
228
229 BM_INLINE int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
230 {
231         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
232         Entry *e;
233         Entry *p = 0;
234
235         for (e= gh->buckets[hash]; e; e= e->next) {
236                 if (gh->cmpfp(key, e->key)==0) {
237                         Entry *n= e->next;
238
239                         if (keyfreefp) keyfreefp(e->key);
240                         if (valfreefp) valfreefp(e->val);
241                         BLI_mempool_free(gh->entrypool, e);
242
243
244                         e= n;
245                         if (p)
246                                 p->next = n;
247                         else
248                                 gh->buckets[hash] = n;
249
250                         --gh->nentries;
251                         return 1;
252                 }
253                 p = e;
254         }
255  
256         return 0;
257 }
258
259 BM_INLINE int BLI_ghash_haskey(GHash *gh, void *key) {
260         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
261         Entry *e;
262         
263         for (e= gh->buckets[hash]; e; e= e->next)
264                 if (gh->cmpfp(key, e->key)==0)
265                         return 1;
266         
267         return 0;
268 }
269
270 #ifdef __cplusplus
271 }
272 #endif
273
274 #endif