abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hashGen.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [vecGen.h]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Hash maps.]
8 
9  Synopsis [Hash maps.]
10 
11  Author [Aaron P. Hurst, Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - Jan 26, 2011.]
16 
17  Revision [$Id: vecGen.h,v 1.00 2005/06/20 00:00:00 ahurst Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__misc__hash__hashGen_h
22 #define ABC__misc__hash__hashGen_h
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// INCLUDES ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #include <stdio.h>
30 #include "misc/extra/extra.h"
31 
33 
34 
35 ////////////////////////////////////////////////////////////////////////
36 /// PARAMETERS ///
37 ////////////////////////////////////////////////////////////////////////
38 
39 ////////////////////////////////////////////////////////////////////////
40 /// BASIC TYPES ///
41 ////////////////////////////////////////////////////////////////////////
42 
43 typedef struct Hash_Gen_t_ Hash_Gen_t;
45 
47 {
48  char * key;
49  void * data;
51 };
52 
53 typedef int (*Hash_GenHashFunction_t)(void* key, int nBins);
54 typedef int (*Hash_GenCompFunction_t)(void* key, void* data);
55 
56 struct Hash_Gen_t_
57 {
58  int nSize;
59  int nBins;
62  int fFreeKey;
64 };
65 
66 
67 
68 ////////////////////////////////////////////////////////////////////////
69 /// MACRO DEFINITIONS ///
70 ////////////////////////////////////////////////////////////////////////
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 
76 ////////////////////////////////////////////////////////////////////////
77 /// FUNCTION DEFINITIONS ///
78 ////////////////////////////////////////////////////////////////////////
79 
80 /**Function*************************************************************
81 
82  Synopsis [Default hash function for strings.]
83 
84  Description []
85 
86  SideEffects []
87 
88  SeeAlso []
89 
90 ***********************************************************************/
91 static 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 
102 static int Hash_DefaultCmpFuncStr( void * key1, void * key2 )
103 {
104  return strcmp((const char*)key1, (const char*) key2);
105 }
106 
107 /**Function*************************************************************
108 
109  Synopsis [Default hash function for (long) integers.]
110 
111  Description []
112 
113  SideEffects []
114 
115  SeeAlso []
116 
117 ***********************************************************************/
118 static int Hash_DefaultHashFuncInt( void * key, int nBins )
119 {
120  return (long)key % nBins;
121 }
122 
123 /**Function*************************************************************
124 
125  Synopsis [Default comparison function for (long) integers.]
126 
127  Description []
128 
129  SideEffects []
130 
131  SeeAlso []
132 
133 ***********************************************************************/
134 static int Hash_DefaultCmpFuncInt( void * key1, void* key2 )
135 {
136  return (long)key1 - (long)key2;
137 }
138 
139 /**Function*************************************************************
140 
141  Synopsis [Allocates a hash map with the given number of bins.]
142 
143  Description []
144 
145  SideEffects []
146 
147  SeeAlso []
148 
149 ***********************************************************************/
150 static 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 
168 /**Function*************************************************************
169 
170  Synopsis [Returns 1 if a key already exists.]
171 
172  Description []
173 
174  SideEffects []
175 
176  SeeAlso []
177 
178 ***********************************************************************/
179 static 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 
199 /**Function*************************************************************
200 
201  Synopsis [Finds or creates an entry with a key and writes value.]
202 
203  Description []
204 
205  SideEffects []
206 
207  SeeAlso []
208 
209 ***********************************************************************/
210 static 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 
242 /**Function*************************************************************
243 
244  Synopsis [Finds or creates an entry with a key.]
245 
246  Description [fCreate specifies whether a new entry should be created.]
247 
248  SideEffects []
249 
250  SeeAlso []
251 
252 ***********************************************************************/
253 static 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 
285 /**Function*************************************************************
286 
287  Synopsis [Deletes an entry.]
288 
289  Description [Returns data, if there was any.]
290 
291  SideEffects []
292 
293  SeeAlso []
294 
295 ***********************************************************************/
296 static 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 
326 /**Function*************************************************************
327 
328  Synopsis [Frees the hash.]
329 
330  Description []
331 
332  SideEffects []
333 
334  SeeAlso []
335 
336 ***********************************************************************/
337 static 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 
359 ////////////////////////////////////////////////////////////////////////
360 /// END OF FILE ///
361 ////////////////////////////////////////////////////////////////////////
362 
363 
364 
366 
367 #endif
static void * Hash_GenRemove(Hash_Gen_t *p, void *key)
Definition: hashGen.h:296
static int Hash_DefaultHashFuncStr(void *key, int nBins)
FUNCTION DEFINITIONS ///.
Definition: hashGen.h:91
static Llb_Mgr_t * p
Definition: llb3Image.c:950
char * key
Definition: hashGen.h:48
Hash_GenHashFunction_t fHash
Definition: hashGen.h:60
static Hash_Gen_Entry_t * Hash_GenEntry(Hash_Gen_t *p, void *key, int fCreate)
Definition: hashGen.h:253
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static int Hash_DefaultHashFuncInt(void *key, int nBins)
Definition: hashGen.h:118
int strcmp()
Hash_Gen_Entry_t ** pArray
Definition: hashGen.h:63
void * data
Definition: hashGen.h:49
static Hash_Gen_t * Hash_GenAlloc(int nBins, int(*Hash_FuncHash)(void *, int), int(*Hash_FuncComp)(void *, void *), int fFreeKey)
Definition: hashGen.h:150
static void Hash_GenFree(Hash_Gen_t *p)
Definition: hashGen.h:337
int(* Hash_GenCompFunction_t)(void *key, void *data)
Definition: hashGen.h:54
static int Hash_DefaultCmpFuncStr(void *key1, void *key2)
Definition: hashGen.h:102
int nSize
Definition: hashGen.h:58
int fFreeKey
Definition: hashGen.h:62
int(* Hash_GenHashFunction_t)(void *key, int nBins)
Definition: hashGen.h:53
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Definition: abc_global.h:105
typedefABC_NAMESPACE_HEADER_START struct Hash_Gen_t_ Hash_Gen_t
INCLUDES ///.
Definition: hashGen.h:43
#define ABC_NAMESPACE_HEADER_END
Definition: abc_global.h:106
Hash_GenCompFunction_t fComp
Definition: hashGen.h:61
static int Hash_DefaultCmpFuncInt(void *key1, void *key2)
Definition: hashGen.h:134
int nBins
Definition: hashGen.h:59
#define ABC_FREE(obj)
Definition: abc_global.h:232
enum keys key
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
#define assert(ex)
Definition: util_old.h:213
static int Hash_GenExists(Hash_Gen_t *p, void *key)
Definition: hashGen.h:179
Definition: hashGen.h:46
static void Hash_GenWriteEntry(Hash_Gen_t *p, void *key, void *data)
Definition: hashGen.h:210
struct Hash_Gen_Entry_t_ * pNext
Definition: hashGen.h:50