00001
00019 #include "mvc.h"
00020
00024
00025
00026 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 *) );
00027 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 *) );
00028
00032
00044 void Mvc_CoverSort( Mvc_Cover_t * pCover, Mvc_Cube_t * pMask, int (* pCompareFunc)(Mvc_Cube_t *, Mvc_Cube_t *, Mvc_Cube_t *) )
00045 {
00046 Mvc_Cube_t * pHead;
00047 int nCubes;
00048
00049 nCubes = Mvc_CoverReadCubeNum(pCover);
00050 if ( nCubes <= 1 )
00051 return;
00052
00053 pHead = Mvc_CoverSort_rec( Mvc_CoverReadCubeHead(pCover), nCubes, pMask, pCompareFunc );
00054
00055 Mvc_CoverSetCubeHead( pCover, pHead );
00056 Mvc_CoverSetCubeTail( pCover, Mvc_ListGetTailFromHead(pHead) );
00057
00058 assert( pCompareFunc( Mvc_CoverReadCubeHead(pCover), Mvc_CoverReadCubeTail(pCover), pMask ) <= 0 );
00059 }
00060
00072 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 *) )
00073 {
00074 Mvc_Cube_t * pList1, * pList2;
00075 int nItems1, nItems2, i;
00076
00077
00078 if ( nItems == 1 )
00079 {
00080 Mvc_CubeSetNext( pList, NULL );
00081 return pList;
00082 }
00083
00084
00085 nItems1 = nItems/2;
00086 nItems2 = nItems - nItems1;
00087
00088
00089 pList1 = pList2 = pList;
00090 for ( i = 0; i < nItems1; i++ )
00091 pList2 = Mvc_CubeReadNext( pList2 );
00092
00093
00094 pList1 = Mvc_CoverSort_rec( pList1, nItems1, pMask, pCompareFunc );
00095 pList2 = Mvc_CoverSort_rec( pList2, nItems2, pMask, pCompareFunc );
00096
00097
00098 return Mvc_CoverSortMerge( pList1, pList2, pMask, pCompareFunc );
00099 }
00100
00101
00113 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 *) )
00114 {
00115 Mvc_Cube_t * pList = NULL, ** ppTail = &pList;
00116 Mvc_Cube_t * pCube;
00117 while ( pList1 && pList2 )
00118 {
00119 if ( pCompareFunc( pList1, pList2, pMask ) < 0 )
00120 {
00121 pCube = pList1;
00122 pList1 = Mvc_CubeReadNext(pList1);
00123 }
00124 else
00125 {
00126 pCube = pList2;
00127 pList2 = Mvc_CubeReadNext(pList2);
00128 }
00129 *ppTail = pCube;
00130 ppTail = Mvc_CubeReadNextP(pCube);
00131 }
00132 *ppTail = pList1? pList1: pList2;
00133 return pList;
00134 }
00135
00136
00140
00141