abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hashInt.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [hashInt.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]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - May 16, 2006.]
16 
17  Revision [$Id: vecInt.h,v 1.00 2005/06/20 00:00:00 ahurst Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__misc__hash__hashInt_h
22 #define ABC__misc__hash__hashInt_h
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// INCLUDES ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #include <stdio.h>
30 #include "misc/extra/extra.h"
31 
33 
34 
35 extern int Hash_DefaultHashFunc(int key, int nBins);
36 
37 ////////////////////////////////////////////////////////////////////////
38 /// PARAMETERS ///
39 ////////////////////////////////////////////////////////////////////////
40 
41 ////////////////////////////////////////////////////////////////////////
42 /// BASIC TYPES ///
43 ////////////////////////////////////////////////////////////////////////
44 
45 typedef struct Hash_Int_t_ Hash_Int_t;
47 
49 {
50  int key;
51  int data;
53 };
54 
55 struct Hash_Int_t_
56 {
57  int nSize;
58  int nBins;
59  int (* fHash)(int key, int nBins);
61 };
62 
63 
64 
65 ////////////////////////////////////////////////////////////////////////
66 /// MACRO DEFINITIONS ///
67 ////////////////////////////////////////////////////////////////////////
68 
69 #define Hash_IntForEachEntry( pHash, pEntry, bin) \
70  for(bin=-1, pEntry=NULL; bin < pHash->nBins; (!pEntry)?(pEntry=pHash->pArray[++bin]):(pEntry=pEntry->pNext)) \
71  if (pEntry)
72 
73 ////////////////////////////////////////////////////////////////////////
74 /// FUNCTION DEFINITIONS ///
75 ////////////////////////////////////////////////////////////////////////
76 
77 /**Function*************************************************************
78 
79  Synopsis [Allocates a hash map with the given number of bins.]
80 
81  Description []
82 
83  SideEffects []
84 
85  SeeAlso []
86 
87 ***********************************************************************/
88 static inline Hash_Int_t * Hash_IntAlloc( int nBins )
89 {
90  Hash_Int_t * p;
91  int i;
92  assert(nBins > 0);
93  p = ABC_ALLOC( Hash_Int_t, 1);
94  p->nBins = nBins;
96  p->nSize = 0;
97  p->pArray = ABC_ALLOC( Hash_Int_Entry_t *, nBins+1 );
98  for(i=0; i<nBins; i++)
99  p->pArray[i] = NULL;
100 
101  return p;
102 }
103 
104 /**Function*************************************************************
105 
106  Synopsis [Returns 1 if a key already exists.]
107 
108  Description []
109 
110  SideEffects []
111 
112  SeeAlso []
113 
114 ***********************************************************************/
115 static inline int Hash_IntExists( Hash_Int_t *p, int key)
116 {
117  int bin;
118  Hash_Int_Entry_t *pEntry, **pLast;
119 
120  // find the bin where this key would live
121  bin = (*(p->fHash))(key, p->nBins);
122 
123  // search for key
124  pLast = &(p->pArray[bin]);
125  pEntry = p->pArray[bin];
126  while(pEntry) {
127  if (pEntry->key == key) {
128  return 1;
129  }
130  pLast = &(pEntry->pNext);
131  pEntry = pEntry->pNext;
132  }
133 
134  return 0;
135 }
136 
137 /**Function*************************************************************
138 
139  Synopsis [Finds or creates an entry with a key and writes value.]
140 
141  Description []
142 
143  SideEffects []
144 
145  SeeAlso []
146 
147 ***********************************************************************/
148 static inline void Hash_IntWriteEntry( Hash_Int_t *p, int key, int data )
149 {
150  int bin;
151  Hash_Int_Entry_t *pEntry, **pLast;
152 
153  // find the bin where this key would live
154  bin = (*(p->fHash))(key, p->nBins);
155 
156  // search for key
157  pLast = &(p->pArray[bin]);
158  pEntry = p->pArray[bin];
159  while(pEntry) {
160  if (pEntry->key == key) {
161  pEntry->data = data;
162  return;
163  }
164  pLast = &(pEntry->pNext);
165  pEntry = pEntry->pNext;
166  }
167 
168  // this key does not currently exist
169  // create a new entry and add to bin
170  p->nSize++;
171  (*pLast) = pEntry = ABC_ALLOC( Hash_Int_Entry_t, 1 );
172  pEntry->pNext = NULL;
173  pEntry->key = key;
174  pEntry->data = data;
175 
176  return;
177 }
178 
179 
180 /**Function*************************************************************
181 
182  Synopsis [Finds or creates an entry with a key.]
183 
184  Description [fCreate specifies whether new entries will be created.]
185 
186  SideEffects []
187 
188  SeeAlso []
189 
190 ***********************************************************************/
191 static inline int Hash_IntEntry( Hash_Int_t *p, int key, int fCreate )
192 {
193  int bin;
194  Hash_Int_Entry_t *pEntry, **pLast;
195 
196  // find the bin where this key would live
197  bin = (*(p->fHash))(key, p->nBins);
198 
199  // search for key
200  pLast = &(p->pArray[bin]);
201  pEntry = p->pArray[bin];
202  while(pEntry) {
203  if (pEntry->key == key)
204  return pEntry->data;
205  pLast = &(pEntry->pNext);
206  pEntry = pEntry->pNext;
207  }
208 
209  // this key does not currently exist
210  if (fCreate) {
211  // create a new entry and add to bin
212  p->nSize++;
213  (*pLast) = pEntry = ABC_ALLOC( Hash_Int_Entry_t, 1 );
214  pEntry->pNext = NULL;
215  pEntry->key = key;
216  pEntry->data = 0;
217  return pEntry->data;
218  }
219 
220  return 0;
221 }
222 
223 
224 /**Function*************************************************************
225 
226  Synopsis [Finds or creates an entry with a key and returns the pointer to it.]
227 
228  Description []
229 
230  SideEffects []
231 
232  SeeAlso []
233 
234 ***********************************************************************/
235 static inline int* Hash_IntEntryPtr( Hash_Int_t *p, int key )
236 {
237  int bin;
238  Hash_Int_Entry_t *pEntry, **pLast;
239 
240  // find the bin where this key would live
241  bin = (*(p->fHash))(key, p->nBins);
242 
243  // search for key
244  pLast = &(p->pArray[bin]);
245  pEntry = p->pArray[bin];
246  while(pEntry) {
247  if (pEntry->key == key)
248  return &(pEntry->data);
249  pLast = &(pEntry->pNext);
250  pEntry = pEntry->pNext;
251  }
252 
253  // this key does not currently exist
254  // create a new entry and add to bin
255  p->nSize++;
256  (*pLast) = pEntry = ABC_ALLOC( Hash_Int_Entry_t, 1 );
257  pEntry->pNext = NULL;
258  pEntry->key = key;
259  pEntry->data = 0;
260 
261  return &(pEntry->data);
262 }
263 
264 /**Function*************************************************************
265 
266  Synopsis [Frees the hash.]
267 
268  Description []
269 
270  SideEffects []
271 
272  SeeAlso []
273 
274 ***********************************************************************/
275 static inline void Hash_IntFree( Hash_Int_t *p ) {
276  int bin;
277  Hash_Int_Entry_t *pEntry, *pTemp;
278 
279  // free bins
280  for(bin = 0; bin < p->nBins; bin++) {
281  pEntry = p->pArray[bin];
282  while(pEntry) {
283  pTemp = pEntry;
284  pEntry = pEntry->pNext;
285  ABC_FREE( pTemp );
286  }
287  }
288 
289  // free hash
290  ABC_FREE( p->pArray );
291  ABC_FREE( p );
292 }
293 
294 ////////////////////////////////////////////////////////////////////////
295 /// END OF FILE ///
296 ////////////////////////////////////////////////////////////////////////
297 
298 
299 
301 
302 #endif
static Hash_Int_t * Hash_IntAlloc(int nBins)
FUNCTION DEFINITIONS ///.
Definition: hashInt.h:88
Definition: hashInt.h:48
struct Hash_Int_Entry_t_ * pNext
Definition: hashInt.h:52
int nSize
Definition: hashInt.h:57
static Llb_Mgr_t * p
Definition: llb3Image.c:950
int nBins
Definition: hashInt.h:58
static int * Hash_IntEntryPtr(Hash_Int_t *p, int key)
Definition: hashInt.h:235
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
static void Hash_IntFree(Hash_Int_t *p)
Definition: hashInt.h:275
Hash_Int_Entry_t ** pArray
Definition: hashInt.h:60
int(* fHash)(int key, int nBins)
Definition: hashInt.h:59
static int Hash_IntExists(Hash_Int_t *p, int key)
Definition: hashInt.h:115
ABC_NAMESPACE_HEADER_START int Hash_DefaultHashFunc(int key, int nBins)
INCLUDES ///.
Definition: hash.h:57
int key
Definition: hashInt.h:50
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Definition: abc_global.h:105
#define ABC_NAMESPACE_HEADER_END
Definition: abc_global.h:106
static void Hash_IntWriteEntry(Hash_Int_t *p, int key, int data)
Definition: hashInt.h:148
static int Hash_IntEntry(Hash_Int_t *p, int key, int fCreate)
Definition: hashInt.h:191
#define ABC_FREE(obj)
Definition: abc_global.h:232
enum keys key
int data
Definition: hashInt.h:51
#define assert(ex)
Definition: util_old.h:213