Skip to content

Commit

Permalink
Merge pull request #10804 from rouault/use_std_sort
Browse files Browse the repository at this point in the history
[Lint] Use std::sort() instead of qsort()
  • Loading branch information
rouault authored Sep 16, 2024
2 parents 68c0dbc + 06e7a7b commit 5b9e9e7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 72 deletions.
32 changes: 12 additions & 20 deletions alg/gdalwarpoperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,22 +832,6 @@ void GDALDestroyWarpOperation(GDALWarpOperationH hOperation)
/* CollectChunkList() */
/************************************************************************/

static int OrderWarpChunk(const void *_a, const void *_b)
{
const GDALWarpChunk *a = static_cast<const GDALWarpChunk *>(_a);
const GDALWarpChunk *b = static_cast<const GDALWarpChunk *>(_b);
if (a->dy < b->dy)
return -1;
else if (a->dy > b->dy)
return 1;
else if (a->dx < b->dx)
return -1;
else if (a->dx > b->dx)
return 1;
else
return 0;
}

void GDALWarpOperation::CollectChunkList(int nDstXOff, int nDstYOff,
int nDstXSize, int nDstYSize)

Expand All @@ -859,10 +843,18 @@ void GDALWarpOperation::CollectChunkList(int nDstXOff, int nDstYOff,
CollectChunkListInternal(nDstXOff, nDstYOff, nDstXSize, nDstYSize);

// Sort chunks from top to bottom, and for equal y, from left to right.
// TODO(schwehr): Use std::sort.
if (pasChunkList)
qsort(pasChunkList, nChunkListCount, sizeof(GDALWarpChunk),
OrderWarpChunk);
if (nChunkListCount > 1)
{
std::sort(pasChunkList, pasChunkList + nChunkListCount,
[](const GDALWarpChunk &a, const GDALWarpChunk &b)
{
if (a.dy < b.dy)
return true;
if (a.dy > b.dy)
return false;
return a.dx < b.dx;
});
}

/* -------------------------------------------------------------------- */
/* Find the global source window. */
Expand Down
20 changes: 2 additions & 18 deletions ogr/ogrfeaturequery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "ogr_swq.h"

#include <cstddef>
#include <cstdlib>
#include <algorithm>

#include "cpl_conv.h"
Expand Down Expand Up @@ -407,18 +406,6 @@ int OGRFeatureQuery::CanUseIndex(const swq_expr_node *psExpr, OGRLayer *poLayer)
/* multi-part queries with ranges. */
/************************************************************************/

static int CompareGIntBig(const void *pa, const void *pb)
{
const GIntBig a = *(reinterpret_cast<const GIntBig *>(pa));
const GIntBig b = *(reinterpret_cast<const GIntBig *>(pb));
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}

GIntBig *OGRFeatureQuery::EvaluateAgainstIndices(OGRLayer *poLayer,
OGRErr *peErr)

Expand Down Expand Up @@ -668,8 +655,7 @@ GIntBig *OGRFeatureQuery::EvaluateAgainstIndices(const swq_expr_node *psExpr,
if (nFIDCount > 1)
{
// The returned FIDs are expected to be in sorted order.
qsort(panFIDs, static_cast<size_t>(nFIDCount), sizeof(GIntBig),
CompareGIntBig);
std::sort(panFIDs, panFIDs + nFIDCount);
}
return panFIDs;
}
Expand Down Expand Up @@ -712,9 +698,7 @@ GIntBig *OGRFeatureQuery::EvaluateAgainstIndices(const swq_expr_node *psExpr,
if (nFIDCount > 1)
{
// The returned FIDs are expected to be sorted.
// TODO(schwehr): Use std::sort.
qsort(panFIDs, static_cast<size_t>(nFIDCount), sizeof(GIntBig),
CompareGIntBig);
std::sort(panFIDs, panFIDs + nFIDCount);
}
return panFIDs;
}
Expand Down
23 changes: 4 additions & 19 deletions ogr/ogrsf_frmts/gml/gmlhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "gmlreader.h"
#include "gmlreaderp.h"

#include <algorithm>
#include <climits>
#include <cstddef>
#include <cstdlib>
Expand Down Expand Up @@ -486,23 +487,6 @@ struct _GeometryNamesStruct
const char *pszName;
};

/************************************************************************/
/* GMLHandlerSortGeometryElements() */
/************************************************************************/

static int GMLHandlerSortGeometryElements(const void *pAIn, const void *pBIn)
{
const GeometryNamesStruct *pA =
static_cast<const GeometryNamesStruct *>(pAIn);
const GeometryNamesStruct *pB =
static_cast<const GeometryNamesStruct *>(pBIn);
CPLAssert(pA->nHash != pB->nHash);
if (pA->nHash < pB->nHash)
return -1;
else
return 1;
}

/************************************************************************/
/* GMLHandler() */
/************************************************************************/
Expand All @@ -520,8 +504,9 @@ GMLHandler::GMLHandler(GMLReader *poReader)
pasGeometryNames[i].nHash =
CPLHashSetHashStr(pasGeometryNames[i].pszName);
}
qsort(pasGeometryNames, GML_GEOMETRY_TYPE_COUNT,
sizeof(GeometryNamesStruct), GMLHandlerSortGeometryElements);
std::sort(pasGeometryNames, pasGeometryNames + GML_GEOMETRY_TYPE_COUNT,
[](const GeometryNamesStruct &a, const GeometryNamesStruct &b)
{ return a.nHash < b.nHash; });

stateStack[0] = STATE_TOP;
}
Expand Down
22 changes: 7 additions & 15 deletions port/cplstringlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ char **CPLStringList::StealList()
return papszRetList;
}

/* Case insensitive comparison function */
static int CPLCompareKeyValueString(const char *pszKVa, const char *pszKVb)
{
const char *pszItera = pszKVa;
Expand Down Expand Up @@ -685,19 +686,6 @@ static int CPLCompareKeyValueString(const char *pszKVa, const char *pszKVb)
}
}

/************************************************************************/
/* llCompareStr() */
/* */
/* Note this is case insensitive! This is because we normally */
/* treat key value keywords as case insensitive. */
/************************************************************************/
static int llCompareStr(const void *a, const void *b)
{
return CPLCompareKeyValueString(
*static_cast<const char **>(const_cast<void *>(a)),
*static_cast<const char **>(const_cast<void *>(b)));
}

/************************************************************************/
/* Sort() */
/************************************************************************/
Expand All @@ -721,8 +709,12 @@ CPLStringList &CPLStringList::Sort()
if (!MakeOurOwnCopy())
return *this;

if (nCount)
qsort(papszList, nCount, sizeof(char *), llCompareStr);
if (nCount > 1)
{
std::sort(papszList, papszList + nCount,
[](const char *a, const char *b)
{ return CPLCompareKeyValueString(a, b) < 0; });
}
bIsSorted = true;

return *this;
Expand Down

0 comments on commit 5b9e9e7

Please sign in to comment.