Skip to content

Commit

Permalink
GMLAS: use cpl::set and cpl::map
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 12, 2024
1 parent 457ba22 commit 8daa64f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
10 changes: 6 additions & 4 deletions ogr/ogrsf_frmts/gmlas/ogr_gmlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#ifndef OGR_GMLAS_INCLUDED
#define OGR_GMLAS_INCLUDED

#include "cpl_std_ext.h"

#include "xercesc_headers.h"
#include "ogr_xerces.h"

Expand Down Expand Up @@ -398,7 +400,7 @@ class GMLASConfiguration
bool m_bExposeMetadataLayers;

/** For flatening rules, map prefix namespace to its URI */
std::map<CPLString, CPLString> m_oMapPrefixToURIFlatteningRules;
cpl::map<CPLString, CPLString> m_oMapPrefixToURIFlatteningRules;

std::vector<CPLString> m_osForcedFlattenedXPath;

Expand All @@ -421,13 +423,13 @@ class GMLASConfiguration
bool m_bSWEProcessDataArray;

/** For ignored xpaths, map prefix namespace to its URI */
std::map<CPLString, CPLString> m_oMapPrefixToURIIgnoredXPaths;
cpl::map<CPLString, CPLString> m_oMapPrefixToURIIgnoredXPaths;

/** Ignored xpaths */
std::vector<CPLString> m_aosIgnoredXPaths;

/** For type constraints, map prefix namespace to its URI */
std::map<CPLString, CPLString> m_oMapPrefixToURITypeConstraints;
cpl::map<CPLString, CPLString> m_oMapPrefixToURITypeConstraints;

