ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
fxuHeapS.c
Go to the documentation of this file.
1
18
19#include "fxuInt.h"
20
22
23
27
28#define FXU_HEAP_SINGLE_WEIGHT(pSingle) ((pSingle)->Weight)
29#define FXU_HEAP_SINGLE_CURRENT(p, pSingle) ((p)->pTree[(pSingle)->HNum])
30#define FXU_HEAP_SINGLE_PARENT_EXISTS(p, pSingle) ((pSingle)->HNum > 1)
31#define FXU_HEAP_SINGLE_CHILD1_EXISTS(p, pSingle) (((pSingle)->HNum << 1) <= p->nItems)
32#define FXU_HEAP_SINGLE_CHILD2_EXISTS(p, pSingle) ((((pSingle)->HNum << 1)+1) <= p->nItems)
33#define FXU_HEAP_SINGLE_PARENT(p, pSingle) ((p)->pTree[(pSingle)->HNum >> 1])
34#define FXU_HEAP_SINGLE_CHILD1(p, pSingle) ((p)->pTree[(pSingle)->HNum << 1])
35#define FXU_HEAP_SINGLE_CHILD2(p, pSingle) ((p)->pTree[((pSingle)->HNum << 1)+1])
36#define FXU_HEAP_SINGLE_ASSERT(p, pSingle) assert( (pSingle)->HNum >= 1 && (pSingle)->HNum <= p->nItemsAlloc )
37
38static void Fxu_HeapSingleResize( Fxu_HeapSingle * p );
39static void Fxu_HeapSingleSwap( Fxu_Single ** pSingle1, Fxu_Single ** pSingle2 );
40static void Fxu_HeapSingleMoveUp( Fxu_HeapSingle * p, Fxu_Single * pSingle );
41static void Fxu_HeapSingleMoveDn( Fxu_HeapSingle * p, Fxu_Single * pSingle );
42
46
59{
62 memset( p, 0, sizeof(Fxu_HeapSingle) );
63 p->nItems = 0;
64 p->nItemsAlloc = 2000;
65 p->pTree = ABC_ALLOC( Fxu_Single *, p->nItemsAlloc + 10 );
66 p->pTree[0] = NULL;
67 return p;
68}
69
70
82void Fxu_HeapSingleResize( Fxu_HeapSingle * p )
83{
84 p->nItemsAlloc *= 2;
85 p->pTree = ABC_REALLOC( Fxu_Single *, p->pTree, p->nItemsAlloc + 10 );
86}
87
100{
101 int i;
102 i = 0;
103 ABC_FREE( p->pTree );
104 i = 1;
105 ABC_FREE( p );
106}
107
108
120void Fxu_HeapSinglePrint( FILE * pFile, Fxu_HeapSingle * p )
121{
122 Fxu_Single * pSingle;
123 int Counter = 1;
124 int Degree = 1;
125
127 fprintf( pFile, "The contents of the heap:\n" );
128 fprintf( pFile, "Level %d: ", Degree );
129 Fxu_HeapSingleForEachItem( p, pSingle )
130 {
131 assert( Counter == p->pTree[Counter]->HNum );
132 fprintf( pFile, "%2d=%3d ", Counter, FXU_HEAP_SINGLE_WEIGHT(p->pTree[Counter]) );
133 if ( ++Counter == (1 << Degree) )
134 {
135 fprintf( pFile, "\n" );
136 Degree++;
137 fprintf( pFile, "Level %d: ", Degree );
138 }
139 }
140 fprintf( pFile, "\n" );
141 fprintf( pFile, "End of the heap printout.\n" );
142}
143
156{
157 Fxu_Single * pSingle;
158 Fxu_HeapSingleForEachItem( p, pSingle )
159 {
160 assert( pSingle->HNum == p->i );
161 Fxu_HeapSingleCheckOne( p, pSingle );
162 }
163}
164
177{
178 int Weight1, Weight2;
179 if ( FXU_HEAP_SINGLE_CHILD1_EXISTS(p,pSingle) )
180 {
181 Weight1 = FXU_HEAP_SINGLE_WEIGHT(pSingle);
182 Weight2 = FXU_HEAP_SINGLE_WEIGHT( FXU_HEAP_SINGLE_CHILD1(p,pSingle) );
183 assert( Weight1 >= Weight2 );
184 }
185 if ( FXU_HEAP_SINGLE_CHILD2_EXISTS(p,pSingle) )
186 {
187 Weight1 = FXU_HEAP_SINGLE_WEIGHT(pSingle);
188 Weight2 = FXU_HEAP_SINGLE_WEIGHT( FXU_HEAP_SINGLE_CHILD2(p,pSingle) );
189 assert( Weight1 >= Weight2 );
190 }
191}
192
205{
206 if ( p->nItems == p->nItemsAlloc )
207 Fxu_HeapSingleResize( p );
208 // put the last entry to the last place and move up
209 p->pTree[++p->nItems] = pSingle;
210 pSingle->HNum = p->nItems;
211 // move the last entry up if necessary
212 Fxu_HeapSingleMoveUp( p, pSingle );
213}
214
227{
228 FXU_HEAP_SINGLE_ASSERT(p,pSingle);
229 if ( FXU_HEAP_SINGLE_PARENT_EXISTS(p,pSingle) &&
231 Fxu_HeapSingleMoveUp( p, pSingle );
232 else if ( FXU_HEAP_SINGLE_CHILD1_EXISTS(p,pSingle) &&
234 Fxu_HeapSingleMoveDn( p, pSingle );
235 else if ( FXU_HEAP_SINGLE_CHILD2_EXISTS(p,pSingle) &&
237 Fxu_HeapSingleMoveDn( p, pSingle );
238}
239
252{
253 int Place = pSingle->HNum;
254 FXU_HEAP_SINGLE_ASSERT(p,pSingle);
255 // put the last entry to the deleted place
256 // decrement the number of entries
257 p->pTree[Place] = p->pTree[p->nItems--];
258 p->pTree[Place]->HNum = Place;
259 // move the top entry down if necessary
260 Fxu_HeapSingleUpdate( p, p->pTree[Place] );
261 pSingle->HNum = 0;
262}
263
276{
277 if ( p->nItems == 0 )
278 return NULL;
279 return p->pTree[1];
280}
281
294{
295 Fxu_Single * pSingle;
296 if ( p->nItems == 0 )
297 return NULL;
298 // prepare the return value
299 pSingle = p->pTree[1];
300 pSingle->HNum = 0;
301 // put the last entry on top
302 // decrement the number of entries
303 p->pTree[1] = p->pTree[p->nItems--];
304 p->pTree[1]->HNum = 1;
305 // move the top entry down if necessary
306 Fxu_HeapSingleMoveDn( p, p->pTree[1] );
307 return pSingle;
308}
309
322{
323 if ( p->nItems == 0 )
324 return -1;
325 return FXU_HEAP_SINGLE_WEIGHT(p->pTree[1]);
326}
327
328
329
341void Fxu_HeapSingleSwap( Fxu_Single ** pSingle1, Fxu_Single ** pSingle2 )
342{
343 Fxu_Single * pSingle;
344 int Temp;
345 pSingle = *pSingle1;
346 *pSingle1 = *pSingle2;
347 *pSingle2 = pSingle;
348 Temp = (*pSingle1)->HNum;
349 (*pSingle1)->HNum = (*pSingle2)->HNum;
350 (*pSingle2)->HNum = Temp;
351}
352
364void Fxu_HeapSingleMoveUp( Fxu_HeapSingle * p, Fxu_Single * pSingle )
365{
366 Fxu_Single ** ppSingle, ** ppPar;
367 ppSingle = &FXU_HEAP_SINGLE_CURRENT(p, pSingle);
368 while ( FXU_HEAP_SINGLE_PARENT_EXISTS(p,*ppSingle) )
369 {
370 ppPar = &FXU_HEAP_SINGLE_PARENT(p,*ppSingle);
371 if ( FXU_HEAP_SINGLE_WEIGHT(*ppSingle) > FXU_HEAP_SINGLE_WEIGHT(*ppPar) )
372 {
373 Fxu_HeapSingleSwap( ppSingle, ppPar );
374 ppSingle = ppPar;
375 }
376 else
377 break;
378 }
379}
380
392void Fxu_HeapSingleMoveDn( Fxu_HeapSingle * p, Fxu_Single * pSingle )
393{
394 Fxu_Single ** ppChild1, ** ppChild2, ** ppSingle;
395 ppSingle = &FXU_HEAP_SINGLE_CURRENT(p, pSingle);
396 while ( FXU_HEAP_SINGLE_CHILD1_EXISTS(p,*ppSingle) )
397 { // if Child1 does not exist, Child2 also does not exists
398
399 // get the children
400 ppChild1 = &FXU_HEAP_SINGLE_CHILD1(p,*ppSingle);
401 if ( FXU_HEAP_SINGLE_CHILD2_EXISTS(p,*ppSingle) )
402 {
403 ppChild2 = &FXU_HEAP_SINGLE_CHILD2(p,*ppSingle);
404
405 // consider two cases
406 if ( FXU_HEAP_SINGLE_WEIGHT(*ppSingle) >= FXU_HEAP_SINGLE_WEIGHT(*ppChild1) &&
407 FXU_HEAP_SINGLE_WEIGHT(*ppSingle) >= FXU_HEAP_SINGLE_WEIGHT(*ppChild2) )
408 { // Var is larger than both, skip
409 break;
410 }
411 else
412 { // Var is smaller than one of them, then swap it with larger
413 if ( FXU_HEAP_SINGLE_WEIGHT(*ppChild1) >= FXU_HEAP_SINGLE_WEIGHT(*ppChild2) )
414 {
415 Fxu_HeapSingleSwap( ppSingle, ppChild1 );
416 // update the pointer
417 ppSingle = ppChild1;
418 }
419 else
420 {
421 Fxu_HeapSingleSwap( ppSingle, ppChild2 );
422 // update the pointer
423 ppSingle = ppChild2;
424 }
425 }
426 }
427 else // Child2 does not exist
428 {
429 // consider two cases
430 if ( FXU_HEAP_SINGLE_WEIGHT(*ppSingle) >= FXU_HEAP_SINGLE_WEIGHT(*ppChild1) )
431 { // Var is larger than Child1, skip
432 break;
433 }
434 else
435 { // Var is smaller than Child1, then swap them
436 Fxu_HeapSingleSwap( ppSingle, ppChild1 );
437 // update the pointer
438 ppSingle = ppChild1;
439 }
440 }
441 }
442}
443
444
449
#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
Fxu_HeapSingle * Fxu_HeapSingleStart()
FUNCTION DEFINITIONS ///.
Definition fxuHeapS.c:58
#define FXU_HEAP_SINGLE_CHILD2_EXISTS(p, pSingle)
Definition fxuHeapS.c:32
#define FXU_HEAP_SINGLE_CURRENT(p, pSingle)
Definition fxuHeapS.c:29
Fxu_Single * Fxu_HeapSingleReadMax(Fxu_HeapSingle *p)
Definition fxuHeapS.c:275
void Fxu_HeapSingleUpdate(Fxu_HeapSingle *p, Fxu_Single *pSingle)
Definition fxuHeapS.c:226
#define FXU_HEAP_SINGLE_WEIGHT(pSingle)
DECLARATIONS ///.
Definition fxuHeapS.c:28
#define FXU_HEAP_SINGLE_ASSERT(p, pSingle)
Definition fxuHeapS.c:36
Fxu_Single * Fxu_HeapSingleGetMax(Fxu_HeapSingle *p)
Definition fxuHeapS.c:293
#define FXU_HEAP_SINGLE_CHILD1(p, pSingle)
Definition fxuHeapS.c:34
void Fxu_HeapSingleInsert(Fxu_HeapSingle *p, Fxu_Single *pSingle)
Definition fxuHeapS.c:204
void Fxu_HeapSingleStop(Fxu_HeapSingle *p)
Definition fxuHeapS.c:99
void Fxu_HeapSingleDelete(Fxu_HeapSingle *p, Fxu_Single *pSingle)
Definition fxuHeapS.c:251
void Fxu_HeapSingleCheckOne(Fxu_HeapSingle *p, Fxu_Single *pSingle)
Definition fxuHeapS.c:176
#define FXU_HEAP_SINGLE_PARENT(p, pSingle)
Definition fxuHeapS.c:33
#define FXU_HEAP_SINGLE_CHILD2(p, pSingle)
Definition fxuHeapS.c:35
#define FXU_HEAP_SINGLE_CHILD1_EXISTS(p, pSingle)
Definition fxuHeapS.c:31
void Fxu_HeapSingleCheck(Fxu_HeapSingle *p)
Definition fxuHeapS.c:155
int Fxu_HeapSingleReadMaxWeight(Fxu_HeapSingle *p)
Definition fxuHeapS.c:321
void Fxu_HeapSinglePrint(FILE *pFile, Fxu_HeapSingle *p)
Definition fxuHeapS.c:120
#define FXU_HEAP_SINGLE_PARENT_EXISTS(p, pSingle)
Definition fxuHeapS.c:30
struct FxuSingle Fxu_Single
Definition fxuInt.h:73
#define Fxu_HeapSingleForEachItem(Heap, Single)
Definition fxuInt.h:379
struct FxuHeapSingle Fxu_HeapSingle
Definition fxuInt.h:85
int HNum
Definition fxuInt.h:270
#define assert(ex)
Definition util_old.h:213
char * memset()