ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
hashPtr.h
Go to the documentation of this file.
1
20
21#ifndef ABC__misc__hash__hashPtr_h
22#define ABC__misc__hash__hashPtr_h
23
24
28
29#include <stdio.h>
30#include "misc/extra/extra.h"
31
33
34
35extern int Hash_DefaultHashFunc(int key, int nBins);
36
40
44
45typedef struct Hash_Ptr_t_ Hash_Ptr_t;
47
49{
50 int key;
51 void * data;
53};
54
56{
57 int nSize;
58 int nBins;
59 int (* fHash)(int key, int nBins);
61};
62
63
64
68
69#define Hash_PtrForEachEntry( pHash, pEntry, bin ) \
70 for(bin=-1, pEntry=NULL; bin < pHash->nBins; (!pEntry)?(pEntry=pHash->pArray[++bin]):(pEntry=pEntry->pNext)) \
71 if (pEntry)
72
76
88static inline Hash_Ptr_t * Hash_PtrAlloc( int nBins )
89{
90 Hash_Ptr_t * p;
91 int i;
92 assert(nBins > 0);
93 p = ABC_ALLOC( Hash_Ptr_t, 1);
94 p->nBins = nBins;
95 p->fHash = Hash_DefaultHashFunc;
96 p->nSize = 0;
97 p->pArray = ABC_ALLOC( Hash_Ptr_Entry_t *, nBins );
98 for(i=0; i<nBins; i++)
99 p->pArray[i] = NULL;
100
101 return p;
102}
103
115static inline int Hash_PtrExists( Hash_Ptr_t *p, int key )
116{
117 int bin;
118 Hash_Ptr_Entry_t *pEntry;
119
120 // find the bin where this key would live
121 bin = (*(p->fHash))(key, p->nBins);
122
123 // search for key
124 pEntry = p->pArray[bin];
125 while(pEntry) {
126 if (pEntry->key == key) {
127 return 1;
128 }
129 pEntry = pEntry->pNext;
130 }
131
132 return 0;
133}
134
146static inline void Hash_PtrWriteEntry( Hash_Ptr_t *p, int key, void * data )
147{
148 int bin;
149 Hash_Ptr_Entry_t *pEntry, **pLast;
150
151 // find the bin where this key would live
152 bin = (*(p->fHash))(key, p->nBins);
153
154 // search for key
155 pLast = &(p->pArray[bin]);
156 pEntry = p->pArray[bin];
157 while(pEntry) {
158 if (pEntry->key == key) {
159 pEntry->data = data;
160 return;
161 }
162 pLast = &(pEntry->pNext);
163 pEntry = pEntry->pNext;
164 }
165
166 // this key does not currently exist
167 // create a new entry and add to bin
168 p->nSize++;
169 (*pLast) = pEntry = ABC_ALLOC( Hash_Ptr_Entry_t, 1 );
170 pEntry->pNext = NULL;
171 pEntry->key = key;
172 pEntry->data = data;
173
174 return;
175}
176
177
189static inline void * Hash_PtrEntry( Hash_Ptr_t *p, int key, int fCreate )
190{
191 int bin;
192 Hash_Ptr_Entry_t *pEntry, **pLast;
193
194 // find the bin where this key would live
195 bin = (*(p->fHash))(key, p->nBins);
196
197 // search for key
198 pLast = &(p->pArray[bin]);
199 pEntry = p->pArray[bin];
200 while(pEntry) {
201 if (pEntry->key == key)
202 return pEntry->data;
203 pLast = &(pEntry->pNext);
204 pEntry = pEntry->pNext;
205 }
206
207 // this key does not currently exist
208 if (fCreate) {
209 // create a new entry and add to bin
210 p->nSize++;
211 (*pLast) = pEntry = ABC_ALLOC( Hash_Ptr_Entry_t, 1 );
212 pEntry->pNext = NULL;
213 pEntry->key = key;
214 pEntry->data = NULL;
215 return pEntry->data;
216 }
217
218 return NULL;
219}
220
221
233static inline void** Hash_PtrEntryPtr( Hash_Ptr_t *p, int key )
234{
235 int bin;
236 Hash_Ptr_Entry_t *pEntry, **pLast;
237
238 // find the bin where this key would live
239 bin = (*(p->fHash))(key, p->nBins);
240
241 // search for key
242 pLast = &(p->pArray[bin]);
243 pEntry = p->pArray[bin];
244 while(pEntry) {
245 if (pEntry->key == key)
246 return &(pEntry->data);
247 pLast = &(pEntry->pNext);
248 pEntry = pEntry->pNext;
249 }
250
251 // this key does not currently exist
252 // create a new entry and add to bin
253 p->nSize++;
254 (*pLast) = pEntry = ABC_ALLOC( Hash_Ptr_Entry_t, 1 );
255 pEntry->pNext = NULL;
256 pEntry->key = key;
257 pEntry->data = NULL;
258
259 return &(pEntry->data);
260}
261
273static inline void* Hash_PtrRemove( Hash_Ptr_t *p, int key )
274{
275 int bin;
276 void * data;
277 Hash_Ptr_Entry_t *pEntry, **pLast;
278
279 // find the bin where this key would live
280 bin = (*(p->fHash))(key, p->nBins);
281
282 // search for key
283 pLast = &(p->pArray[bin]);
284 pEntry = p->pArray[bin];
285 while(pEntry) {
286 if (pEntry->key == key) {
287 p->nSize--;
288 data = pEntry->data;
289 *pLast = pEntry->pNext;
290 return data;
291 }
292 pLast = &(pEntry->pNext);
293 pEntry = pEntry->pNext;
294 }
295
296 // could not find key
297 return NULL;
298}
299
311static inline void Hash_PtrFree( Hash_Ptr_t *p )
312{
313 int bin;
314 Hash_Ptr_Entry_t *pEntry, *pTemp;
315
316 // free bins
317 for(bin = 0; bin < p->nBins; bin++) {
318 pEntry = p->pArray[bin];
319 while(pEntry) {
320 pTemp = pEntry;
321 pEntry = pEntry->pNext;
322 ABC_FREE( pTemp );
323 }
324 }
325
326 // free hash
327 ABC_FREE( p->pArray );
328 ABC_FREE( p );
329}
330
334
335
336
338
339#endif
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#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
ABC_NAMESPACE_HEADER_START int Hash_DefaultHashFunc(int key, int nBins)
INCLUDES ///.
Definition hash.h:57
struct Hash_Ptr_Entry_t_ Hash_Ptr_Entry_t
Definition hashPtr.h:46
struct Hash_Ptr_t_ Hash_Ptr_t
PARAMETERS ///.
Definition hashPtr.h:45
ABC_NAMESPACE_HEADER_START int Hash_DefaultHashFunc(int key, int nBins)
INCLUDES ///.
Definition hash.h:57
enum keys key
Definition main.h:25
Definition hashPtr.h:49
struct Hash_Ptr_Entry_t_ * pNext
Definition hashPtr.h:52
int key
Definition hashPtr.h:50
void * data
Definition hashPtr.h:51
Hash_Ptr_Entry_t ** pArray
Definition hashPtr.h:60
int(* fHash)(int key, int nBins)
Definition hashPtr.h:59
int nBins
Definition hashPtr.h:58
int nSize
Definition hashPtr.h:57
#define assert(ex)
Definition util_old.h:213