abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
vecHsh.h
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [vecHsh.h]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [Resizable arrays.]
8 
9  Synopsis [Hashing vector entries.]
10 
11  Author [Alan Mishchenko]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: vecHsh.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
18 
19 ***********************************************************************/
20 
21 #ifndef ABC__misc__vec__vecHsh_h
22 #define ABC__misc__vec__vecHsh_h
23 
24 
25 ////////////////////////////////////////////////////////////////////////
26 /// INCLUDES ///
27 ////////////////////////////////////////////////////////////////////////
28 
29 #include <stdio.h>
30 
32 
33 
34 ////////////////////////////////////////////////////////////////////////
35 /// PARAMETERS ///
36 ////////////////////////////////////////////////////////////////////////
37 
38 ////////////////////////////////////////////////////////////////////////
39 /// BASIC TYPES ///
40 ////////////////////////////////////////////////////////////////////////
41 
42 typedef struct Hsh_IntObj_t_ Hsh_IntObj_t;
44 {
45  int iData;
46  int iNext;
47 };
48 
51 {
54 };
55 
56 typedef struct Hsh_IntMan_t_ Hsh_IntMan_t;
58 {
59  int nSize; // data size
60  Vec_Int_t * vData; // data storage
61  Vec_Int_t * vTable; // hash table
62  Vec_Wrd_t * vObjs; // hash objects
63 };
64 
65 
66 
67 typedef struct Hsh_VecObj_t_ Hsh_VecObj_t;
69 {
70  int nSize;
71  int iNext;
72  int pArray[0];
73 };
74 
75 typedef struct Hsh_VecMan_t_ Hsh_VecMan_t;
77 {
78  Vec_Int_t * vTable; // hash table
79  Vec_Int_t * vData; // data storage
80  Vec_Int_t * vMap; // mapping entries into data;
81  Vec_Int_t vTemp; // temporary array
82 };
83 
84 ////////////////////////////////////////////////////////////////////////
85 /// MACRO DEFINITIONS ///
86 ////////////////////////////////////////////////////////////////////////
87 
88 static inline unsigned * Hsh_IntData( Hsh_IntMan_t * p, int iData ) { return (unsigned *)Vec_IntEntryP( p->vData, p->nSize * iData ); }
89 static inline Hsh_IntObj_t * Hsh_IntObj( Hsh_IntMan_t * p, int iObj ) { return iObj == -1 ? NULL : (Hsh_IntObj_t *)Vec_WrdEntryP( p->vObjs, iObj ); }
90 static inline word Hsh_IntWord( int iData, int iNext ) { Hsh_IntObjWord_t Obj = { {iData, iNext} }; return Obj.wWord; }
91 
92 static inline Hsh_VecObj_t * Hsh_VecObj( Hsh_VecMan_t * p, int i ) { return i == -1 ? NULL : (Hsh_VecObj_t *)Vec_IntEntryP(p->vData, Vec_IntEntry(p->vMap, i)); }
93 
94 ////////////////////////////////////////////////////////////////////////
95 /// FUNCTION DEFINITIONS ///
96 ////////////////////////////////////////////////////////////////////////
97 
98 /**Function*************************************************************
99 
100  Synopsis [Hashing data entries composed of nSize integers.]
101 
102  Description []
103 
104  SideEffects []
105 
106  SeeAlso []
107 
108 ***********************************************************************/
109 static inline Hsh_IntMan_t * Hsh_IntManStart( Vec_Int_t * vData, int nSize, int nEntries )
110 {
111  Hsh_IntMan_t * p;
112  p = ABC_CALLOC( Hsh_IntMan_t, 1 );
113  p->nSize = nSize;
114  p->vData = vData;
115  p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nEntries) );
116  p->vObjs = Vec_WrdAlloc( nEntries );
117  return p;
118 }
119 static inline void Hsh_IntManStop( Hsh_IntMan_t * p )
120 {
121  Vec_IntFree( p->vTable );
122  Vec_WrdFree( p->vObjs );
123  ABC_FREE( p );
124 }
125 
126 /**Function*************************************************************
127 
128  Synopsis []
129 
130  Description []
131 
132  SideEffects []
133 
134  SeeAlso []
135 
136 ***********************************************************************/
137 static inline int Hsh_IntManHash( unsigned * pData, int nSize, int nTableSize )
138 {
139  static int s_Primes[7] = { 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
140  unsigned char * pDataC = (unsigned char *)pData;
141  int c, nChars = nSize * 4;
142  unsigned Key = 0;
143  for ( c = 0; c < nChars; c++ )
144  Key += pDataC[c] * s_Primes[c % 7];
145  return (int)(Key % nTableSize);
146 }
147 static inline int * Hsh_IntManLookup( Hsh_IntMan_t * p, unsigned * pData )
148 {
149  Hsh_IntObj_t * pObj;
150  int * pPlace = Vec_IntEntryP( p->vTable, Hsh_IntManHash(pData, p->nSize, Vec_IntSize(p->vTable)) );
151  for ( ; (pObj = Hsh_IntObj(p, *pPlace)); pPlace = &pObj->iNext )
152  if ( !memcmp( pData, Hsh_IntData(p, pObj->iData), sizeof(int) * p->nSize ) )
153  return pPlace;
154  assert( *pPlace == -1 );
155  return pPlace;
156 }
157 static inline int Hsh_IntManAdd( Hsh_IntMan_t * p, int iData )
158 {
159  int i, * pPlace;
160  if ( Vec_WrdSize(p->vObjs) > Vec_IntSize(p->vTable) )
161  {
163  for ( i = 0; i < Vec_WrdSize(p->vObjs); i++ )
164  {
165  pPlace = Vec_IntEntryP( p->vTable, Hsh_IntManHash(Hsh_IntData(p, i), p->nSize, Vec_IntSize(p->vTable)) );
166  Hsh_IntObj(p, i)->iNext = *pPlace; *pPlace = i;
167  }
168  }
169  pPlace = Hsh_IntManLookup( p, Hsh_IntData(p, iData) );
170  if ( *pPlace == -1 )
171  {
172  *pPlace = Vec_WrdSize(p->vObjs);
173  Vec_WrdPush( p->vObjs, Hsh_IntWord(iData, -1) );
174  return Vec_WrdSize(p->vObjs) - 1;
175  }
176  return (word *)Hsh_IntObj(p, *pPlace) - Vec_WrdArray(p->vObjs);
177 }
178 
179 /**Function*************************************************************
180 
181  Synopsis [Hashes data by value.]
182 
183  Description [Array vData contains data entries, each of 'nSize' integers.
184  The resulting array contains the indexes of unique data entries.]
185 
186  SideEffects []
187 
188  SeeAlso []
189 
190 ***********************************************************************/
191 static inline Vec_Int_t * Hsh_IntManHashArray( Vec_Int_t * vData, int nSize )
192 {
193  Hsh_IntMan_t * p;
194  Vec_Int_t * vRes = Vec_IntAlloc( 100 );
195  int i, nEntries = Vec_IntSize(vData) / nSize;
196  assert( Vec_IntSize(vData) % nSize == 0 );
197  p = Hsh_IntManStart( vData, nSize, nEntries );
198  for ( i = 0; i < nEntries; i++ )
199  Vec_IntPush( vRes, Hsh_IntManAdd(p, i) );
200  Hsh_IntManStop( p );
201  return vRes;
202 }
203 static inline Vec_Int_t * Hsh_WrdManHashArray( Vec_Wrd_t * vDataW, int nSize )
204 {
205  Hsh_IntMan_t * p;
206  Vec_Int_t Data = { 2*Vec_WrdCap(vDataW), 2*Vec_WrdSize(vDataW), (int *)Vec_WrdArray(vDataW) };
207  Vec_Int_t * vData = &Data;
208  Vec_Int_t * vRes = Vec_IntAlloc( 100 );
209  int i, nEntries = Vec_IntSize(vData) / (2*nSize);
210  assert( Vec_IntSize(vData) % (2*nSize) == 0 );
211  p = Hsh_IntManStart( vData, (2*nSize), nEntries );
212  for ( i = 0; i < nEntries; i++ )
213  Vec_IntPush( vRes, Hsh_IntManAdd(p, i) );
214  Hsh_IntManStop( p );
215  return vRes;
216 }
217 static inline Hsh_IntMan_t * Hsh_WrdManHashArrayStart( Vec_Wrd_t * vDataW, int nSize )
218 {
219  Hsh_IntMan_t * p;
220  int i, nEntries = Vec_WrdSize(vDataW) / nSize;
221  Vec_Int_t * vData = Vec_IntAlloc( 2*Vec_WrdSize(vDataW) );
222  memcpy( Vec_IntArray(vData), Vec_WrdArray(vDataW), sizeof(word)*Vec_WrdSize(vDataW) );
223  vData->nSize = 2*Vec_WrdSize(vDataW);
224 /*
225  for ( i = 0; i < 30; i++ )
226  {
227  extern void Extra_PrintHex( FILE * pFile, unsigned * pTruth, int nVars );
228  Extra_PrintHex( stdout, (unsigned *) Vec_WrdEntryP(vDataW, i), 6 ); printf( " " );
229  Kit_DsdPrintFromTruth( (unsigned *) Vec_WrdEntryP(vDataW, i), 6 ); printf( "\n" );
230  }
231 */
232  assert( Vec_IntSize(vData) % (2*nSize) == 0 );
233  p = Hsh_IntManStart( vData, (2*nSize), nEntries );
234  for ( i = 0; i < nEntries; i++ )
235  Hsh_IntManAdd( p, i );
236  assert( Vec_WrdSize(p->vObjs) == nEntries );
237  return p;
238 }
239 
240 /**Function*************************************************************
241 
242  Synopsis [Test procedure.]
243 
244  Description []
245 
246  SideEffects []
247 
248  SeeAlso []
249 
250 ***********************************************************************/
251 static inline void Hsh_IntManHashArrayTest()
252 {
253  Vec_Int_t * vData = Vec_IntAlloc( 10 );
254  Vec_Int_t * vRes;
255  Vec_IntPush( vData, 12 );
256  Vec_IntPush( vData, 17 );
257  Vec_IntPush( vData, 13 );
258  Vec_IntPush( vData, 12 );
259  Vec_IntPush( vData, 15 );
260  Vec_IntPush( vData, 3 );
261  Vec_IntPush( vData, 16 );
262  Vec_IntPush( vData, 16 );
263  Vec_IntPush( vData, 12 );
264  Vec_IntPush( vData, 17 );
265  Vec_IntPush( vData, 12 );
266  Vec_IntPush( vData, 12 );
267 
268  vRes = Hsh_IntManHashArray( vData, 2 );
269 
270  Vec_IntPrint( vData );
271  Vec_IntPrint( vRes );
272 
273  Vec_IntFree( vData );
274  Vec_IntFree( vRes );
275 }
276 
277 
278 
279 
280 
281 /**Function*************************************************************
282 
283  Synopsis [Hashing integer arrays.]
284 
285  Description []
286 
287  SideEffects []
288 
289  SeeAlso []
290 
291 ***********************************************************************/
292 static inline Hsh_VecMan_t * Hsh_VecManStart( int nEntries )
293 {
294  Hsh_VecMan_t * p;
295  p = ABC_CALLOC( Hsh_VecMan_t, 1 );
296  p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nEntries) );
297  p->vData = Vec_IntAlloc( nEntries * 4 );
298  p->vMap = Vec_IntAlloc( nEntries );
299  return p;
300 }
301 static inline void Hsh_VecManStop( Hsh_VecMan_t * p )
302 {
303  Vec_IntFree( p->vTable );
304  Vec_IntFree( p->vData );
305  Vec_IntFree( p->vMap );
306  ABC_FREE( p );
307 }
308 static inline Vec_Int_t * Hsh_VecReadEntry( Hsh_VecMan_t * p, int i )
309 {
310  Hsh_VecObj_t * pObj = Hsh_VecObj( p, i );
311  p->vTemp.nSize = p->vTemp.nCap = pObj->nSize;
312  p->vTemp.pArray = (int*)pObj + 2;
313  return &p->vTemp;
314 }
315 static inline int Hsh_VecSize( Hsh_VecMan_t * p )
316 {
317  return Vec_IntSize(p->vMap);
318 }
319 
320 /**Function*************************************************************
321 
322  Synopsis []
323 
324  Description []
325 
326  SideEffects []
327 
328  SeeAlso []
329 
330 ***********************************************************************/
331 static inline int Hsh_VecManHash( Vec_Int_t * vVec, int nTableSize )
332 {
333  static unsigned s_Primes[7] = {4177, 5147, 5647, 6343, 7103, 7873, 8147};
334  unsigned Key = 0;
335  int i, Entry;
336  Vec_IntForEachEntry( vVec, Entry, i )
337  Key += (unsigned)Entry * s_Primes[i % 7];
338  return (int)(Key % nTableSize);
339 }
340 static inline int Hsh_VecManAdd( Hsh_VecMan_t * p, Vec_Int_t * vVec )
341 {
342  Hsh_VecObj_t * pObj;
343  int i, Ent, * pPlace;
344  if ( Vec_IntSize(p->vMap) > Vec_IntSize(p->vTable) )
345  {
347  for ( i = 0; i < Vec_IntSize(p->vMap); i++ )
348  {
350  Hsh_VecObj(p, i)->iNext = *pPlace; *pPlace = i;
351  }
352  }
353  pPlace = Vec_IntEntryP( p->vTable, Hsh_VecManHash(vVec, Vec_IntSize(p->vTable)) );
354  for ( ; (pObj = Hsh_VecObj(p, *pPlace)); pPlace = &pObj->iNext )
355  if ( pObj->nSize == Vec_IntSize(vVec) && !memcmp( pObj->pArray, Vec_IntArray(vVec), sizeof(int) * pObj->nSize ) )
356  return *pPlace;
357  *pPlace = Vec_IntSize(p->vMap);
358  assert( Vec_IntSize(p->vData) % 2 == 0 );
359  Vec_IntPush( p->vMap, Vec_IntSize(p->vData) );
360  Vec_IntPush( p->vData, Vec_IntSize(vVec) );
361  Vec_IntPush( p->vData, -1 );
362  Vec_IntForEachEntry( vVec, Ent, i )
363  Vec_IntPush( p->vData, Ent );
364  if ( Vec_IntSize(vVec) & 1 )
365  Vec_IntPush( p->vData, -1 );
366  return Vec_IntSize(p->vMap) - 1;
367 }
368 
369 /**Function*************************************************************
370 
371  Synopsis [Test procedure.]
372 
373  Description []
374 
375  SideEffects []
376 
377  SeeAlso []
378 
379 ***********************************************************************/
380 static inline void Hsh_VecManHashTest()
381 {
382  Hsh_VecMan_t * p;
383  Vec_Int_t * vTemp;
384  Vec_Int_t * vRes = Vec_IntAlloc( 1000 );
385  int i;
386 
387  p = Hsh_VecManStart( 5 );
388  for ( i = 0; i < 20; i++ )
389  {
390  vTemp = Vec_IntStartNatural( i );
391  Vec_IntPush( vRes, Hsh_VecManAdd( p, vTemp ) );
392  Vec_IntFree( vTemp );
393  }
394  for ( ; i > 0; i-- )
395  {
396  vTemp = Vec_IntStartNatural( i );
397  Vec_IntPush( vRes, Hsh_VecManAdd( p, vTemp ) );
398  Vec_IntFree( vTemp );
399  }
400  Vec_IntPrint( vRes );
401  Vec_IntFree( vRes );
402 
403  Hsh_VecManStop( p );
404 }
405 
406 
407 ////////////////////////////////////////////////////////////////////////
408 /// END OF FILE ///
409 ////////////////////////////////////////////////////////////////////////
410 
412 
413 #endif
414 
static int Hsh_VecManAdd(Hsh_VecMan_t *p, Vec_Int_t *vVec)
Definition: vecHsh.h:340
static int * Vec_IntArray(Vec_Int_t *p)
Definition: vecInt.h:328
static int Abc_PrimeCudd(unsigned int p)
Definition: abc_global.h:383
static Llb_Mgr_t * p
Definition: llb3Image.c:950
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition: bblif.c:37
static void Vec_WrdPush(Vec_Wrd_t *p, word Entry)
Definition: vecWrd.h:618
static int Vec_WrdCap(Vec_Wrd_t *p)
Definition: vecWrd.h:352
Vec_Int_t vTemp
Definition: vecHsh.h:81
int iData
Definition: vecHsh.h:45
char * memcpy()
static Vec_Int_t * Vec_IntStartNatural(int nSize)
Definition: bblif.c:192
static int Vec_WrdSize(Vec_Wrd_t *p)
Definition: vecWrd.h:336
static Vec_Int_t * Vec_IntStartFull(int nSize)
Definition: vecInt.h:119
static Vec_Int_t * Hsh_IntManHashArray(Vec_Int_t *vData, int nSize)
Definition: vecHsh.h:191
static Vec_Int_t * Hsh_WrdManHashArray(Vec_Wrd_t *vDataW, int nSize)
Definition: vecHsh.h:203
static void Hsh_VecManStop(Hsh_VecMan_t *p)
Definition: vecHsh.h:301
typedefABC_NAMESPACE_HEADER_START struct Hsh_IntObj_t_ Hsh_IntObj_t
INCLUDES ///.
Definition: vecHsh.h:42
static Hsh_IntMan_t * Hsh_WrdManHashArrayStart(Vec_Wrd_t *vDataW, int nSize)
Definition: vecHsh.h:217
static int Hsh_VecManHash(Vec_Int_t *vVec, int nTableSize)
Definition: vecHsh.h:331
Vec_Int_t * vMap
Definition: vecHsh.h:80
Vec_Int_t * vData
Definition: vecHsh.h:60
static int s_Primes[MAX_PRIMES]
Definition: fxuPair.c:30
static Vec_Int_t * Vec_IntAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: bblif.c:149
static int * Hsh_IntManLookup(Hsh_IntMan_t *p, unsigned *pData)
Definition: vecHsh.h:147
static word Hsh_IntWord(int iData, int iNext)
Definition: vecHsh.h:90
int pArray[0]
Definition: vecHsh.h:72
static int Vec_IntEntry(Vec_Int_t *p, int i)
Definition: bblif.c:268
unsigned __int64 word
DECLARATIONS ///.
Definition: kitPerm.c:36
static void Hsh_IntManHashArrayTest()
Definition: vecHsh.h:251
static void Vec_WrdFree(Vec_Wrd_t *p)
Definition: vecWrd.h:260
static void Vec_IntFill(Vec_Int_t *p, int nSize, int Fill)
Definition: bblif.c:356
int memcmp()
static Hsh_VecObj_t * Hsh_VecObj(Hsh_VecMan_t *p, int i)
Definition: vecHsh.h:92
Hsh_IntObj_t wObj
Definition: vecHsh.h:52
static void Vec_IntPush(Vec_Int_t *p, int Entry)
Definition: bblif.c:468
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Definition: abc_global.h:105
Vec_Wrd_t * vObjs
Definition: vecHsh.h:62
static void Hsh_IntManStop(Hsh_IntMan_t *p)
Definition: vecHsh.h:119
Vec_Int_t * vData
Definition: vecHsh.h:79
#define ABC_NAMESPACE_HEADER_END
Definition: abc_global.h:106
static Hsh_VecMan_t * Hsh_VecManStart(int nEntries)
Definition: vecHsh.h:292
static unsigned * Hsh_IntData(Hsh_IntMan_t *p, int iData)
MACRO DEFINITIONS ///.
Definition: vecHsh.h:88
static Vec_Wrd_t * Vec_WrdAlloc(int nCap)
FUNCTION DEFINITIONS ///.
Definition: vecWrd.h:80
static int Hsh_IntManAdd(Hsh_IntMan_t *p, int iData)
Definition: vecHsh.h:157
Vec_Int_t * vTable
Definition: vecHsh.h:61
int nSize
Definition: vecHsh.h:59
int iNext
Definition: vecHsh.h:71
static int Hsh_VecSize(Hsh_VecMan_t *p)
Definition: vecHsh.h:315
static Hsh_IntObj_t * Hsh_IntObj(Hsh_IntMan_t *p, int iObj)
Definition: vecHsh.h:89
static int Vec_IntSize(Vec_Int_t *p)
Definition: bblif.c:252
int nSize
Definition: vecHsh.h:70
Vec_Int_t * vTable
Definition: vecHsh.h:78
static word * Vec_WrdArray(Vec_Wrd_t *p)
Definition: vecWrd.h:316
#define ABC_FREE(obj)
Definition: abc_global.h:232
static void Hsh_VecManHashTest()
Definition: vecHsh.h:380
#define ABC_CALLOC(type, num)
Definition: abc_global.h:230
static int Hsh_IntManHash(unsigned *pData, int nSize, int nTableSize)
Definition: vecHsh.h:137
#define assert(ex)
Definition: util_old.h:213
static int * Vec_IntEntryP(Vec_Int_t *p, int i)
Definition: vecInt.h:417
static Vec_Int_t * Hsh_VecReadEntry(Hsh_VecMan_t *p, int i)
Definition: vecHsh.h:308
static void Vec_IntPrint(Vec_Int_t *vVec)
Definition: vecInt.h:1803
static void Vec_IntFree(Vec_Int_t *p)
Definition: bblif.c:235
static Hsh_IntMan_t * Hsh_IntManStart(Vec_Int_t *vData, int nSize, int nEntries)
FUNCTION DEFINITIONS ///.
Definition: vecHsh.h:109
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition: vecInt.h:54
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.
Definition: vecWrd.h:42
int iNext
Definition: vecHsh.h:46
static word * Vec_WrdEntryP(Vec_Wrd_t *p, int i)
Definition: vecWrd.h:401