svn merge -r 23207:23528 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / blenlib / intern / edgehash.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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: none of this file.
24  *
25  * Contributor(s): Daniel Dunbar
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * A general (pointer -> pointer) hash table ADT
29  */
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35 #include "BLI_edgehash.h"
36
37 /***/
38
39 static unsigned int hashsizes[]= {
40         1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, 
41         16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169, 
42         4194319, 8388617, 16777259, 33554467, 67108879, 134217757, 
43         268435459
44 };
45
46 #define EDGEHASH(v0,v1)         ((v0*39)^(v1*31))
47
48 /***/
49
50 typedef struct Entry Entry;
51 struct Entry {
52         Entry *next;
53         int v0, v1;
54         void *val;
55 };
56
57 struct EdgeHash {
58         Entry **buckets;
59         int nbuckets, nentries, cursize;
60 };
61
62 /***/
63
64 EdgeHash *BLI_edgehash_new(void) {
65         EdgeHash *eh= MEM_mallocN(sizeof(*eh), "EdgeHash");
66         eh->cursize= 0;
67         eh->nentries= 0;
68         eh->nbuckets= hashsizes[eh->cursize];
69         
70         eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets));
71         memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets));
72         
73         return eh;
74 }
75
76 void BLI_edgehash_insert(EdgeHash *eh, int v0, int v1, void *val) {
77         unsigned int hash;
78         Entry *e= malloc(sizeof(*e));
79
80         if (v1<v0) {
81                 v0 ^= v1;
82                 v1 ^= v0;
83                 v0 ^= v1;
84         }
85         hash = EDGEHASH(v0,v1)%eh->nbuckets;
86
87         e->v0 = v0;
88         e->v1 = v1;
89         e->val = val;
90         e->next= eh->buckets[hash];
91         eh->buckets[hash]= e;
92         
93         if (++eh->nentries>eh->nbuckets*3) {
94                 Entry *e, **old= eh->buckets;
95                 int i, nold= eh->nbuckets;
96                 
97                 eh->nbuckets= hashsizes[++eh->cursize];
98                 eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets));
99                 memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets));
100                 
101                 for (i=0; i<nold; i++) {
102                         for (e= old[i]; e;) {
103                                 Entry *n= e->next;
104                                 
105                                 hash= EDGEHASH(e->v0,e->v1)%eh->nbuckets;
106                                 e->next= eh->buckets[hash];
107                                 eh->buckets[hash]= e;
108                                 
109                                 e= n;
110                         }
111                 }
112                 
113                 free(old);
114         }
115 }
116
117 void** BLI_edgehash_lookup_p(EdgeHash *eh, int v0, int v1) {
118         unsigned int hash;
119         Entry *e;
120
121         if (v1<v0) {
122                 v0 ^= v1;
123                 v1 ^= v0;
124                 v0 ^= v1;
125         }
126         hash = EDGEHASH(v0,v1)%eh->nbuckets;
127         for (e= eh->buckets[hash]; e; e= e->next)
128                 if (v0==e->v0 && v1==e->v1)
129                         return &e->val;
130         
131         return NULL;
132 }
133
134 void* BLI_edgehash_lookup(EdgeHash *eh, int v0, int v1) {
135         void **value_p = BLI_edgehash_lookup_p(eh,v0,v1);
136
137         return value_p?*value_p:NULL;
138 }
139
140 int BLI_edgehash_haskey(EdgeHash *eh, int v0, int v1) {
141         return BLI_edgehash_lookup_p(eh, v0, v1)!=NULL;
142 }
143
144 int BLI_edgehash_size(EdgeHash *eh) {
145         return eh->nentries;
146 }
147
148 void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp) {
149         int i;
150         
151         for (i=0; i<eh->nbuckets; i++) {
152                 Entry *e;
153                 
154                 for (e= eh->buckets[i]; e; ) {
155                         Entry *n= e->next;
156                         
157                         if (valfreefp) valfreefp(e->val);
158                         free(e);
159                         
160                         e= n;
161                 }
162                 eh->buckets[i]= NULL;
163         }
164
165         eh->nentries= 0;
166 }
167
168 void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp) {
169         BLI_edgehash_clear(eh, valfreefp);
170         
171         free(eh->buckets);
172         MEM_freeN(eh);
173 }
174
175
176 /***/
177
178 struct EdgeHashIterator {
179         EdgeHash *eh;
180         int curBucket;
181         Entry *curEntry;
182 };
183
184 EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) {
185         EdgeHashIterator *ehi= malloc(sizeof(*ehi));
186         ehi->eh= eh;
187         ehi->curEntry= NULL;
188         ehi->curBucket= -1;
189         while (!ehi->curEntry) {
190                 ehi->curBucket++;
191                 if (ehi->curBucket==ehi->eh->nbuckets)
192                         break;
193                 ehi->curEntry= ehi->eh->buckets[ehi->curBucket];
194         }
195         return ehi;
196 }
197 void BLI_edgehashIterator_free(EdgeHashIterator *ehi) {
198         free(ehi);
199 }
200
201 void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, int *v0_r, int *v1_r) {
202         if (ehi->curEntry) {
203                 *v0_r = ehi->curEntry->v0;
204                 *v1_r = ehi->curEntry->v1;
205         }
206 }
207 void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi) {
208         return ehi->curEntry?ehi->curEntry->val:NULL;
209 }
210
211 void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val) {
212         if(ehi->curEntry)
213                 ehi->curEntry->val= val;
214 }
215
216 void BLI_edgehashIterator_step(EdgeHashIterator *ehi) {
217         if (ehi->curEntry) {
218         ehi->curEntry= ehi->curEntry->next;
219                 while (!ehi->curEntry) {
220                         ehi->curBucket++;
221                         if (ehi->curBucket==ehi->eh->nbuckets)
222                                 break;
223                         ehi->curEntry= ehi->eh->buckets[ehi->curBucket];
224                 }
225         }
226 }
227 int BLI_edgehashIterator_isDone(EdgeHashIterator *ehi) {
228         return !ehi->curEntry;
229 }
230