ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
vecHsh.h
Go to the documentation of this file.
1
20
21#ifndef ABC__misc__vec__vecHsh_h
22#define ABC__misc__vec__vecHsh_h
23
24
28
29#include <stdio.h>
30
32
33
37
41
44{
45 Vec_Int_t * vData; // stored data
46 Vec_Int_t * vNext; // next items
47 Vec_Int_t * vTable; // hash table
48};
49
50
51
54{
55 int iData;
56 int iNext;
57};
58
65
68{
69 int nSize; // data size
70 Vec_Int_t * vData; // data storage
71 Vec_Int_t * vTable; // hash table
72 Vec_Wrd_t * vObjs; // hash objects
73};
74
75
76
79{
80 int nSize;
81 int iNext;
82 int pArray[0];
83};
84
87{
88 Vec_Int_t * vTable; // hash table
89 Vec_Int_t * vData; // data storage
90 Vec_Int_t * vMap; // mapping entries into data
91 Vec_Int_t * vValue; // mapping entries into values
92 Vec_Int_t * vEntry; // temporary entry
93 Vec_Int_t vTemp; // temporary array
94 Vec_Int_t vTemp1; // temporary array
95 Vec_Int_t vTemp2; // temporary array
96};
97
101
102static inline unsigned * Hsh_IntData( Hsh_IntMan_t * p, int iData ) { return (unsigned *)Vec_IntEntryP( p->vData, p->nSize * iData ); }
103static inline Hsh_IntObj_t * Hsh_IntObj( Hsh_IntMan_t * p, int iObj ) { return iObj == -1 ? NULL : (Hsh_IntObj_t *)Vec_WrdEntryP( p->vObjs, iObj ); }
104static inline word Hsh_IntWord( int iData, int iNext ) { Hsh_IntObjWord_t Obj = { {iData, iNext} }; return Obj.wWord; }
105
106static 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)); }
107
111
123static inline Hsh_Int1Man_t * Hsh_Int1ManStart( int nEntries )
124{
126 p = ABC_CALLOC( Hsh_Int1Man_t, 1 );
127 p->vData = Vec_IntAlloc( nEntries );
128 p->vNext = Vec_IntAlloc( nEntries );
129 p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nEntries) );
130 return p;
131}
132static inline void Hsh_Int1ManStop( Hsh_Int1Man_t * p )
133{
134 Vec_IntFree( p->vData );
135 Vec_IntFree( p->vNext );
136 Vec_IntFree( p->vTable );
137 ABC_FREE( p );
138}
139static inline int Hsh_Int1ManEntryNum( Hsh_Int1Man_t * p )
140{
141 return Vec_IntSize(p->vData);
142}
143
144
156// https://en.wikipedia.org/wiki/Jenkins_hash_function
157static inline int Hsh_Int1ManHash( int Data, int nTableSize )
158{
159 unsigned i, hash = 0;
160 unsigned char * pDataC = (unsigned char *)&Data;
161 for ( i = 0; i < 4; i++ )
162 {
163 hash += pDataC[i];
164 hash += hash << 10;
165 hash ^= hash >> 6;
166 }
167 hash += hash << 3;
168 hash ^= hash >> 11;
169 hash += hash << 15;
170 return (int)(hash % nTableSize);
171}
172static inline int * Hsh_Int1ManLookupInt( Hsh_Int1Man_t * p, int Data )
173{
174 int Item, * pPlace = Vec_IntEntryP( p->vTable, Hsh_Int1ManHash(Data, Vec_IntSize(p->vTable)) );
175 for ( Item = *pPlace; Item >= 0; Item = Vec_IntEntry(p->vNext, Item) )
176 if ( Vec_IntEntry(p->vData, Item) == (int)Data )
177 return pPlace;
178 assert( Item == -1 );
179 return pPlace;
180}
181static inline int Hsh_Int1ManLookup( Hsh_Int1Man_t * p, int Data )
182{
183 return *Hsh_Int1ManLookupInt(p, Data);
184}
185static inline int Hsh_Int1ManAdd( Hsh_Int1Man_t * p, int Data )
186{
187 int i, Entry, * pPlace;
188 if ( Vec_IntSize(p->vData) > Vec_IntSize(p->vTable) )
189 {
190 Vec_IntFill( p->vTable, Abc_PrimeCudd(2*Vec_IntSize(p->vTable)), -1 );
191 Vec_IntForEachEntry( p->vData, Entry, i )
192 {
193 pPlace = Vec_IntEntryP( p->vTable, Hsh_Int1ManHash(Entry, Vec_IntSize(p->vTable)) );
194 Vec_IntWriteEntry( p->vNext, i, *pPlace ); *pPlace = i;
195 }
196 }
197 pPlace = Hsh_Int1ManLookupInt( p, Data );
198 if ( *pPlace == -1 )
199 {
200 *pPlace = Vec_IntSize(p->vData);
201 Vec_IntPush( p->vData, Data );
202 Vec_IntPush( p->vNext, -1 );
203 }
204 return *pPlace;
205}
206
218static inline void Hsh_Int1ManHashArrayTest()
219{
220 Hsh_Int1Man_t * p = Hsh_Int1ManStart( 10 );
221
222 printf( "Entry = %d. Hashed = %d.\n", 5, Hsh_Int1ManAdd(p, 5 ) );
223 printf( "Entry = %d. Hashed = %d.\n", 7, Hsh_Int1ManAdd(p, 7 ) );
224 printf( "Entry = %d. Hashed = %d.\n", 7, Hsh_Int1ManAdd(p, 7 ) );
225 printf( "Entry = %d. Hashed = %d.\n", 7, Hsh_Int1ManAdd(p, 7 ) );
226 printf( "Entry = %d. Hashed = %d.\n", 8, Hsh_Int1ManAdd(p, 8 ) );
227 printf( "Entry = %d. Hashed = %d.\n", 5, Hsh_Int1ManAdd(p, 5 ) );
228 printf( "Entry = %d. Hashed = %d.\n", 3, Hsh_Int1ManAdd(p, 3 ) );
229 printf( "Entry = %d. Hashed = %d.\n", 4, Hsh_Int1ManAdd(p, 4 ) );
230
231 printf( "Size = %d.\n", Hsh_Int1ManEntryNum(p) );
232
233 Hsh_Int1ManStop( p );
234}
235
236
248static inline Hsh_IntMan_t * Hsh_IntManStart( Vec_Int_t * vData, int nSize, int nEntries )
249{
250 Hsh_IntMan_t * p;
251 p = ABC_CALLOC( Hsh_IntMan_t, 1 );
252 p->nSize = nSize;
253 p->vData = vData;
254 p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nEntries) );
255 p->vObjs = Vec_WrdAlloc( nEntries );
256 return p;
257}
258static inline void Hsh_IntManStop( Hsh_IntMan_t * p )
259{
260 Vec_IntFree( p->vTable );
261 Vec_WrdFree( p->vObjs );
262 ABC_FREE( p );
263}
264static inline int Hsh_IntManEntryNum( Hsh_IntMan_t * p )
265{
266 return Vec_WrdSize(p->vObjs);
267}
268
280// https://en.wikipedia.org/wiki/Jenkins_hash_function
281static inline int Hsh_IntManHash( unsigned * pData, int nSize, int nTableSize )
282{
283 int i = 0; unsigned hash = 0;
284 unsigned char * pDataC = (unsigned char *)pData;
285 nSize <<= 2;
286 while ( i != nSize )
287 {
288 hash += pDataC[i++];
289 hash += hash << 10;
290 hash ^= hash >> 6;
291 }
292 hash += hash << 3;
293 hash ^= hash >> 11;
294 hash += hash << 15;
295 return (int)(hash % nTableSize);
296}
297static inline int Hsh_IntManHash2( unsigned * pData, int nSize, int nTableSize )
298{
299 static int s_Primes[7] = { 4177, 5147, 5647, 6343, 7103, 7873, 8147 };
300 unsigned char * pDataC = (unsigned char *)pData;
301 int c, nChars = nSize * 4;
302 unsigned Key = 0;
303 for ( c = 0; c < nChars; c++ )
304 Key += pDataC[c] * s_Primes[c % 7];
305 return (int)(Key % nTableSize);
306}
307static inline int * Hsh_IntManLookup( Hsh_IntMan_t * p, unsigned * pData )
308{
309 Hsh_IntObj_t * pObj;
310 int * pPlace = Vec_IntEntryP( p->vTable, Hsh_IntManHash(pData, p->nSize, Vec_IntSize(p->vTable)) );
311 for ( ; (pObj = Hsh_IntObj(p, *pPlace)); pPlace = &pObj->iNext )
312 if ( !memcmp( pData, Hsh_IntData(p, pObj->iData), sizeof(int) * (size_t)p->nSize ) )
313 return pPlace;
314 assert( *pPlace == -1 );
315 return pPlace;
316}
317static inline int Hsh_IntManAdd( Hsh_IntMan_t * p, int iData )
318{
319 int i, * pPlace;
320 if ( Vec_WrdSize(p->vObjs) > Vec_IntSize(p->vTable) )
321 {
322 Vec_IntFill( p->vTable, Abc_PrimeCudd(2*Vec_IntSize(p->vTable)), -1 );
323 for ( i = 0; i < Vec_WrdSize(p->vObjs); i++ )
324 {
325 pPlace = Vec_IntEntryP( p->vTable, Hsh_IntManHash(Hsh_IntData(p, Hsh_IntObj(p, i)->iData), p->nSize, Vec_IntSize(p->vTable)) );
326 Hsh_IntObj(p, i)->iNext = *pPlace; *pPlace = i;
327 }
328 }
329 pPlace = Hsh_IntManLookup( p, Hsh_IntData(p, iData) );
330 if ( *pPlace == -1 )
331 {
332 *pPlace = Vec_WrdSize(p->vObjs);
333 Vec_WrdPush( p->vObjs, Hsh_IntWord(iData, -1) );
334 return Vec_WrdSize(p->vObjs) - 1;
335 }
336 return (word *)Hsh_IntObj(p, *pPlace) - Vec_WrdArray(p->vObjs);
337}
338
351static inline Vec_Int_t * Hsh_IntManHashArray( Vec_Int_t * vData, int nSize )
352{
353 Hsh_IntMan_t * p;
354 Vec_Int_t * vRes = Vec_IntAlloc( 100 );
355 int i, nEntries = Vec_IntSize(vData) / nSize;
356 assert( Vec_IntSize(vData) % nSize == 0 );
357 p = Hsh_IntManStart( vData, nSize, nEntries );
358 for ( i = 0; i < nEntries; i++ )
359 Vec_IntPush( vRes, Hsh_IntManAdd(p, i) );
360 Hsh_IntManStop( p );
361 return vRes;
362}
363static inline Vec_Int_t * Hsh_WrdManHashArray( Vec_Wrd_t * vDataW, int nSize )
364{
365 Hsh_IntMan_t * p;
366 Vec_Int_t Data = { 2*Vec_WrdCap(vDataW), 2*Vec_WrdSize(vDataW), (int *)Vec_WrdArray(vDataW) };
367 Vec_Int_t * vData = &Data;
368 Vec_Int_t * vRes = Vec_IntAlloc( 100 );
369 int i, nEntries = Vec_IntSize(vData) / (2*nSize);
370 assert( Vec_IntSize(vData) % (2*nSize) == 0 );
371 p = Hsh_IntManStart( vData, (2*nSize), nEntries );
372 for ( i = 0; i < nEntries; i++ )
373 Vec_IntPush( vRes, Hsh_IntManAdd(p, i) );
374 Hsh_IntManStop( p );
375 return vRes;
376}
377static inline Hsh_IntMan_t * Hsh_WrdManHashArrayStart( Vec_Wrd_t * vDataW, int nSize )
378{
379 Hsh_IntMan_t * p;
380 int i, nEntries = Vec_WrdSize(vDataW) / nSize;
381 Vec_Int_t * vData = Vec_IntAlloc( 2*Vec_WrdSize(vDataW) );
382 memcpy( Vec_IntArray(vData), Vec_WrdArray(vDataW), sizeof(word)*(size_t)Vec_WrdSize(vDataW) );
383 vData->nSize = 2*Vec_WrdSize(vDataW);
384/*
385 for ( i = 0; i < 30; i++ )
386 {
387 extern void Extra_PrintHex( FILE * pFile, unsigned * pTruth, int nVars );
388 Extra_PrintHex( stdout, (unsigned *) Vec_WrdEntryP(vDataW, i), 6 ); printf( " " );
389 Kit_DsdPrintFromTruth( (unsigned *) Vec_WrdEntryP(vDataW, i), 6 ); printf( "\n" );
390 }
391*/
392 assert( Vec_IntSize(vData) % (2*nSize) == 0 );
393 p = Hsh_IntManStart( vData, (2*nSize), nEntries );
394 for ( i = 0; i < nEntries; i++ )
395 Hsh_IntManAdd( p, i );
396 assert( Vec_WrdSize(p->vObjs) == nEntries );
397 return p;
398}
399
411static inline void Hsh_IntManHashArrayTest()
412{
413 Vec_Int_t * vData = Vec_IntAlloc( 10 );
414 Vec_Int_t * vRes;
415 Vec_IntPush( vData, 12 );
416 Vec_IntPush( vData, 17 );
417 Vec_IntPush( vData, 13 );
418 Vec_IntPush( vData, 12 );
419 Vec_IntPush( vData, 15 );
420 Vec_IntPush( vData, 3 );
421 Vec_IntPush( vData, 16 );
422 Vec_IntPush( vData, 16 );
423 Vec_IntPush( vData, 12 );
424 Vec_IntPush( vData, 17 );
425 Vec_IntPush( vData, 12 );
426 Vec_IntPush( vData, 12 );
427
428 vRes = Hsh_IntManHashArray( vData, 2 );
429
430 Vec_IntPrint( vData );
431 Vec_IntPrint( vRes );
432
433 Vec_IntFree( vData );
434 Vec_IntFree( vRes );
435}
436
437
438
439
440
452static inline Hsh_VecMan_t * Hsh_VecManStart( int nEntries )
453{
454 Hsh_VecMan_t * p;
455 p = ABC_CALLOC( Hsh_VecMan_t, 1 );
456 p->vTable = Vec_IntStartFull( Abc_PrimeCudd(nEntries) );
457 p->vData = Vec_IntAlloc( nEntries * 4 );
458 p->vMap = Vec_IntAlloc( nEntries );
459 return p;
460}
461static inline void Hsh_VecManStop( Hsh_VecMan_t * p )
462{
463 Vec_IntFree( p->vTable );
464 Vec_IntFree( p->vData );
465 Vec_IntFree( p->vMap );
466 Vec_IntFreeP( &p->vValue );
467 Vec_IntFreeP( &p->vEntry );
468 ABC_FREE( p );
469}
470static inline int * Hsh_VecReadArray( Hsh_VecMan_t * p, int i )
471{
472 return (int*)Hsh_VecObj(p, i) + 2;
473}
474static inline Vec_Int_t * Hsh_VecReadEntry( Hsh_VecMan_t * p, int i )
475{
476 Hsh_VecObj_t * pObj = Hsh_VecObj( p, i );
477 p->vTemp.nSize = p->vTemp.nCap = pObj->nSize;
478 p->vTemp.pArray = (int*)pObj + 2;
479 return &p->vTemp;
480}
481static inline Vec_Int_t * Hsh_VecReadEntry1( Hsh_VecMan_t * p, int i )
482{
483 Hsh_VecObj_t * pObj = Hsh_VecObj( p, i );
484 p->vTemp1.nSize = p->vTemp1.nCap = pObj->nSize;
485 p->vTemp1.pArray = (int*)pObj + 2;
486 return &p->vTemp1;
487}
488static inline Vec_Int_t * Hsh_VecReadEntry2( Hsh_VecMan_t * p, int i )
489{
490 Hsh_VecObj_t * pObj = Hsh_VecObj( p, i );
491 p->vTemp2.nSize = p->vTemp2.nCap = pObj->nSize;
492 p->vTemp2.pArray = (int*)pObj + 2;
493 return &p->vTemp2;
494}
495static inline int Hsh_VecSize( Hsh_VecMan_t * p )
496{
497 return Vec_IntSize(p->vMap);
498}
499static inline double Hsh_VecManMemory( Hsh_VecMan_t * p )
500{
501 return !p ? 0.0 : Vec_IntMemory(p->vTable) + Vec_IntMemory(p->vData) + Vec_IntMemory(p->vMap);
502}
503
515static inline int Hsh_VecManHash( Vec_Int_t * vVec, int nTableSize )
516{
517 static unsigned s_Primes[7] = {4177, 5147, 5647, 6343, 7103, 7873, 8147};
518 unsigned Key = 0;
519 int i, Entry;
520 Vec_IntForEachEntry( vVec, Entry, i )
521 Key += (unsigned)Entry * s_Primes[i % 7];
522 return (int)(Key % nTableSize);
523}
524static inline int Hsh_VecManAdd( Hsh_VecMan_t * p, Vec_Int_t * vVec )
525{
526 Hsh_VecObj_t * pObj;
527 int i, Ent, * pPlace;
528 if ( Vec_IntSize(p->vMap) > Vec_IntSize(p->vTable) )
529 {
530 Vec_IntFill( p->vTable, Abc_PrimeCudd(2*Vec_IntSize(p->vTable)), -1 );
531 for ( i = 0; i < Vec_IntSize(p->vMap); i++ )
532 {
533 pPlace = Vec_IntEntryP( p->vTable, Hsh_VecManHash(Hsh_VecReadEntry(p, i), Vec_IntSize(p->vTable)) );
534 Hsh_VecObj(p, i)->iNext = *pPlace; *pPlace = i;
535 }
536 }
537 pPlace = Vec_IntEntryP( p->vTable, Hsh_VecManHash(vVec, Vec_IntSize(p->vTable)) );
538 for ( ; (pObj = Hsh_VecObj(p, *pPlace)); pPlace = &pObj->iNext )
539 if ( pObj->nSize == Vec_IntSize(vVec) && !memcmp( pObj->pArray, Vec_IntArray(vVec), sizeof(int) * (size_t)pObj->nSize ) )
540 return *pPlace;
541 *pPlace = Vec_IntSize(p->vMap);
542 assert( Vec_IntSize(p->vData) % 2 == 0 );
543 Vec_IntPush( p->vMap, Vec_IntSize(p->vData) );
544 Vec_IntPush( p->vData, Vec_IntSize(vVec) );
545 Vec_IntPush( p->vData, -1 );
546 Vec_IntForEachEntry( vVec, Ent, i )
547 Vec_IntPush( p->vData, Ent );
548 if ( Vec_IntSize(vVec) & 1 )
549 Vec_IntPush( p->vData, -1 );
550 return Vec_IntSize(p->vMap) - 1;
551}
552
564static inline void Hsh_VecManHashTest()
565{
566 Hsh_VecMan_t * p;
567 Vec_Int_t * vTemp;
568 Vec_Int_t * vRes = Vec_IntAlloc( 1000 );
569 int i;
570
571 p = Hsh_VecManStart( 5 );
572 for ( i = 0; i < 20; i++ )
573 {
574 vTemp = Vec_IntStartNatural( i );
575 Vec_IntPush( vRes, Hsh_VecManAdd( p, vTemp ) );
576 Vec_IntFree( vTemp );
577 }
578 for ( ; i > 0; i-- )
579 {
580 vTemp = Vec_IntStartNatural( i );
581 Vec_IntPush( vRes, Hsh_VecManAdd( p, vTemp ) );
582 Vec_IntFree( vTemp );
583 }
584 Vec_IntPrint( vRes );
585 Vec_IntFree( vRes );
586
587 Hsh_VecManStop( p );
588}
589
590
594
596
597#endif
598
#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 ///.
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
Vec_Int_t * vNext
Definition vecHsh.h:46
Vec_Int_t * vData
Definition vecHsh.h:45
Vec_Int_t * vTable
Definition vecHsh.h:47
Vec_Int_t * vTable
Definition vecHsh.h:71
Vec_Wrd_t * vObjs
Definition vecHsh.h:72
Vec_Int_t * vData
Definition vecHsh.h:70
Vec_Int_t * vEntry
Definition vecHsh.h:92
Vec_Int_t * vValue
Definition vecHsh.h:91
Vec_Int_t vTemp
Definition vecHsh.h:93
Vec_Int_t vTemp1
Definition vecHsh.h:94
Vec_Int_t vTemp2
Definition vecHsh.h:95
Vec_Int_t * vData
Definition vecHsh.h:89
Vec_Int_t * vTable
Definition vecHsh.h:88
Vec_Int_t * vMap
Definition vecHsh.h:90
int pArray[0]
Definition vecHsh.h:82
Hsh_IntObj_t wObj
Definition vecHsh.h:62
#define assert(ex)
Definition util_old.h:213
char * memcpy()
int memcmp()
struct Hsh_IntMan_t_ Hsh_IntMan_t
Definition vecHsh.h:66
struct Hsh_IntObj_t_ Hsh_IntObj_t
Definition vecHsh.h:52
struct Hsh_VecMan_t_ Hsh_VecMan_t
Definition vecHsh.h:85
union Hsh_IntObjWord_t_ Hsh_IntObjWord_t
Definition vecHsh.h:59
typedefABC_NAMESPACE_HEADER_START struct Hsh_Int1Man_t_ Hsh_Int1Man_t
INCLUDES ///.
Definition vecHsh.h:42
struct Hsh_VecObj_t_ Hsh_VecObj_t
Definition vecHsh.h:77
#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