Skip to content

Commit

Permalink
Merge pull request #10464 from rouault/cpl_contains
Browse files Browse the repository at this point in the history
Add a cpl::contains(container, value) helper
  • Loading branch information
rouault committed Jul 23, 2024
2 parents dc2a4bd + 93ec1b7 commit 3122857
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
5 changes: 2 additions & 3 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3232,7 +3232,7 @@ char **GDALDataset::GetFileList()
const GDALAntiRecursionStruct::DatasetContext datasetCtxt(osMainFilename, 0,
std::string());
auto &aosDatasetList = sAntiRecursion.aosDatasetNamesWithFlags;
if (aosDatasetList.find(datasetCtxt) != aosDatasetList.end())
if (cpl::contains(aosDatasetList, datasetCtxt))
return nullptr;

/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -3626,8 +3626,7 @@ GDALDatasetH CPL_STDCALL GDALOpenEx(const char *pszFilename,
osAllowedDrivers += pszDriverName;
auto dsCtxt = GDALAntiRecursionStruct::DatasetContext(
std::string(pszFilename), nOpenFlags, osAllowedDrivers);
if (sAntiRecursion.aosDatasetNamesWithFlags.find(dsCtxt) !=
sAntiRecursion.aosDatasetNamesWithFlags.end())
if (cpl::contains(sAntiRecursion.aosDatasetNamesWithFlags, dsCtxt))
{
CPLError(CE_Failure, CPLE_AppDefined,
"GDALOpen() called on %s recursively", pszFilename);
Expand Down
7 changes: 3 additions & 4 deletions gcore/gdaldriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,7 @@ CPLErr GDALDriver::QuietDeleteForCreateCopy(const char *pszFilename,
{
CPLString osFilename(pszFileInList);
osFilename.replaceAll('\\', '/');
if (oSetExistingDestFiles.find(osFilename) !=
oSetExistingDestFiles.end())
if (cpl::contains(oSetExistingDestFiles, osFilename))
{
oSetExistingDestFilesFoundInSource.insert(osFilename);
}
Expand All @@ -1078,8 +1077,8 @@ CPLErr GDALDriver::QuietDeleteForCreateCopy(const char *pszFilename,
{
for (const std::string &osFilename : oSetExistingDestFiles)
{
if (oSetExistingDestFilesFoundInSource.find(osFilename) ==
oSetExistingDestFilesFoundInSource.end())
if (!cpl::contains(oSetExistingDestFilesFoundInSource,
osFilename))
{
VSIUnlink(osFilename.c_str());
}
Expand Down
17 changes: 6 additions & 11 deletions gcore/gdaldrivermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden)

if (m_bInDeferredDriverLoading)
{
if (m_oMapRealDrivers.find(poDriver->GetDescription()) !=
m_oMapRealDrivers.end())
if (cpl::contains(m_oMapRealDrivers, poDriver->GetDescription()))
{
CPLError(
CE_Failure, CPLE_AppDefined,
Expand Down Expand Up @@ -1032,8 +1031,7 @@ void GDALDriverManager::AutoLoadDrivers()
continue;
}

if (m_oSetPluginFileNames.find(papszFiles[iFile]) !=
m_oSetPluginFileNames.end())
if (cpl::contains(m_oSetPluginFileNames, papszFiles[iFile]))
{
continue;
}
Expand Down Expand Up @@ -1157,14 +1155,12 @@ void GDALDriverManager::ReorderDrivers()
{
CPLString osUCDriverName(pszLine);
osUCDriverName.toupper();
if (oSetOrderedDrivers.find(osUCDriverName) !=
oSetOrderedDrivers.end())
if (cpl::contains(oSetOrderedDrivers, osUCDriverName))
{
CPLError(CE_Warning, CPLE_AppDefined,
"Duplicated name %s in [order] section", pszLine);
}
else if (oMapNameToDrivers.find(osUCDriverName) !=
oMapNameToDrivers.end())
else if (cpl::contains(oMapNameToDrivers, osUCDriverName))
{
aosOrderedDrivers.emplace_back(pszLine);
oSetOrderedDrivers.insert(osUCDriverName);
Expand All @@ -1189,8 +1185,7 @@ void GDALDriverManager::ReorderDrivers()
for (int i = 0; i < nDrivers; ++i)
{
const char *pszName = papoDrivers[i]->GetDescription();
if (oSetOrderedDrivers.find(CPLString(pszName).toupper()) ==
oSetOrderedDrivers.end())
if (!cpl::contains(oSetOrderedDrivers, CPLString(pszName).toupper()))
{
// Could happen for a private plugin
CPLDebug("GDAL",
Expand Down Expand Up @@ -1342,7 +1337,7 @@ const char *GDALPluginDriverProxy::GetMetadataItem(const char *pszName,
}
return pszValue;
}
else if (m_oSetMetadataItems.find(pszName) != m_oSetMetadataItems.end())
else if (cpl::contains(m_oSetMetadataItems, pszName))
{
return GDALDriver::GetMetadataItem(pszName, pszDomain);
}
Expand Down
4 changes: 1 addition & 3 deletions gcore/gdaljp2metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ int GDALJP2Metadata::ReadAndParse(VSILFILE *fpLL, int nGEOJP2Index,
aoSetPriorities.insert(nGMLJP2Index);
if (nMSIGIndex >= 0)
aoSetPriorities.insert(nMSIGIndex);
std::set<int>::iterator oIter = aoSetPriorities.begin();
for (; oIter != aoSetPriorities.end(); ++oIter)
for (const int nIndex : aoSetPriorities)
{
int nIndex = *oIter;
if ((nIndex == nGEOJP2Index && ParseJP2GeoTIFF()) ||
(nIndex == nGMLJP2Index && ParseGMLCoverageDesc()) ||
(nIndex == nMSIGIndex && ParseMSIG()))
Expand Down
23 changes: 10 additions & 13 deletions gcore/gdalmultidim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,8 @@ bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup,
'_' + dim->GetName());
newDimName = newDimNamePrefix;
int nIterCount = 2;
while (mapExistingDstDims.find(newDimName) !=
mapExistingDstDims.end())
while (
cpl::contains(mapExistingDstDims, newDimName))
{
newDimName = newDimNamePrefix +
CPLSPrintf("_%d", nIterCount);
Expand Down Expand Up @@ -1148,9 +1148,8 @@ bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup,
auto srcArray = poSrcGroup->OpenMDArray(name);
EXIT_OR_CONTINUE_IF_NULL(srcArray);

const auto oIterDimName =
mapSrcVariableNameToIndexedDimName.find(srcArray->GetName());
if (oIterDimName != mapSrcVariableNameToIndexedDimName.end())
if (cpl::contains(mapSrcVariableNameToIndexedDimName,
srcArray->GetName()))
{
if (!CopyArray(srcArray))
return false;
Expand All @@ -1163,9 +1162,8 @@ bool GDALGroup::CopyFrom(const std::shared_ptr<GDALGroup> &poDstRootGroup,
auto srcArray = poSrcGroup->OpenMDArray(name);
EXIT_OR_CONTINUE_IF_NULL(srcArray);

const auto oIterDimName =
mapSrcVariableNameToIndexedDimName.find(srcArray->GetName());
if (oIterDimName == mapSrcVariableNameToIndexedDimName.end())
if (!cpl::contains(mapSrcVariableNameToIndexedDimName,
srcArray->GetName()))
{
if (!CopyArray(srcArray))
return false;
Expand Down Expand Up @@ -1308,8 +1306,8 @@ GDALGroup::ResolveMDArray(const std::string &osName,
GetInnerMostGroup(osPath, curGroupHolder, osLastPart);
if (poGroupPtr)
poGroup = poGroupPtr->OpenGroup(osLastPart);
if (poGroup && oSetAlreadyVisited.find(poGroup->GetFullName()) ==
oSetAlreadyVisited.end())
if (poGroup &&
!cpl::contains(oSetAlreadyVisited, poGroup->GetFullName()))
{
oQueue.push(poGroup);
goOn = true;
Expand Down Expand Up @@ -1340,9 +1338,8 @@ GDALGroup::ResolveMDArray(const std::string &osName,
for (const auto &osGroupName : aosGroupNames)
{
auto poSubGroup = groupPtr->OpenGroup(osGroupName);
if (poSubGroup &&
oSetAlreadyVisited.find(poSubGroup->GetFullName()) ==
oSetAlreadyVisited.end())
if (poSubGroup && !cpl::contains(oSetAlreadyVisited,
poSubGroup->GetFullName()))
{
oQueue.push(poSubGroup);
oSetAlreadyVisited.insert(poSubGroup->GetFullName());
Expand Down
8 changes: 8 additions & 0 deletions port/cpl_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,14 @@ extern "C++"
{
return t;
}

/** Emulates the C++20 .contains() method */
template <typename C, typename V>
inline bool contains(const C &container, const V &value)
{
return container.count(value) != 0;
}

} // namespace cpl
}
#endif
Expand Down

0 comments on commit 3122857

Please sign in to comment.