/** Map an XPath to a list of potential types for its children */
std::map<CPLString, std::vector<CPLString>>
Expand Down Expand Up @@ -1119,7 +1121,7 @@ class GMLASSchemaAnalyzer
const CPLString &osParentXPath, XSModelGroup *poModelGroup,
int nRecursionCounter,
std::set<XSElementDeclaration *> &oSetVisitedEltDecl,
std::set<XSModelGroup *> &oSetVisitedModelGroups,
cpl::set<XSModelGroup *> &oSetVisitedModelGroups,
std::vector<XSElementDeclaration *> &oVectorEltsForTopClass,
std::set<CPLString> &aoSetXPathEltsForTopClass, XSModel *poModel,
bool &bSimpleEnoughOut, int &nCountSubEltsOut);
Expand Down
4 changes: 2 additions & 2 deletions ogr/ogrsf_frmts/gmlas/ogrgmlasconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static void CPL_STDCALL GMLASConfigurationErrorHandler(CPLErr /*eErr*/,
/************************************************************************/

static void ParseNamespaces(CPLXMLNode *psContainerNode,
std::map<CPLString, CPLString> &oMap)
cpl::map<CPLString, CPLString> &oMap)
{
CPLXMLNode *psNamespaces = CPLGetXMLNode(psContainerNode, "Namespaces");
if (psNamespaces != nullptr)
Expand All @@ -232,7 +232,7 @@ static void ParseNamespaces(CPLXMLNode *psContainerNode,
const std::string osURI = CPLGetXMLValue(psIter, "uri", "");
if (!osPrefix.empty() && !osURI.empty())
{
if (oMap.find(osPrefix) == oMap.end())
if (!oMap.contains(osPrefix))
{
oMap[osPrefix] = osURI;
}
Expand Down
30 changes: 13 additions & 17 deletions ogr/ogrsf_frmts/gmlas/ogrgmlasdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ void OGRGMLASDataSource::FillOtherMetadataLayer(
}

int nNSIdx = 1;
std::set<CPLString> oSetVisitedURI;
cpl::set<CPLString> oSetVisitedURI;
for (int i = 0; i < static_cast<int>(aoXSDs.size()); i++)
{
const CPLString osURI(aoXSDs[i].first);
Expand Down Expand Up @@ -599,12 +599,13 @@ void OGRGMLASDataSource::FillOtherMetadataLayer(
CPL_IGNORE_RET_VAL(m_poOtherMetadataLayer->CreateFeature(poFeature));
delete poFeature;

if (m_oMapURIToPrefix.find(osURI) != m_oMapURIToPrefix.end())
const auto oIter = m_oMapURIToPrefix.find(osURI);
if (oIter != m_oMapURIToPrefix.end())
{
poFeature = new OGRFeature(poFDefn);
poFeature->SetField(szKEY,
CPLSPrintf(szNAMESPACE_PREFIX_FMT, nNSIdx));
poFeature->SetField(szVALUE, m_oMapURIToPrefix[osURI].c_str());
poFeature->SetField(szVALUE, oIter->second.c_str());
CPL_IGNORE_RET_VAL(
m_poOtherMetadataLayer->CreateFeature(poFeature));
delete poFeature;
Expand All @@ -618,9 +619,9 @@ void OGRGMLASDataSource::FillOtherMetadataLayer(
const CPLString &osURI(oIter.first);
const CPLString &osPrefix(oIter.second);

if (oSetVisitedURI.find(osURI) == oSetVisitedURI.end() &&
osURI != szXML_URI && osURI != szXS_URI && osURI != szXSI_URI &&
osURI != szXMLNS_URI && osURI != szOGRGMLAS_URI)
if (!oSetVisitedURI.contains(osURI) && osURI != szXML_URI &&
osURI != szXS_URI && osURI != szXSI_URI && osURI != szXMLNS_URI &&
osURI != szOGRGMLAS_URI)
{
OGRFeature *poFeature = new OGRFeature(poFDefn);
poFeature->SetField(szKEY, CPLSPrintf(szNAMESPACE_URI_FMT, nNSIdx));
Expand Down Expand Up @@ -1319,7 +1320,7 @@ bool OGRGMLASDataSource::RunFirstPassIfNeeded(GMLASReader *poReader,
// No need to warn afterwards
m_oConf.m_oMapIgnoredXPathToWarn.clear();

std::set<CPLString> aoSetRemovedLayerNames;
cpl::set<CPLString> aoSetRemovedLayerNames;
bSuccess = poReaderFirstPass->RunFirstPass(
pfnProgress, pProgressData, m_bRemoveUnusedLayers,
m_bRemoveUnusedFields,
Expand Down Expand Up @@ -1349,8 +1350,7 @@ bool OGRGMLASDataSource::RunFirstPassIfNeeded(GMLASReader *poReader,
{
const char *pszLayerName =
poFeature->GetFieldAsString(szLAYER_NAME);
if (aoSetRemovedLayerNames.find(pszLayerName) !=
aoSetRemovedLayerNames.end())
if (aoSetRemovedLayerNames.contains(pszLayerName))
{
CPL_IGNORE_RET_VAL(m_poLayersMetadataLayer->DeleteFeature(
poFeature->GetFID()));
Expand All @@ -1367,10 +1367,8 @@ bool OGRGMLASDataSource::RunFirstPassIfNeeded(GMLASReader *poReader,
poFeature->GetFieldAsString(szLAYER_NAME);
const char *pszRelatedLayerName =
poFeature->GetFieldAsString(szFIELD_RELATED_LAYER);
if (aoSetRemovedLayerNames.find(pszLayerName) !=
aoSetRemovedLayerNames.end() ||
aoSetRemovedLayerNames.find(pszRelatedLayerName) !=
aoSetRemovedLayerNames.end())
if (aoSetRemovedLayerNames.contains(pszLayerName) ||
aoSetRemovedLayerNames.contains(pszRelatedLayerName))
{
CPL_IGNORE_RET_VAL(m_poFieldsMetadataLayer->DeleteFeature(
poFeature->GetFID()));
Expand All @@ -1387,10 +1385,8 @@ bool OGRGMLASDataSource::RunFirstPassIfNeeded(GMLASReader *poReader,
poFeature->GetFieldAsString(szPARENT_LAYER);
const char *pszChildLayerName =
poFeature->GetFieldAsString(szCHILD_LAYER);
if (aoSetRemovedLayerNames.find(pszParentLayerName) !=
aoSetRemovedLayerNames.end() ||
aoSetRemovedLayerNames.find(pszChildLayerName) !=
aoSetRemovedLayerNames.end())
if (aoSetRemovedLayerNames.contains(pszParentLayerName) ||
aoSetRemovedLayerNames.contains(pszChildLayerName))
{
CPL_IGNORE_RET_VAL(m_poRelationshipsLayer->DeleteFeature(
poFeature->GetFID()));
Expand Down
19 changes: 9 additions & 10 deletions ogr/ogrsf_frmts/gmlas/ogrgmlasschemaanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,14 +1082,14 @@ bool GMLASSchemaAnalyzer::Analyze(GMLASXSDCache &oCache,
// Find which elements must be top levels (because referenced several
// times)
std::set<XSElementDeclaration *> oSetVisitedEltDecl;
std::set<XSModelGroup *> oSetVisitedModelGroups;
cpl::set<XSModelGroup *> oSetVisitedModelGroups;
std::vector<XSElementDeclaration *> oVectorEltsForTopClass;

// For some reason, different XSElementDeclaration* can point to the
// same element, but we only want to instantiate a single class.
// This is the case for base:SpatialDataSet in
// inspire/geologicalunit/geologicalunit.gml test dataset.
std::set<CPLString> aoSetXPathEltsForTopClass;
cpl::set<CPLString> aoSetXPathEltsForTopClass;

// Third and fourth passes
for (int iPass = 0; iPass < 2; ++iPass)
Expand Down Expand Up @@ -1146,8 +1146,7 @@ bool GMLASSchemaAnalyzer::Analyze(GMLASXSDCache &oCache,
.c_str());
#endif
oSetVisitedEltDecl.insert(poEltDecl);
if (aoSetXPathEltsForTopClass.find(osXPath) ==
aoSetXPathEltsForTopClass.end())
if (!aoSetXPathEltsForTopClass.contains(osXPath))
{
m_oSetEltsForTopClass.insert(poEltDecl);
oVectorEltsForTopClass.push_back(poEltDecl);
Expand Down Expand Up @@ -1831,7 +1830,7 @@ void GMLASSchemaAnalyzer::CreateNonNestedRelationship(
apoImplEltList.insert(apoImplEltList.begin(), poElt);
}

std::set<CPLString> aoSetSubEltXPath;
cpl::set<CPLString> aoSetSubEltXPath;
if (nMaxOccurs == 1 && !bForceJunctionTable)
{
// If the field isn't repeated, then we can link to each
Expand All @@ -1845,7 +1844,7 @@ void GMLASSchemaAnalyzer::CreateNonNestedRelationship(
MakeXPath(transcode(poSubElt->getNamespace()), osSubEltName));

// For AbstractFeature_SpatialDataSet_pkid in SpatialDataSet_member
if (aoSetSubEltXPath.find(osSubEltXPath) != aoSetSubEltXPath.end())
if (aoSetSubEltXPath.contains(osSubEltXPath))
{
continue;
}
Expand Down Expand Up @@ -1911,7 +1910,7 @@ void GMLASSchemaAnalyzer::CreateNonNestedRelationship(
MakeXPath(transcode(poSubElt->getNamespace()), osSubEltName));

// For AbstractFeature_SpatialDataSet_pkid in SpatialDataSet_member
if (aoSetSubEltXPath.find(osSubEltXPath) != aoSetSubEltXPath.end())
if (aoSetSubEltXPath.contains(osSubEltXPath))
{
continue;
}
Expand Down Expand Up @@ -2031,13 +2030,13 @@ bool GMLASSchemaAnalyzer::IsIgnoredXPath(const CPLString &osXPath)
bool GMLASSchemaAnalyzer::FindElementsWithMustBeToLevel(
const CPLString &osParentXPath, XSModelGroup *poModelGroup,
int nRecursionCounter, std::set<XSElementDeclaration *> &oSetVisitedEltDecl,
std::set<XSModelGroup *> &oSetVisitedModelGroups,
cpl::set<XSModelGroup *> &oSetVisitedModelGroups,
std::vector<XSElementDeclaration *> &oVectorEltsForTopClass,
std::set<CPLString> &aoSetXPathEltsForTopClass, XSModel *poModel,
bool &bSimpleEnoughOut, int &nCountSubEltsOut)
{
const bool bAlreadyVisitedMG = (oSetVisitedModelGroups.find(poModelGroup) !=
oSetVisitedModelGroups.end());
const bool bAlreadyVisitedMG =
oSetVisitedModelGroups.contains(poModelGroup);

oSetVisitedModelGroups.insert(poModelGroup);

Expand Down
21 changes: 11 additions & 10 deletions ogr/ogrsf_frmts/gmlas/ogrgmlaswriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GMLASWriter
OGRLayer *m_poLayerRelationshipsLayer;
std::vector<LayerDescription> m_aoLayerDesc;
std::map<CPLString, int> m_oMapLayerNameToIdx;
std::map<CPLString, int> m_oMapXPathToIdx;
cpl::map<CPLString, int> m_oMapXPathToIdx;
std::map<CPLString, OGRLayer *> m_oMapLayerNameToLayer;
std::map<CPLString, XPathComponents> m_oMapXPathToComponents;
std::map<const OGRSpatialReference *, bool> m_oMapSRSToCoordSwap;
Expand Down Expand Up @@ -298,7 +298,7 @@ bool GMLASWriter::Write(GDALProgressFunc pfnProgress, void *pProgressData)
CPLString osXSDFilenames =
CSLFetchNameValueDef(m_papszOptions, szINPUT_XSD_OPTION, "");
std::vector<PairURIFilename> aoXSDs;
std::map<CPLString, CPLString> oMapURIToPrefix;
cpl::map<CPLString, CPLString> oMapURIToPrefix;
CPLString osGMLVersion;

if (!osXSDFilenames.empty())
Expand Down Expand Up @@ -388,13 +388,15 @@ bool GMLASWriter::Write(GDALProgressFunc pfnProgress, void *pProgressData)

for (int i = 1; i <= static_cast<int>(oMapToUri.size()); ++i)
{
if (oMapToUri.find(i) != oMapToUri.end())
const auto oIter = oMapToUri.find(i);
if (oIter != oMapToUri.end())
{
const CPLString &osURI(oMapToUri[i]);
const CPLString &osURI(oIter->second);
aoXSDs.push_back(PairURIFilename(osURI, oMapToLocation[i]));
if (oMapToPrefix.find(i) != oMapToPrefix.end())
const auto oIter2 = oMapToPrefix.find(i);
if (oIter2 != oMapToPrefix.end())
{
oMapURIToPrefix[osURI] = oMapToPrefix[i];
oMapURIToPrefix[osURI] = oIter2->second;
}
}
}
Expand Down Expand Up @@ -503,9 +505,9 @@ bool GMLASWriter::Write(GDALProgressFunc pfnProgress, void *pProgressData)
CPLSPrintf("%d", m_oConf.m_nIndentSize)))));
m_osIndentation.assign(nIndentSize, ' ');

if (oMapURIToPrefix.find(szGML32_URI) != oMapURIToPrefix.end() ||
if (oMapURIToPrefix.contains(szGML32_URI) ||
// Used by tests
oMapURIToPrefix.find("http://fake_gml32") != oMapURIToPrefix.end())
oMapURIToPrefix.contains("http://fake_gml32"))
{
m_osGMLVersion = "3.2.1";
}
Expand Down Expand Up @@ -917,8 +919,7 @@ bool GMLASWriter::CollectLayers()
desc.osName.c_str());
return false;
}
if (!desc.bIsJunction &&
m_oMapXPathToIdx.find(desc.osXPath) != m_oMapXPathToIdx.end())
if (!desc.bIsJunction && m_oMapXPathToIdx.contains(desc.osXPath))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Several layers with same %s = %s", szLAYER_XPATH,
Expand Down

0 comments on commit 8daa64f

Please sign in to comment.