ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
fxuHeapD.c
Go to the documentation of this file.
1
18
19#include "fxuInt.h"
20
22
23
27
28#define FXU_HEAP_DOUBLE_WEIGHT(pDiv) ((pDiv)->Weight)
29#define FXU_HEAP_DOUBLE_CURRENT(p, pDiv) ((p)->pTree[(pDiv)->HNum])
30#define FXU_HEAP_DOUBLE_PARENT_EXISTS(p, pDiv) ((pDiv)->HNum > 1)
31#define FXU_HEAP_DOUBLE_CHILD1_EXISTS(p, pDiv) (((pDiv)->HNum << 1) <= p->nItems)
32#define FXU_HEAP_DOUBLE_CHILD2_EXISTS(p, pDiv) ((((pDiv)->HNum << 1)+1) <= p->nItems)
33#define FXU_HEAP_DOUBLE_PARENT(p, pDiv) ((p)->pTree[(pDiv)->HNum >> 1])
34#define FXU_HEAP_DOUBLE_CHILD1(p, pDiv) ((p)->pTree[(pDiv)->HNum << 1])
35#define FXU_HEAP_DOUBLE_CHILD2(p, pDiv) ((p)->pTree[((pDiv)->HNum << 1)+1])
36#define FXU_HEAP_DOUBLE_ASSERT(p, pDiv) assert( (pDiv)->HNum >= 1 && (pDiv)->HNum <= p->nItemsAlloc )
37
38static void Fxu_HeapDoubleResize( Fxu_HeapDouble * p );
39static void Fxu_HeapDoubleSwap( Fxu_Double ** pDiv1, Fxu_Double ** pDiv2 );
40static void Fxu_HeapDoubleMoveUp( Fxu_HeapDouble * p, Fxu_Double * pDiv );
41static void Fxu_HeapDoubleMoveDn( Fxu_HeapDouble * p, Fxu_Double * pDiv );
42
46
59{
62 memset( p, 0, sizeof(Fxu_HeapDouble) );
63 p->nItems = 0;
64 p->nItemsAlloc = 10000;
65 p->pTree = ABC_ALLOC( Fxu_Double *, p->nItemsAlloc + 1 );
66 p->pTree[0] = NULL;
67 return p;
68}
69
70
82void Fxu_HeapDoubleResize( Fxu_HeapDouble * p )
83{
84 p->nItemsAlloc *= 2;
85 p->pTree = ABC_REALLOC( Fxu_Double *, p->pTree, p->nItemsAlloc + 1 );
86}
87
100{
101 ABC_FREE( p->pTree );
102 ABC_FREE( p );
103}
104
105
117void Fxu_HeapDoublePrint( FILE * pFile, Fxu_HeapDouble * p )
118{
119 Fxu_Double * pDiv;
120 int Counter = 1;
121 int Degree = 1;
122
124 fprintf( pFile, "The contents of the heap:\n" );
125 fprintf( pFile, "Level %d: ", Degree );
127 {
128 assert( Counter == p->pTree[Counter]->HNum );
129 fprintf( pFile, "%2d=%3d ", Counter, FXU_HEAP_DOUBLE_WEIGHT(p->pTree[Counter]) );
130 if ( ++Counter == (1 << Degree) )
131 {
132 fprintf( pFile, "\n" );
133 Degree++;
134 fprintf( pFile, "Level %d: ", Degree );
135 }
136 }
137 fprintf( pFile, "\n" );
138 fprintf( pFile, "End of the heap printout.\n" );
139}
140
153{
154 Fxu_Double * pDiv;
156 {
157 assert( pDiv->HNum == p->i );
158 Fxu_HeapDoubleCheckOne( p, pDiv );
159 }
160}
161
174{
175 int Weight1, Weight2;
177 {
178 Weight1 = FXU_HEAP_DOUBLE_WEIGHT(pDiv);
180 assert( Weight1 >= Weight2 );
181 }
183 {
184 Weight1 = FXU_HEAP_DOUBLE_WEIGHT(pDiv);
186 assert( Weight1 >= Weight2 );
187 }
188}
189
202{
203 if ( p->nItems == p->nItemsAlloc )
204 Fxu_HeapDoubleResize( p );
205 // put the last entry to the last place and move up
206 p->pTree[++p->nItems] = pDiv;
207 pDiv->HNum = p->nItems;
208 // move the last entry up if necessary
209 Fxu_HeapDoubleMoveUp( p, pDiv );
210}
211
224{
225//printf( "Updating divisor %d.\n", pDiv->Num );
226
228 if ( FXU_HEAP_DOUBLE_PARENT_EXISTS(p,pDiv) &&
230 Fxu_HeapDoubleMoveUp( p, pDiv );
231 else if ( FXU_HEAP_DOUBLE_CHILD1_EXISTS(p,pDiv) &&
233 Fxu_HeapDoubleMoveDn( p, pDiv );
234 else if ( FXU_HEAP_DOUBLE_CHILD2_EXISTS(p,pDiv) &&
236 Fxu_HeapDoubleMoveDn( p, pDiv );
237}
238
251{
253 // put the last entry to the deleted place
254 // decrement the number of entries
255 p->pTree[pDiv->HNum] = p->pTree[p->nItems--];
256 p->pTree[pDiv->HNum]->HNum = pDiv->HNum;
257 // move the top entry down if necessary
258 Fxu_HeapDoubleUpdate( p, p->pTree[pDiv->HNum] );
259 pDiv->HNum = 0;
260}
261
274{
275 if ( p->nItems == 0 )
276 return NULL;
277 return p->pTree[1];
278}
279
292{
293 Fxu_Double * pDiv;
294 if ( p->nItems == 0 )
295 return NULL;
296 // prepare the return value
297 pDiv = p->pTree[1];
298 pDiv->HNum = 0;
299 // put the last entry on top
300 // decrement the number of entries
301 p->pTree[1] = p->pTree[p->nItems--];
302 p->pTree[1]->HNum = 1;
303 // move the top entry down if necessary
304 Fxu_HeapDoubleMoveDn( p, p->pTree[1] );
305 return pDiv;
306}
307
320{
321 if ( p->nItems == 0 )
322 return -1;
323 else
324 return FXU_HEAP_DOUBLE_WEIGHT(p->pTree[1]);
325}
326
327
328
340void Fxu_HeapDoubleSwap( Fxu_Double ** pDiv1, Fxu_Double ** pDiv2 )
341{
342 Fxu_Double * pDiv;
343 int Temp;
344 pDiv = *pDiv1;
345 *pDiv1 = *pDiv2;
346 *pDiv2 = pDiv;
347 Temp = (*pDiv1)->HNum;
348 (*pDiv1)->HNum = (*pDiv2)->HNum;
349 (*pDiv2)->HNum = Temp;
350}
351
363void Fxu_HeapDoubleMoveUp( Fxu_HeapDouble * p, Fxu_Double * pDiv )
364{
365 Fxu_Double ** ppDiv, ** ppPar;
366 ppDiv = &FXU_HEAP_DOUBLE_CURRENT(p, pDiv);
367 while ( FXU_HEAP_DOUBLE_PARENT_EXISTS(p,*ppDiv) )
368 {
369 ppPar = &FXU_HEAP_DOUBLE_PARENT(p,*ppDiv);
370 if ( FXU_HEAP_DOUBLE_WEIGHT(*ppDiv) > FXU_HEAP_DOUBLE_WEIGHT(*ppPar) )
371 {
372 Fxu_HeapDoubleSwap( ppDiv, ppPar );
373 ppDiv = ppPar;
374 }
375 else
376 break;
377 }
378}
379
391void Fxu_HeapDoubleMoveDn( Fxu_HeapDouble * p, Fxu_Double * pDiv )
392{
393 Fxu_Double ** ppChild1, ** ppChild2, ** ppDiv;
394 ppDiv = &FXU_HEAP_DOUBLE_CURRENT(p, pDiv);
395 while ( FXU_HEAP_DOUBLE_CHILD1_EXISTS(p,*ppDiv) )
396 { // if Child1 does not exist, Child2 also does not exists
397
398 // get the children
399 ppChild1 = &FXU_HEAP_DOUBLE_CHILD1(p,*ppDiv);
400 if ( FXU_HEAP_DOUBLE_CHILD2_EXISTS(p,*ppDiv) )
401 {
402 ppChild2 = &FXU_HEAP_DOUBLE_CHILD2(p,*ppDiv);
403
404 // consider two cases
405 if ( FXU_HEAP_DOUBLE_WEIGHT(*ppDiv) >= FXU_HEAP_DOUBLE_WEIGHT(*ppChild1) &&
406 FXU_HEAP_DOUBLE_WEIGHT(*ppDiv) >= FXU_HEAP_DOUBLE_WEIGHT(*ppChild2) )
407 { // Div is larger than both, skip
408 break;
409 }
410 else
411 { // Div is smaller than one of them, then swap it with larger
412 if ( FXU_HEAP_DOUBLE_WEIGHT(*ppChild1) >= FXU_HEAP_DOUBLE_WEIGHT(*ppChild2) )
413 {
414 Fxu_HeapDoubleSwap( ppDiv, ppChild1 );
415 // update the pointer
416 ppDiv = ppChild1;
417 }
418 else
419 {
420 Fxu_HeapDoubleSwap( ppDiv, ppChild2 );
421 // update the pointer
422 ppDiv = ppChild2;
423 }
424 }
425 }
426 else // Child2 does not exist
427 {
428 // consider two cases
429 if ( FXU_HEAP_DOUBLE_WEIGHT(*ppDiv) >= FXU_HEAP_DOUBLE_WEIGHT(*ppChild1) )
430 { // Div is larger than Child1, skip
431 break;
432 }
433 else
434 { // Div is smaller than Child1, then swap them
435 Fxu_HeapDoubleSwap( ppDiv, ppChild1 );
436 // update the pointer
437 ppDiv = ppChild1;
438 }
439 }
440 }
441}
442
443
447
448
450
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_REALLOC(type, obj, num)
Definition abc_global.h:268
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
Cube * p
Definition exorList.c:222
#define FXU_HEAP_DOUBLE_CHILD2_EXISTS(p, pDiv)
Definition fxuHeapD.c:32
void Fxu_HeapDoubleUpdate(Fxu_HeapDouble *p, Fxu_Double *pDiv)
Definition fxuHeapD.c:223
#define FXU_HEAP_DOUBLE_CHILD1_EXISTS(p, pDiv)
Definition fxuHeapD.c:31
void Fxu_HeapDoubleStop(Fxu_HeapDouble *p)
Definition fxuHeapD.c:99
void Fxu_HeapDoublePrint(FILE *pFile, Fxu_HeapDouble *p)
Definition fxuHeapD.c:117
#define FXU_HEAP_DOUBLE_PARENT(p, pDiv)
Definition fxuHeapD.c:33
#define FXU_HEAP_DOUBLE_WEIGHT(pDiv)
DECLARATIONS ///.
Definition fxuHeapD.c:28
Fxu_HeapDouble * Fxu_HeapDoubleStart()
FUNCTION DEFINITIONS ///.
Definition fxuHeapD.c:58
void Fxu_HeapDoubleCheckOne(Fxu_HeapDouble *p, Fxu_Double *pDiv)
Definition fxuHeapD.c:173
#define FXU_HEAP_DOUBLE_ASSERT(p, pDiv)
Definition fxuHeapD.c:36
void Fxu_HeapDoubleCheck(Fxu_HeapDouble *p)
Definition fxuHeapD.c:152
int Fxu_HeapDoubleReadMaxWeight(Fxu_HeapDouble *p)
Definition fxuHeapD.c:319
#define FXU_HEAP_DOUBLE_PARENT_EXISTS(p, pDiv)
Definition fxuHeapD.c:30
#define FXU_HEAP_DOUBLE_CURRENT(p, pDiv)
Definition fxuHeapD.c:29
void Fxu_HeapDoubleDelete(Fxu_HeapDouble *p, Fxu_Double *pDiv)
Definition fxuHeapD.c:250
void Fxu_HeapDoubleInsert(Fxu_HeapDouble *p, Fxu_Double *pDiv)
Definition fxuHeapD.c:201
#define FXU_HEAP_DOUBLE_CHILD2(p, pDiv)
Definition fxuHeapD.c:35
#define FXU_HEAP_DOUBLE_CHILD1(p, pDiv)
Definition fxuHeapD.c:34
Fxu_Double * Fxu_HeapDoubleGetMax(Fxu_HeapDouble *p)
Definition fxuHeapD.c:291
Fxu_Double * Fxu_HeapDoubleReadMax(Fxu_HeapDouble *p)
Definition fxuHeapD.c:273
struct FxuHeapDouble Fxu_HeapDouble
Definition fxuInt.h:84
struct FxuDouble Fxu_Double
Definition fxuInt.h:72
#define Fxu_HeapDoubleForEachItem(Heap, Div)
Definition fxuInt.h:375
int HNum
Definition fxuInt.h:257
#define assert(ex)
Definition util_old.h:213
char * memset()