7ae68101154650dec362a08e2c8766a15fd0fb76
[blender-staging.git] / source / blender / blenlib / intern / edgehash.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: none of this file.
22  *
23  * Contributor(s): Daniel Dunbar
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * A general (pointer -> pointer) hash table ADT
27  */
28
29 /** \file blender/blenlib/intern/edgehash.c
30  *  \ingroup bli
31  */
32
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38 #include "BLI_edgehash.h"
39
40 /***/
41
42 static unsigned int hashsizes[]= {
43         1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, 
44         16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169, 
45         4194319, 8388617, 16777259, 33554467, 67108879, 134217757, 
46         268435459
47 };
48
49 #define EDGEHASH(v0,v1)         ((v0*39)^(v1*31))
50
51 /***/
52
53 typedef struct Entry Entry;
54 struct Entry {
55         Entry *next;
56         int v0, v1;
57         void *val;
58 };
59
60 struct EdgeHash {
61         Entry **buckets;
62         int nbuckets, nentries, cursize;
63 };
64
65 /***/
66
67 EdgeHash *BLI_edgehash_new(void)
68 {
69         EdgeHash *eh= MEM_mallocN(sizeof(*eh), "EdgeHash");
70         eh->cursize= 0;
71         eh->nentries= 0;
72         eh->nbuckets= hashsizes[eh->cursize];
73         
74         eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets));
75         memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets));
76         
77         return eh;
78 }
79
80 void BLI_edgehash_insert(EdgeHash *eh, int v0, int v1, void *val)
81 {
82         unsigned int hash;
83         Entry *e= malloc(sizeof(*e));
84
85         if (v1<v0) {
86                 v0 ^= v1;
87                 v1 ^= v0;
88                 v0 ^= v1;
89         }
90         hash = EDGEHASH(v0,v1)%eh->nbuckets;
91
92         e->v0 = v0;
93         e->v1 = v1;
94         e->val = val;
95         e->next= eh->buckets[hash];
96         eh->buckets[hash]= e;
97         
98         if (++eh->nentries>eh->nbuckets*3) {
99                 Entry **old= eh->buckets;
100                 int i, nold= eh->nbuckets;
101                 
102                 eh->nbuckets= hashsizes[++eh->cursize];
103                 eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets));
104                 memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets));
105                 
106                 for (i=0; i<nold; i++) {
107                         for (e= old[i]; e;) {
108                                 Entry *n= e->next;
109                                 
110                                 hash= EDGEHASH(e->v0,e->v1)%eh->nbuckets;
111                                 e->next= eh->buckets[hash];
112                                 eh->buckets[hash]= e;
113                                 
114                                 e= n;
115                         }
116                 }
117                 
118                 free(old);
119         }
120 }
121
122 void** BLI_edgehash_lookup_p(EdgeHash *eh, int v0, int v1)
123 {
124         unsigned int hash;
125         Entry *e;
126
127         if (v1<v0) {
128                 v0 ^= v1;
129                 v1 ^= v0;
130                 v0 ^= v1;
131         }
132         hash = EDGEHASH(v0,v1)%eh->nbuckets;
133         for (e= eh->buckets[hash]; e; e= e->next)
134                 if (v0==e->v0 && v1==e->v1)
135                         return &e->val;
136         
137         return NULL;
138 }
139
140 void* BLI_edgehash_lookup(EdgeHash *eh, int v0, int v1)
141 {
142         void **value_p = BLI_edgehash_lookup_p(eh,v0,v1);
143
144         return value_p?*value_p:NULL;
145 }
146
147 int BLI_edgehash_haskey(EdgeHash *eh, int v0, int v1)
148 {
149         return BLI_edgehash_lookup_p(eh, v0, v1)!=NULL;
150 }
151
152 int BLI_edgehash_size(EdgeHash *eh)
153 {
154         return eh->nentries;
155 }
156
157 void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp)
158 {
159         int i;
160         
161         for (i=0; i<eh->nbuckets; i++) {
162                 Entry *e;
163                 
164                 for (e= eh->buckets[i]; e; ) {
165                         Entry *n= e->next;
166                         
167                         if (valfreefp) valfreefp(e->val);
168                         free(e);
169                         
170                         e= n;
171                 }
172                 eh->buckets[i]= NULL;
173         }
174
175         eh->nentries= 0;
176 }
177
178 void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp)
179 {
180         BLI_edgehash_clear(eh, valfreefp);
181         
182         free(eh->buckets);
183         MEM_freeN(eh);
184 }
185
186
187 /***/
188
189 struct EdgeHashIterator {
190         EdgeHash *eh;
191         int curBucket;
192         Entry *curEntry;
193 };
194
195 EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh)
196 {
197         EdgeHashIterator *ehi= malloc(sizeof(*ehi));
198         ehi->eh= eh;
199         ehi->curEntry= NULL;
200         ehi->curBucket= -1;
201         while (!ehi->curEntry) {
202                 ehi->curBucket++;
203                 if (ehi->curBucket==ehi->eh->nbuckets)
204                         break;
205                 ehi->curEntry= ehi->eh->buckets[ehi->curBucket];
206         }
207         return ehi;
208 }
209 void BLI_edgehashIterator_free(EdgeHashIterator *ehi)
210 {
211         free(ehi);
212 }
213
214 void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, int *v0_r, int *v1_r)
215 {
216         if (ehi->curEntry) {
217                 *v0_r = ehi->curEntry->v0;
218                 *v1_r = ehi->curEntry->v1;
219         }
220 }
221 void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi)
222 {
223         return ehi->curEntry?ehi->curEntry->val:NULL;
224 }
225
226 void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val)
227 {
228         if(ehi->curEntry)
229                 ehi->curEntry->val= val;
230 }
231
232 void BLI_edgehashIterator_step(EdgeHashIterator *ehi)
233 {
234         if (ehi->curEntry) {
235                 ehi->curEntry= ehi->curEntry->next;
236                 while (!ehi->curEntry) {
237                         ehi->curBucket++;
238                         if (ehi->curBucket==ehi->eh->nbuckets)
239                                 break;
240                         ehi->curEntry= ehi->eh->buckets[ehi->curBucket];
241                 }
242         }
243 }
244 int BLI_edgehashIterator_isDone(EdgeHashIterator *ehi)
245 {
246         return !ehi->curEntry;
247 }
248