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