use std::sort instead of qsort

This commit is contained in:
Garux 2021-03-09 20:09:08 +03:00
parent 52ef1c838a
commit 0737e962de
4 changed files with 221 additions and 276 deletions

View File

@ -265,33 +265,6 @@ int ImportLightmapsMain( int argc, char **argv ){
------------------------------------------------------------------------------- */ ------------------------------------------------------------------------------- */
/*
CompareLightSurface()
compare function for qsort()
*/
static int CompareLightSurface( const void *a, const void *b ){
shaderInfo_t *asi, *bsi;
/* get shaders */
asi = surfaceInfos[ *( (const int*) a ) ].si;
bsi = surfaceInfos[ *( (const int*) b ) ].si;
/* dummy check */
if ( asi == NULL ) {
return -1;
}
if ( bsi == NULL ) {
return 1;
}
/* compare shader names */
return strcmp( asi->shader, bsi->shader );
}
/* /*
FinishRawLightmap() FinishRawLightmap()
allocates a raw lightmap's necessary buffers allocates a raw lightmap's necessary buffers
@ -304,7 +277,22 @@ void FinishRawLightmap( rawLightmap_t *lm ){
/* sort light surfaces by shader name */ /* sort light surfaces by shader name */
qsort( &lightSurfaces[ lm->firstLightSurface ], lm->numLightSurfaces, sizeof( int ), CompareLightSurface ); std::sort( &lightSurfaces[ lm->firstLightSurface ], &lightSurfaces[ lm->firstLightSurface ] + lm->numLightSurfaces, []( const int a, const int b ){
/* get shaders */
const shaderInfo_t *asi = surfaceInfos[ a ].si;
const shaderInfo_t *bsi = surfaceInfos[ b ].si;
/* dummy check */
if ( asi == NULL ) {
return true;
}
if ( bsi == NULL ) {
return false;
}
/* compare shader names */
return strcmp( asi->shader, bsi->shader ) < 0;
} );
/* count clusters */ /* count clusters */
lm->numLightClusters = 0; lm->numLightClusters = 0;
@ -834,102 +822,101 @@ bool AddSurfaceToRawLightmap( int num, rawLightmap_t *lm ){
/* /*
CompareSurfaceInfo() CompareSurfaceInfo()
compare function for qsort() compare functor for std::sort()
*/ */
static int CompareSurfaceInfo( const void *a, const void *b ){ struct CompareSurfaceInfo
surfaceInfo_t *aInfo, *bInfo; {
int i; bool operator()( const int a, const int b ) const {
/* get surface info */
const surfaceInfo_t& aInfo = surfaceInfos[ a ];
const surfaceInfo_t& bInfo = surfaceInfos[ b ];
/* model first */
/* get surface info */ if ( aInfo.modelindex < bInfo.modelindex ) {
aInfo = &surfaceInfos[ *( (const int*) a ) ]; return false;
bInfo = &surfaceInfos[ *( (const int*) b ) ];
/* model first */
if ( aInfo->modelindex < bInfo->modelindex ) {
return 1;
}
else if ( aInfo->modelindex > bInfo->modelindex ) {
return -1;
}
/* then lightmap status */
if ( aInfo->hasLightmap < bInfo->hasLightmap ) {
return 1;
}
else if ( aInfo->hasLightmap > bInfo->hasLightmap ) {
return -1;
}
/* 27: then shader! */
if ( aInfo->si < bInfo->si ) {
return 1;
}
else if ( aInfo->si > bInfo->si ) {
return -1;
}
/* then lightmap sample size */
if ( aInfo->sampleSize < bInfo->sampleSize ) {
return 1;
}
else if ( aInfo->sampleSize > bInfo->sampleSize ) {
return -1;
}
/* then lightmap axis */
for ( i = 0; i < 3; i++ )
{
if ( aInfo->axis[ i ] < bInfo->axis[ i ] ) {
return 1;
} }
else if ( aInfo->axis[ i ] > bInfo->axis[ i ] ) { else if ( aInfo.modelindex > bInfo.modelindex ) {
return -1; return true;
} }
}
/* then plane */ /* then lightmap status */
if ( aInfo->plane == NULL && bInfo->plane != NULL ) { if ( aInfo.hasLightmap < bInfo.hasLightmap ) {
return 1; return false;
} }
else if ( aInfo->plane != NULL && bInfo->plane == NULL ) { else if ( aInfo.hasLightmap > bInfo.hasLightmap ) {
return -1; return true;
} }
else if ( aInfo->plane != NULL && bInfo->plane != NULL ) {
for ( i = 0; i < 3; i++ ) /* 27: then shader! */
if ( aInfo.si < bInfo.si ) {
return false;
}
else if ( aInfo.si > bInfo.si ) {
return true;
}
/* then lightmap sample size */
if ( aInfo.sampleSize < bInfo.sampleSize ) {
return false;
}
else if ( aInfo.sampleSize > bInfo.sampleSize ) {
return true;
}
/* then lightmap axis */
for ( int i = 0; i < 3; i++ )
{ {
if ( aInfo->plane->normal()[ i ] < bInfo->plane->normal()[ i ] ) { if ( aInfo.axis[ i ] < bInfo.axis[ i ] ) {
return 1; return false;
} }
else if ( aInfo->plane->normal()[ i ] > bInfo->plane->normal()[ i ] ) { else if ( aInfo.axis[ i ] > bInfo.axis[ i ] ) {
return -1; return true;
} }
} }
if ( aInfo->plane->dist() < bInfo->plane->dist() ) {
return 1; /* then plane */
if ( aInfo.plane == NULL && bInfo.plane != NULL ) {
return false;
} }
else if ( aInfo->plane->dist() > bInfo->plane->dist() ) { else if ( aInfo.plane != NULL && bInfo.plane == NULL ) {
return -1; return true;
}
else if ( aInfo.plane != NULL && bInfo.plane != NULL ) {
for ( int i = 0; i < 3; i++ )
{
if ( aInfo.plane->normal()[ i ] < bInfo.plane->normal()[ i ] ) {
return false;
}
else if ( aInfo.plane->normal()[ i ] > bInfo.plane->normal()[ i ] ) {
return true;
}
}
if ( aInfo.plane->dist() < bInfo.plane->dist() ) {
return false;
}
else if ( aInfo.plane->dist() > bInfo.plane->dist() ) {
return true;
}
} }
/* then position in world */
for ( int i = 0; i < 3; i++ )
{
if ( aInfo.minmax.mins[ i ] < bInfo.minmax.mins[ i ] ) {
return false;
}
else if ( aInfo.minmax.mins[ i ] > bInfo.minmax.mins[ i ] ) {
return true;
}
}
/* these are functionally identical (this should almost never happen) */
return false;
} }
};
/* then position in world */
for ( i = 0; i < 3; i++ )
{
if ( aInfo->minmax.mins[ i ] < bInfo->minmax.mins[ i ] ) {
return 1;
}
else if ( aInfo->minmax.mins[ i ] > bInfo->minmax.mins[ i ] ) {
return -1;
}
}
/* these are functionally identical (this should almost never happen) */
return 0;
}
@ -1079,7 +1066,7 @@ void SetupSurfaceLightmaps( void ){
maxMapDistance = vector3_length( g_mapMinmax.maxs - g_mapMinmax.mins ); maxMapDistance = vector3_length( g_mapMinmax.maxs - g_mapMinmax.mins );
/* sort the surfaces info list */ /* sort the surfaces info list */
qsort( sortSurfaces, numBSPDrawSurfaces, sizeof( int ), CompareSurfaceInfo ); std::sort( sortSurfaces, sortSurfaces + numBSPDrawSurfaces, CompareSurfaceInfo() );
/* allocate a list of surfaces that would go into raw lightmaps */ /* allocate a list of surfaces that would go into raw lightmaps */
numLightSurfaces = 0; numLightSurfaces = 0;
@ -2205,64 +2192,53 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){
/* /*
CompareRawLightmap() CompareRawLightmap
compare function for qsort() compare functor for std::sort()
*/ */
static int CompareRawLightmap( const void *a, const void *b ){ struct CompareRawLightmap
rawLightmap_t *alm, *blm; {
surfaceInfo_t *aInfo, *bInfo; bool operator()( const int a, const int b ) const {
int i, min, diff; int diff;
/* get lightmaps */
const rawLightmap_t& alm = rawLightmaps[ a ];
const rawLightmap_t& blm = rawLightmaps[ b ];
/* get lightmaps */ /* get min number of surfaces */
alm = &rawLightmaps[ *( (const int*) a ) ]; const int min = std::min( alm.numLightSurfaces, blm.numLightSurfaces );
blm = &rawLightmaps[ *( (const int*) b ) ];
/* get min number of surfaces */ /* iterate */
min = std::min( alm->numLightSurfaces, blm->numLightSurfaces ); for ( int i = 0; i < min; i++ )
{
/* get surface info */
const surfaceInfo_t& aInfo = surfaceInfos[ lightSurfaces[ alm.firstLightSurface + i ] ];
const surfaceInfo_t& bInfo = surfaceInfos[ lightSurfaces[ blm.firstLightSurface + i ] ];
//#define allocate_bigger_first /* compare shader names */
#ifdef allocate_bigger_first diff = strcmp( aInfo.si->shader, bInfo.si->shader );
/* compare size, allocate bigger first */ if ( diff != 0 ) {
// fastAllocate commit part: can kick fps by unique lightmap/shader combinations*=~2 + bigger compile time return diff < 0;
//return -diff; makes packing faster and rough }
diff = ( blm->w * blm->h ) - ( alm->w * alm->h );
if ( diff != 0 ) {
return diff;
}
#endif
/* iterate */
for ( i = 0; i < min; i++ )
{
/* get surface info */
aInfo = &surfaceInfos[ lightSurfaces[ alm->firstLightSurface + i ] ];
bInfo = &surfaceInfos[ lightSurfaces[ blm->firstLightSurface + i ] ];
/* compare shader names */
diff = strcmp( aInfo->si->shader, bInfo->si->shader );
if ( diff != 0 ) {
return diff;
} }
}
/* test style count */ /* test style count */
diff = 0; diff = 0;
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) for ( int i = 0; i < MAX_LIGHTMAPS; i++ )
diff += blm->styles[ i ] - alm->styles[ i ]; diff += blm.styles[ i ] - alm.styles[ i ];
if ( diff ) { if ( diff != 0 ) {
return diff; return diff < 0;
}
/* compare size */
diff = ( blm.w * blm.h ) - ( alm.w * alm.h );
if ( diff != 0 ) {
return diff < 0;
}
/* must be equivalent */
return false;
} }
#ifndef allocate_bigger_first };
/* compare size */
diff = ( blm->w * blm->h ) - ( alm->w * alm->h );
if ( diff != 0 ) {
return diff;
}
#endif
/* must be equivalent */
return 0;
}
void FillOutLightmap( outLightmap_t *olm ){ void FillOutLightmap( outLightmap_t *olm ){
int x, y; int x, y;
@ -2947,7 +2923,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){
/* fill it out and sort it */ /* fill it out and sort it */
for ( i = 0; i < numRawLightmaps; i++ ) for ( i = 0; i < numRawLightmaps; i++ )
sortLightmaps[ i ] = i; sortLightmaps[ i ] = i;
qsort( sortLightmaps, numRawLightmaps, sizeof( int ), CompareRawLightmap ); std::sort( sortLightmaps, sortLightmaps + numRawLightmaps, CompareRawLightmap() );
Sys_Printf( "%d.", (int) ( I_FloatTime() - timer_start ) ); Sys_Printf( "%d.", (int) ( I_FloatTime() - timer_start ) );

View File

@ -1670,91 +1670,91 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles,
/* /*
CompareMetaTriangles() CompareMetaTriangles
compare function for qsort() compare functor for std::sort()
*/ */
static int CompareMetaTriangles( const void *a_, const void *b_ ){ struct CompareMetaTriangles
auto a = reinterpret_cast<const metaTriangle_t *>( a_ ); {
auto b = reinterpret_cast<const metaTriangle_t *>( b_ ); bool operator()( const metaTriangle_t& a, const metaTriangle_t& b ) const {
/* shader first */
if ( a.si < b.si ) {
return false;
}
else if ( a.si > b.si ) {
return true;
}
/* shader first */ /* then fog */
if ( a->si < b->si ) { else if ( a.fogNum < b.fogNum ) {
return 1; return false;
} }
else if ( a->si > b->si ) { else if ( a.fogNum > b.fogNum ) {
return -1; return true;
} }
/* then fog */ /* then plane */
else if ( a->fogNum < b->fogNum ) { #if 0
return 1; else if ( npDegrees == 0.0f && !a.si->nonplanar &&
} a.planeNum >= 0 && a.planeNum >= 0 ) {
else if ( a->fogNum > b->fogNum ) { if ( a.plane.dist() < b.plane.dist() ) {
return -1; return false;
} }
else if ( a.plane.dist() > b.plane.dist() ) {
return true;
}
else if ( a.plane.normal()[ 0 ] < b.plane.normal()[ 0 ] ) {
return false;
}
else if ( a.plane.normal()[ 0 ] > b.plane.normal()[ 0 ] ) {
return true;
}
else if ( a.plane.normal()[ 1 ] < b.plane.normal()[ 1 ] ) {
return false;
}
else if ( a.plane.normal()[ 1 ] > b.plane.normal()[ 1 ] ) {
return true;
}
else if ( a.plane.normal()[ 2 ] < b.plane.normal()[ 2 ] ) {
return false;
}
else if ( a.plane.normal()[ 2 ] > b.plane.normal()[ 2 ] ) {
return true;
}
}
#endif
/* then plane */ /* then position in world */
#if 0
else if ( npDegrees == 0.0f && !a->si->nonplanar &&
a->planeNum >= 0 && a->planeNum >= 0 ) {
if ( a->plane.dist() < b->plane.dist() ) {
return 1;
}
else if ( a->plane.dist() > b->plane.dist() ) {
return -1;
}
else if ( a->plane.normal()[ 0 ] < b->plane.normal()[ 0 ] ) {
return 1;
}
else if ( a->plane.normal()[ 0 ] > b->plane.normal()[ 0 ] ) {
return -1;
}
else if ( a->plane.normal()[ 1 ] < b->plane.normal()[ 1 ] ) {
return 1;
}
else if ( a->plane.normal()[ 1 ] > b->plane.normal()[ 1 ] ) {
return -1;
}
else if ( a->plane.normal()[ 2 ] < b->plane.normal()[ 2 ] ) {
return 1;
}
else if ( a->plane.normal()[ 2 ] > b->plane.normal()[ 2 ] ) {
return -1;
}
}
#endif
/* then position in world */ /* find mins */
Vector3 aMins( 999999, 999999, 999999 );
/* find mins */ Vector3 bMins( 999999, 999999, 999999 );
Vector3 aMins( 999999, 999999, 999999 ); for ( int i = 0; i < 3; i++ )
Vector3 bMins( 999999, 999999, 999999 );
for ( int i = 0; i < 3; i++ )
{
const int av = a->indexes[ i ];
const int bv = b->indexes[ i ];
for ( int j = 0; j < 3; j++ )
{ {
value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] ); const int av = a.indexes[ i ];
value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] ); const int bv = b.indexes[ i ];
for ( int j = 0; j < 3; j++ )
{
value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] );
value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] );
}
} }
}
/* test it */ /* test it */
for ( int i = 0; i < 3; i++ ) for ( int i = 0; i < 3; i++ )
{ {
if ( aMins[ i ] < bMins[ i ] ) { if ( aMins[ i ] < bMins[ i ] ) {
return 1; return false;
}
else if ( aMins[ i ] > bMins[ i ] ) {
return true;
}
} }
else if ( aMins[ i ] > bMins[ i ] ) {
return -1;
}
}
/* functionally equivalent */ /* functionally equivalent */
return 0; return false;
} }
};
@ -1777,7 +1777,7 @@ void MergeMetaTriangles( void ){
Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" ); Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" );
/* sort the triangles by shader major, fognum minor */ /* sort the triangles by shader major, fognum minor */
qsort( metaTriangles, numMetaTriangles, sizeof( metaTriangle_t ), CompareMetaTriangles ); std::sort( metaTriangles, metaTriangles + numMetaTriangles, CompareMetaTriangles() );
/* init pacifier */ /* init pacifier */
fOld = -1; fOld = -1;

View File

@ -557,29 +557,6 @@ bool FixBrokenSurface( mapDrawSurface_t *ds ){
/*
================
EdgeCompare
================
*/
int EdgeCompare( const void *elem1, const void *elem2 ) {
const float d1 = reinterpret_cast<const originalEdge_t *>( elem1 )->length;
const float d2 = reinterpret_cast<const originalEdge_t *>( elem2 )->length;
if ( d1 < d2 ) {
return -1;
}
if ( d1 > d2 ) {
return 1;
}
return 0;
}
/* /*
FixTJunctions FixTJunctions
call after the surface list has been pruned call after the surface list has been pruned
@ -637,7 +614,9 @@ void FixTJunctions( entity_t *ent ){
axialEdgeLines = numEdgeLines; axialEdgeLines = numEdgeLines;
// sort the non-axial edges by length // sort the non-axial edges by length
qsort( originalEdges, numOriginalEdges, sizeof( originalEdges[0] ), EdgeCompare ); std::sort( originalEdges, originalEdges + numOriginalEdges, []( const originalEdge_t& a, const originalEdge_t& b ){
return a.length < b.length;
} );
// add the non-axial edges, longest first // add the non-axial edges, longest first
// this gives the most accurate edge description // this gives the most accurate edge description

View File

@ -81,25 +81,15 @@ void prl( leaf_t *l ){
the earlier information. the earlier information.
============= =============
*/ */
int PComp( const void *a, const void *b ){
if ( ( *(const vportal_t *const *)a )->nummightsee == ( *(const vportal_t *const *)b )->nummightsee ) {
return 0;
}
if ( ( *(const vportal_t *const *)a )->nummightsee < ( *(const vportal_t *const *)b )->nummightsee ) {
return -1;
}
return 1;
}
void SortPortals( void ){ void SortPortals( void ){
int i; for ( int i = 0 ; i < numportals * 2 ; i++ )
for ( i = 0 ; i < numportals * 2 ; i++ )
sorted_portals[i] = &portals[i]; sorted_portals[i] = &portals[i];
if ( nosort ) { if ( !nosort ) {
return; std::sort( sorted_portals, sorted_portals + numportals * 2, []( vportal_t* const & a, vportal_t* const & b ){
return a->nummightsee < b->nummightsee;
} );
} }
qsort( sorted_portals, numportals * 2, sizeof( sorted_portals[0] ), PComp );
} }