ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
mvcSort.c
Go to the documentation of this file.
1
18
19#include "mvc.h"
20
22
23
27
28
29Mvc_Cube_t * Mvc_CoverSort_rec( Mvc_Cube_t * pList, int nItems, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) );
30Mvc_Cube_t * Mvc_CoverSortMerge( Mvc_Cube_t * pList1, Mvc_Cube_t * pList2, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) );
31
35
47void Mvc_CoverSort( Mvc_Cover_t * pCover, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) )
48{
49 Mvc_Cube_t * pHead;
50 int nCubes;
51 // one cube does not need sorting
52 nCubes = Mvc_CoverReadCubeNum(pCover);
53 if ( nCubes <= 1 )
54 return;
55 // sort the cubes
56 pHead = Mvc_CoverSort_rec( Mvc_CoverReadCubeHead(pCover), nCubes, pMask, pCompareFunc );
57 // insert the sorted list into the cover
58 Mvc_CoverSetCubeHead( pCover, pHead );
60 // make sure that the list is sorted in the increasing order
61 assert( pCompareFunc( Mvc_CoverReadCubeHead(pCover), Mvc_CoverReadCubeTail(pCover), pMask ) <= 0 );
62}
63
75Mvc_Cube_t * Mvc_CoverSort_rec( Mvc_Cube_t * pList, int nItems, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) )
76{
77 Mvc_Cube_t * pList1, * pList2;
78 int nItems1, nItems2, i;
79
80 // trivial case
81 if ( nItems == 1 )
82 {
83 Mvc_CubeSetNext( pList, NULL );
84 return pList;
85 }
86
87 // select the new sizes
88 nItems1 = nItems/2;
89 nItems2 = nItems - nItems1;
90
91 // set the new beginnings
92 pList1 = pList2 = pList;
93 for ( i = 0; i < nItems1; i++ )
94 pList2 = Mvc_CubeReadNext( pList2 );
95
96 // solve recursively
97 pList1 = Mvc_CoverSort_rec( pList1, nItems1, pMask, pCompareFunc );
98 pList2 = Mvc_CoverSort_rec( pList2, nItems2, pMask, pCompareFunc );
99
100 // merge
101 return Mvc_CoverSortMerge( pList1, pList2, pMask, pCompareFunc );
102}
103
104
116Mvc_Cube_t * Mvc_CoverSortMerge( Mvc_Cube_t * pList1, Mvc_Cube_t * pList2, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) )
117{
118 Mvc_Cube_t * pList = NULL, ** ppTail = &pList;
119 Mvc_Cube_t * pCube;
120 while ( pList1 && pList2 )
121 {
122 if ( pCompareFunc( pList1, pList2, pMask ) < 0 )
123 {
124 pCube = pList1;
125 pList1 = Mvc_CubeReadNext(pList1);
126 }
127 else
128 {
129 pCube = pList2;
130 pList2 = Mvc_CubeReadNext(pList2);
131 }
132 *ppTail = pCube;
133 ppTail = Mvc_CubeReadNextP(pCube);
134 }
135 *ppTail = pList1? pList1: pList2;
136 return pList;
137}
138
139
143
144
146
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
Mvc_Cube_t * Mvc_CoverSortMerge(Mvc_Cube_t *pList1, Mvc_Cube_t *pList2, Mvc_Cube_t *pMask, int(*pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *))
Definition mvcSort.c:116
ABC_NAMESPACE_IMPL_START Mvc_Cube_t * Mvc_CoverSort_rec(Mvc_Cube_t *pList, int nItems, Mvc_Cube_t *pMask, int(*pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *))
DECLARATIONS ///.
Definition mvcSort.c:75
void Mvc_CoverSort(Mvc_Cover_t *pCover, Mvc_Cube_t *pMask, int(*pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *))
FuNCTION DEFINITIONS ///.
Definition mvcSort.c:47
void Mvc_CoverSetCubeHead(Mvc_Cover_t *pCover, Mvc_Cube_t *pCube)
Definition mvcApi.c:78
struct MvcCubeStruct Mvc_Cube_t
Definition mvc.h:56
#define Mvc_CubeReadNext(Cube)
MACRO DEFINITIONS ///.
Definition mvc.h:121
#define Mvc_CubeSetNext(Cube, Next)
Definition mvc.h:126
#define Mvc_CubeReadNextP(Cube)
Definition mvc.h:122
Mvc_Cube_t * Mvc_ListGetTailFromHead(Mvc_Cube_t *pHead)
Definition mvcList.c:351
void Mvc_CoverSetCubeTail(Mvc_Cover_t *pCover, Mvc_Cube_t *pCube)
Definition mvcApi.c:79
struct MvcCoverStruct Mvc_Cover_t
Definition mvc.h:58
Mvc_Cube_t * Mvc_CoverReadCubeTail(Mvc_Cover_t *pCover)
Definition mvcApi.c:47
Mvc_Cube_t * Mvc_CoverReadCubeHead(Mvc_Cover_t *pCover)
Definition mvcApi.c:46
int Mvc_CoverReadCubeNum(Mvc_Cover_t *pCover)
Definition mvcApi.c:45
#define assert(ex)
Definition util_old.h:213