svn merge -r39384:39433 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenlib / intern / BLI_ghash.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * A general (pointer -> pointer) hash table ADT
29  */
30
31 /** \file blender/blenlib/intern/BLI_ghash.c
32  *  \ingroup bli
33  */
34
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_utildefines.h"
39 #include "BLI_ghash.h"
40 #include "BLO_sys_types.h" // for intptr_t support
41 /***/
42
43 unsigned int hashsizes[]= {
44         5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, 
45         16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169, 
46         4194319, 8388617, 16777259, 33554467, 67108879, 134217757, 
47         268435459
48 };
49
50 /***/
51
52 GHash *BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) {
53         GHash *gh= MEM_mallocN(sizeof(*gh), info);
54         gh->hashfp= hashfp;
55         gh->cmpfp= cmpfp;
56         gh->entrypool = BLI_mempool_create(sizeof(Entry), 64, 64, 1, 0);
57
58         gh->cursize= 0;
59         gh->nentries= 0;
60         gh->nbuckets= hashsizes[gh->cursize];
61         
62         gh->buckets= MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
63         memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
64         
65         return gh;
66 }
67
68 int BLI_ghash_size(GHash *gh) {
69         return gh->nentries;
70 }
71
72 void BLI_ghash_insert(GHash *gh, void *key, void *val) {
73         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
74         Entry *e= (Entry*) BLI_mempool_alloc(gh->entrypool);
75
76         e->key= key;
77         e->val= val;
78         e->next= gh->buckets[hash];
79         gh->buckets[hash]= e;
80
81         if (++gh->nentries>(float)gh->nbuckets/2) {
82                 Entry **old= gh->buckets;
83                 int i, nold= gh->nbuckets;
84
85                 gh->nbuckets= hashsizes[++gh->cursize];
86                 gh->buckets= (Entry**)MEM_mallocN(gh->nbuckets*sizeof(*gh->buckets), "buckets");
87                 memset(gh->buckets, 0, gh->nbuckets*sizeof(*gh->buckets));
88
89                 for (i=0; i<nold; i++) {
90                         for (e= old[i]; e;) {
91                                 Entry *n= e->next;
92
93                                 hash= gh->hashfp(e->key)%gh->nbuckets;
94                                 e->next= gh->buckets[hash];
95                                 gh->buckets[hash]= e;
96
97                                 e= n;
98                         }
99                 }
100
101                 MEM_freeN(old);
102         }
103 }
104
105 void *BLI_ghash_lookup(GHash *gh, const void *key) {
106         if(gh) {
107                 unsigned int hash= gh->hashfp(key)%gh->nbuckets;
108                 Entry *e;
109
110                 for (e= gh->buckets[hash]; e; e= e->next)
111                         if (gh->cmpfp(key, e->key)==0)
112                                 return e->val;
113         }
114         return NULL;
115 }
116
117 int BLI_ghash_remove (GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
118 {
119         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
120         Entry *e;
121         Entry *p = NULL;
122
123         for (e= gh->buckets[hash]; e; e= e->next) {
124                 if (gh->cmpfp(key, e->key)==0) {
125                         Entry *n= e->next;
126
127                         if (keyfreefp) keyfreefp(e->key);
128                         if (valfreefp) valfreefp(e->val);
129                         BLI_mempool_free(gh->entrypool, e);
130
131                         /* correct but 'e' isnt used before return */
132                         /* e= n; */ /*UNUSED*/
133                         if (p)
134                                 p->next = n;
135                         else
136                                 gh->buckets[hash] = n;
137
138                         --gh->nentries;
139                         return 1;
140                 }
141                 p = e;
142         }
143
144         return 0;
145 }
146
147 int BLI_ghash_haskey(GHash *gh, void *key) {
148         unsigned int hash= gh->hashfp(key)%gh->nbuckets;
149         Entry *e;
150
151         for (e= gh->buckets[hash]; e; e= e->next)
152                 if (gh->cmpfp(key, e->key)==0)
153                         return 1;
154
155         return 0;
156 }
157
158 void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) {
159         int i;
160         
161         if (keyfreefp || valfreefp) {
162                 for (i=0; i<gh->nbuckets; i++) {
163                         Entry *e;
164                         
165                         for (e= gh->buckets[i]; e; ) {
166                                 Entry *n= e->next;
167                                 
168                                 if (keyfreefp) keyfreefp(e->key);
169                                 if (valfreefp) valfreefp(e->val);
170
171                                 e= n;
172                         }
173                 }
174         }
175         
176         MEM_freeN(gh->buckets);
177         BLI_mempool_destroy(gh->entrypool);
178         gh->buckets = NULL;
179         gh->nentries = 0;
180         gh->nbuckets = 0;
181         MEM_freeN(gh);
182 }
183
184 /***/
185
186 GHashIterator *BLI_ghashIterator_new(GHash *gh) {
187         GHashIterator *ghi= MEM_mallocN(sizeof(*ghi), "ghash iterator");
188         ghi->gh= gh;
189         ghi->curEntry= NULL;
190         ghi->curBucket= -1;
191         while (!ghi->curEntry) {
192                 ghi->curBucket++;
193                 if (ghi->curBucket==ghi->gh->nbuckets)
194                         break;
195                 ghi->curEntry= ghi->gh->buckets[ghi->curBucket];
196         }
197         return ghi;
198 }
199 void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh) {
200         ghi->gh= gh;
201         ghi->curEntry= NULL;
202         ghi->curBucket= -1;
203         while (!ghi->curEntry) {
204                 ghi->curBucket++;
205                 if (ghi->curBucket==ghi->gh->nbuckets)
206                         break;
207                 ghi->curEntry= ghi->gh->buckets[ghi->curBucket];
208         }
209 }
210 void BLI_ghashIterator_free(GHashIterator *ghi) {
211         MEM_freeN(ghi);
212 }
213
214 void *BLI_ghashIterator_getKey(GHashIterator *ghi) {
215         return ghi->curEntry?ghi->curEntry->key:NULL;
216 }
217 void *BLI_ghashIterator_getValue(GHashIterator *ghi) {
218         return ghi->curEntry?ghi->curEntry->val:NULL;
219 }
220
221 void BLI_ghashIterator_step(GHashIterator *ghi) {
222         if (ghi->curEntry) {
223                 ghi->curEntry= ghi->curEntry->next;
224                 while (!ghi->curEntry) {
225                         ghi->curBucket++;
226                         if (ghi->curBucket==ghi->gh->nbuckets)
227                                 break;
228                         ghi->curEntry= ghi->gh->buckets[ghi->curBucket];
229                 }
230         }
231 }
232 int BLI_ghashIterator_isDone(GHashIterator *ghi) {
233         return !ghi->curEntry;
234 }
235
236 /***/
237
238 unsigned int BLI_ghashutil_ptrhash(const void *key) {
239         return (unsigned int)(intptr_t)key;
240 }
241 int BLI_ghashutil_ptrcmp(const void *a, const void *b) {
242         if (a==b)
243                 return 0;
244         else
245                 return (a<b)?-1:1;
246 }
247
248 unsigned int BLI_ghashutil_inthash(const void *ptr) {
249         uintptr_t key = (uintptr_t)ptr;
250
251         key += ~(key << 16);
252         key ^=  (key >>  5);
253         key +=  (key <<  3);
254         key ^=  (key >> 13);
255         key += ~(key <<  9);
256         key ^=  (key >> 17);
257
258         return (unsigned int)(key & 0xffffffff);
259 }
260
261 int BLI_ghashutil_intcmp(const void *a, const void *b) {
262         if (a==b)
263                 return 0;
264         else
265                 return (a<b)?-1:1;
266 }
267
268 unsigned int BLI_ghashutil_strhash(const void *ptr) {
269         const char *s= ptr;
270         unsigned int i= 0;
271         unsigned char c;
272         
273         while ( (c= *s++) )
274                 i= i*37 + c;
275                 
276         return i;
277 }
278 int BLI_ghashutil_strcmp(const void *a, const void *b) {
279         return strcmp(a, b);
280 }