00001 00019 #include "mapperInt.h" 00020 00024 00025 static int Map_NodeVecCompareLevels( Map_Node_t ** pp1, Map_Node_t ** pp2 ); 00026 00030 00042 Map_NodeVec_t * Map_NodeVecAlloc( int nCap ) 00043 { 00044 Map_NodeVec_t * p; 00045 p = ALLOC( Map_NodeVec_t, 1 ); 00046 if ( nCap > 0 && nCap < 16 ) 00047 nCap = 16; 00048 p->nSize = 0; 00049 p->nCap = nCap; 00050 p->pArray = p->nCap? ALLOC( Map_Node_t *, p->nCap ) : NULL; 00051 return p; 00052 } 00053 00065 void Map_NodeVecFree( Map_NodeVec_t * p ) 00066 { 00067 FREE( p->pArray ); 00068 FREE( p ); 00069 } 00070 00082 Map_Node_t ** Map_NodeVecReadArray( Map_NodeVec_t * p ) 00083 { 00084 return p->pArray; 00085 } 00086 00098 int Map_NodeVecReadSize( Map_NodeVec_t * p ) 00099 { 00100 return p->nSize; 00101 } 00102 00114 void Map_NodeVecGrow( Map_NodeVec_t * p, int nCapMin ) 00115 { 00116 if ( p->nCap >= nCapMin ) 00117 return; 00118 p->pArray = REALLOC( Map_Node_t *, p->pArray, nCapMin ); 00119 p->nCap = nCapMin; 00120 } 00121 00133 void Map_NodeVecShrink( Map_NodeVec_t * p, int nSizeNew ) 00134 { 00135 assert( p->nSize >= nSizeNew ); 00136 p->nSize = nSizeNew; 00137 } 00138 00150 void Map_NodeVecClear( Map_NodeVec_t * p ) 00151 { 00152 p->nSize = 0; 00153 } 00154 00166 void Map_NodeVecPush( Map_NodeVec_t * p, Map_Node_t * Entry ) 00167 { 00168 if ( p->nSize == p->nCap ) 00169 { 00170 if ( p->nCap < 16 ) 00171 Map_NodeVecGrow( p, 16 ); 00172 else 00173 Map_NodeVecGrow( p, 2 * p->nCap ); 00174 } 00175 p->pArray[p->nSize++] = Entry; 00176 } 00177 00189 int Map_NodeVecPushUnique( Map_NodeVec_t * p, Map_Node_t * Entry ) 00190 { 00191 int i; 00192 for ( i = 0; i < p->nSize; i++ ) 00193 if ( p->pArray[i] == Entry ) 00194 return 1; 00195 Map_NodeVecPush( p, Entry ); 00196 return 0; 00197 } 00198 00210 Map_Node_t * Map_NodeVecPop( Map_NodeVec_t * p ) 00211 { 00212 return p->pArray[--p->nSize]; 00213 } 00214 00226 void Map_NodeVecRemove( Map_NodeVec_t * p, Map_Node_t * Entry ) 00227 { 00228 int i; 00229 for ( i = 0; i < p->nSize; i++ ) 00230 if ( p->pArray[i] == Entry ) 00231 break; 00232 assert( i < p->nSize ); 00233 for ( i++; i < p->nSize; i++ ) 00234 p->pArray[i-1] = p->pArray[i]; 00235 p->nSize--; 00236 } 00237 00249 void Map_NodeVecWriteEntry( Map_NodeVec_t * p, int i, Map_Node_t * Entry ) 00250 { 00251 assert( i >= 0 && i < p->nSize ); 00252 p->pArray[i] = Entry; 00253 } 00254 00266 Map_Node_t * Map_NodeVecReadEntry( Map_NodeVec_t * p, int i ) 00267 { 00268 assert( i >= 0 && i < p->nSize ); 00269 return p->pArray[i]; 00270 } 00271 00283 void Map_NodeVecSortByLevel( Map_NodeVec_t * p ) 00284 { 00285 qsort( (void *)p->pArray, p->nSize, sizeof(Map_Node_t *), 00286 (int (*)(const void *, const void *)) Map_NodeVecCompareLevels ); 00287 } 00288 00300 int Map_NodeVecCompareLevels( Map_Node_t ** pp1, Map_Node_t ** pp2 ) 00301 { 00302 int Level1 = Map_Regular(*pp1)->Level; 00303 int Level2 = Map_Regular(*pp2)->Level; 00304 if ( Level1 < Level2 ) 00305 return -1; 00306 if ( Level1 > Level2 ) 00307 return 1; 00308 if ( Map_Regular(*pp1)->Num < Map_Regular(*pp2)->Num ) 00309 return -1; 00310 if ( Map_Regular(*pp1)->Num > Map_Regular(*pp2)->Num ) 00311 return 1; 00312 return 0; 00313 } 00314 00318