Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lint] Use std::sort() instead of qsort() #10804

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading