ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
hashGen.h
Go to the documentation of this file.
1
20
21#ifndef ABC__misc__hash__hashGen_h
22#define ABC__misc__hash__hashGen_h
23
24
28
29#include <stdio.h>
30#include "misc/extra/extra.h"
31
33
34
38
42
43typedef struct Hash_Gen_t_ Hash_Gen_t;
45
47{
48 char * key;
49 void * data;
51};
52
53typedef int (*Hash_GenHashFunction_t)(void* key, int nBins);
54typedef int (*Hash_GenCompFunction_t)(void* key, void* data);
55
65
66
67
71
72#define Hash_GenForEachEntry( pHash, pEntry, bin ) \
73 for(bin=-1, pEntry=NULL; bin < pHash->nBins; (!pEntry)?(pEntry=pHash->pArray[++bin]):(pEntry=pEntry->pNext)) \
74 if (pEntry)
75
79
91static int Hash_DefaultHashFuncStr( void * key, int nBins )
92{
93 const char* p = (const char*)key;
94 int h=0;
95
96 for( ; *p ; ++p )
97 h += h*5 + *p;
98
99 return (unsigned)h % nBins;
100}
101
102static int Hash_DefaultCmpFuncStr( void * key1, void * key2 )
103{
104 return strcmp((const char*)key1, (const char*) key2);
105}
106
118static int Hash_DefaultHashFuncInt( void * key, int nBins )
119{
120 return (long)key % nBins;
121}
122
134static int Hash_DefaultCmpFuncInt( void * key1, void* key2 )
135{
136 return (long)key1 - (long)key2;
137}
138
150static inline Hash_Gen_t * Hash_GenAlloc(
151 int nBins,
152 int (*Hash_FuncHash)(void *, int),
153 int (*Hash_FuncComp)(void *, void *),
154 int fFreeKey)
155{
156 Hash_Gen_t * p;
157 assert(nBins > 0);
158 p = ABC_CALLOC( Hash_Gen_t, 1 );
159 p->nBins = nBins;
160 p->fHash = Hash_FuncHash? Hash_FuncHash : (int (*)(void *, int))Hash_DefaultHashFuncStr;
161 p->fComp = Hash_FuncComp? Hash_FuncComp : (int (*)(void *, void *))Hash_DefaultCmpFuncStr;
162 p->fFreeKey = fFreeKey;
163 p->nSize = 0;
164 p->pArray = ABC_CALLOC( Hash_Gen_Entry_t *, nBins );
165 return p;
166}
167
179static inline int Hash_GenExists( Hash_Gen_t *p, void * key )
180{
181 int bin;
182 Hash_Gen_Entry_t *pEntry;
183
184 // find the bin where this key would live
185 bin = (*(p->fHash))(key, p->nBins);
186
187 // search for key
188 pEntry = p->pArray[bin];
189 while(pEntry) {
190 if ( !p->fComp(pEntry->key,key) ) {
191 return 1;
192 }
193 pEntry = pEntry->pNext;
194 }
195
196 return 0;
197}
198
210static inline void Hash_GenWriteEntry( Hash_Gen_t *p, void * key, void * data )
211{
212 int bin;
213 Hash_Gen_Entry_t *pEntry, **pLast;
214
215 // find the bin where this key would live
216 bin = (*(p->fHash))(key, p->nBins);
217
218 // search for key
219 pLast = &(p->pArray[bin]);
220 pEntry = p->pArray[bin];
221 while(pEntry) {
222 if ( !p->fComp(pEntry->key,key) ) {
223 pEntry->data = data;
224 return;
225 }
226 pLast = &(pEntry->pNext);
227 pEntry = pEntry->pNext;
228 }
229
230 // this key does not currently exist
231 // create a new entry and add to bin
232 p->nSize++;
233 (*pLast) = pEntry = ABC_ALLOC( Hash_Gen_Entry_t, 1 );
234 pEntry->pNext = NULL;
235 pEntry->key = (char *)key;
236 pEntry->data = data;
237
238 return;
239}
240
241
253static inline Hash_Gen_Entry_t * Hash_GenEntry( Hash_Gen_t *p, void * key, int fCreate )
254{
255 int bin;
256 Hash_Gen_Entry_t *pEntry, **pLast;
257
258 // find the bin where this key would live
259 bin = (*(p->fHash))(key, p->nBins);
260
261 // search for key
262 pLast = &(p->pArray[bin]);
263 pEntry = p->pArray[bin];
264 while(pEntry) {
265 if ( !p->fComp(pEntry->key,key) )
266 return pEntry;
267 pLast = &(pEntry->pNext);
268 pEntry = pEntry->pNext;
269 }
270
271 // this key does not currently exist
272 if (fCreate) {
273 // create a new entry and add to bin
274 p->nSize++;
275 (*pLast) = pEntry = ABC_ALLOC( Hash_Gen_Entry_t, 1 );
276 pEntry->pNext = NULL;
277 pEntry->key = (char *)key;
278 pEntry->data = NULL;
279 return pEntry;
280 }
281
282 return NULL;
283}
284
296static inline void* Hash_GenRemove( Hash_Gen_t *p, void * key )
297{
298 int bin;
299 void * data;
300 Hash_Gen_Entry_t *pEntry, **pLast;
301
302 // find the bin where this key would live
303 bin = (*(p->fHash))(key, p->nBins);
304
305 // search for key
306 pLast = &(p->pArray[bin]);
307 pEntry = p->pArray[bin];
308 while(pEntry) {
309 if ( !p->fComp(pEntry->key,key) ) {
310 p->nSize--;
311 data = pEntry->data;
312 *pLast = pEntry->pNext;
313 if (p->fFreeKey)
314 ABC_FREE(pEntry->key);
315 ABC_FREE(pEntry);
316 return data;
317 }
318 pLast = &(pEntry->pNext);
319 pEntry = pEntry->pNext;
320 }
321
322 // could not find key
323 return NULL;
324}
325
337static inline void Hash_GenFree( Hash_Gen_t *p )
338{
339 int bin;
340 Hash_Gen_Entry_t *pEntry, *pTemp;
341
342 // free bins
343 for(bin = 0; bin < p->nBins; bin++) {
344 pEntry = p->pArray[bin];
345 while(pEntry) {
346 pTemp = pEntry;
347 if( p->fFreeKey )
348 ABC_FREE(pTemp->key);
349 pEntry = pEntry->pNext;
350 ABC_FREE( pTemp );
351 }
352 }
353
354 // free hash
355 ABC_FREE( p->pArray );
356 ABC_FREE( p );
357}
358
362
363
364
366
367#endif
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Cube * p
Definition exorList.c:222
int(* Hash_GenCompFunction_t)(void *key, void *data)
Definition hashGen.h:54
int(* Hash_GenHashFunction_t)(void *key, int nBins)
Definition hashGen.h:53
typedefABC_NAMESPACE_HEADER_START struct Hash_Gen_t_ Hash_Gen_t
INCLUDES ///.
Definition hashGen.h:43
struct Hash_Gen_Entry_t_ Hash_Gen_Entry_t
Definition hashGen.h:44
enum keys key
Definition main.h:25
Definition hashGen.h:47
struct Hash_Gen_Entry_t_ * pNext
Definition hashGen.h:50
char * key
Definition hashGen.h:48
void * data
Definition hashGen.h:49
Hash_GenHashFunction_t fHash
Definition hashGen.h:60
int fFreeKey
Definition hashGen.h:62
Hash_Gen_Entry_t ** pArray
Definition hashGen.h:63
int nSize
Definition hashGen.h:58
int nBins
Definition hashGen.h:59
Hash_GenCompFunction_t fComp
Definition hashGen.h:61
#define assert(ex)
Definition util_old.h:213
int strcmp()