From 21cc71ced3565585f7a2f94875ed845355ab2064 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 4 Sep 2016 16:55:29 +0200 Subject: Eradicate Java-style loops (I): QHashIterator -> STL-style Java-style iterators are slower than STL-style ones, so they should not be used in library code. Replaced them with STL iterator loops. In one case, a QMutableHashIterator actually needn't be mutable, so ported to const_iterator, like all others. Change-Id: Ib7fd1fa5fca2df2c288a61925ee68a5df11caf62 Reviewed-by: Friedemann Kleint --- src/xmlpatterns/acceltree/qacceltree.cpp | 6 +-- src/xmlpatterns/expr/qexpressionfactory.cpp | 18 ++----- src/xmlpatterns/schema/qnamespacesupport.cpp | 5 +- src/xmlpatterns/schema/qxsdfacet_p.h | 1 - src/xmlpatterns/schema/qxsdparticlechecker.cpp | 27 +++-------- src/xmlpatterns/schema/qxsdschemachecker.cpp | 16 ++----- src/xmlpatterns/schema/qxsdstatemachine_tpl_p.h | 56 +++++++--------------- src/xmlpatterns/schema/qxsdtypechecker.cpp | 6 +-- .../schema/qxsdvalidatinginstancereader.cpp | 11 ++--- 9 files changed, 38 insertions(+), 108 deletions(-) diff --git a/src/xmlpatterns/acceltree/qacceltree.cpp b/src/xmlpatterns/acceltree/qacceltree.cpp index c68c3a4..1892745 100644 --- a/src/xmlpatterns/acceltree/qacceltree.cpp +++ b/src/xmlpatterns/acceltree/qacceltree.cpp @@ -122,11 +122,7 @@ void AccelTree::printStats(const NamePool::Ptr &np) const pDebug() << "+---------------+-------+-------+---------------+-------+--------------+"; pDebug() << "Namespaces(" << namespaces.count() << "):"; - QHashIterator > it(namespaces); - while(it.hasNext()) - { - it.next(); - + for (auto it = namespaces.cbegin(), end = namespaces.cend(); it != end; ++it) { pDebug() << "PreNumber: " << QString::number(it.key()); for(int i = 0; i < it.value().count(); ++i) pDebug() << "\t\t" << np->stringForPrefix(it.value().at(i).prefix()) << " = " << np->stringForNamespace(it.value().at(i).namespaceURI()); diff --git a/src/xmlpatterns/expr/qexpressionfactory.cpp b/src/xmlpatterns/expr/qexpressionfactory.cpp index f205def..b0e43be 100644 --- a/src/xmlpatterns/expr/qexpressionfactory.cpp +++ b/src/xmlpatterns/expr/qexpressionfactory.cpp @@ -279,11 +279,7 @@ Expression::Ptr ExpressionFactory::createExpression(const Tokenizer::Ptr &tokeni { pDebug() << "Have " << info->namedTemplates.count() << "named templates"; - QMutableHashIterator it(info->namedTemplates); - - while(it.hasNext()) - { - it.next(); + for (auto it = info->namedTemplates.cbegin(), end = info->namedTemplates.cend(); it != end; ++it) { processNamedTemplate(it.key(), it.value()->body, TemplateInitial); it.value()->body = it.value()->body->typeCheck(context, CommonSequenceTypes::ZeroOrMoreItems); @@ -298,7 +294,6 @@ Expression::Ptr ExpressionFactory::createExpression(const Tokenizer::Ptr &tokeni /* Type check and compress template rules. */ { - QHashIterator it(info->templateRules); /* Since a pattern can exist of AxisStep, its typeCheck() stage * requires a focus. In the case that we're invoked with a name but @@ -313,9 +308,7 @@ Expression::Ptr ExpressionFactory::createExpression(const Tokenizer::Ptr &tokeni patternContext = StaticContext::Ptr(new StaticFocusContext(BuiltinTypes::node, context)); /* For each template pattern. */ - while(it.hasNext()) - { - it.next(); + for (auto it = info->templateRules.cbegin(), end = info->templateRules.cend(); it != end; ++it) { const TemplateMode::Ptr &mode = it.value(); const int len = mode->templatePatterns.count(); TemplatePattern::ID currentTemplateID = -1; @@ -387,12 +380,7 @@ Expression::Ptr ExpressionFactory::createExpression(const Tokenizer::Ptr &tokeni Q_ASSERT_X(modeAll, Q_FUNC_INFO, "We should at least have the builtin templates."); - QHashIterator it(info->templateRules); - - while(it.hasNext()) - { - it.next(); - + for (auto it = info->templateRules.cbegin(), end = info->templateRules.cend(); it != end; ++it) { /* Don't add mode #all to mode #all. */ if(it.key() == nameModeAll) continue; diff --git a/src/xmlpatterns/schema/qnamespacesupport.cpp b/src/xmlpatterns/schema/qnamespacesupport.cpp index 758f6d5..846551b 100644 --- a/src/xmlpatterns/schema/qnamespacesupport.cpp +++ b/src/xmlpatterns/schema/qnamespacesupport.cpp @@ -146,11 +146,8 @@ QList NamespaceSupport::namespaceBindings() const { QList bindings; - QHashIterator it(m_ns); - while (it.hasNext()) { - it.next(); + for (auto it = m_ns.cbegin(), end = m_ns.cend(); it != end; ++it) bindings.append(QXmlName(it.value(), StandardLocalNames::empty, it.key())); - } return bindings; } diff --git a/src/xmlpatterns/schema/qxsdfacet_p.h b/src/xmlpatterns/schema/qxsdfacet_p.h index 8dd7d5f..76919d1 100644 --- a/src/xmlpatterns/schema/qxsdfacet_p.h +++ b/src/xmlpatterns/schema/qxsdfacet_p.h @@ -112,7 +112,6 @@ namespace QPatternist Assertion = 1 << 12, ///< Match an assertion (Assertion Definition) }; typedef QHash Hash; - typedef QHashIterator HashIterator; /** * Creates a new facet object of type None. diff --git a/src/xmlpatterns/schema/qxsdparticlechecker.cpp b/src/xmlpatterns/schema/qxsdparticlechecker.cpp index 59fc111..139390d 100644 --- a/src/xmlpatterns/schema/qxsdparticlechecker.cpp +++ b/src/xmlpatterns/schema/qxsdparticlechecker.cpp @@ -392,25 +392,17 @@ bool XsdParticleChecker::isUPAConform(const XsdParticle::Ptr &particle, const Na // the basic idea of that algorithm is to iterate over all states of that machine and check that no two edges // that match on the same term leave a state, so for a given term it should always be obvious which edge to take - QHashIterator::StateId, XsdStateMachine::StateType> stateIt(states); - while (stateIt.hasNext()) { // iterate over all states - stateIt.next(); - + for (auto stateIt = states.cbegin(), end = states.cend(); stateIt != end; ++stateIt) { // fetch all transitions the current state allows const QHash::StateId> > currentTransitions = transitions.value(stateIt.key()); - QHashIterator::StateId> > transitionIt(currentTransitions); - while (transitionIt.hasNext()) { // iterate over all transitions - transitionIt.next(); - + for (auto transitionIt = currentTransitions.cbegin(), end = currentTransitions.cend(); transitionIt != end; ++transitionIt) { if (transitionIt.value().size() > 1) { // we have one state with two edges leaving it, that means // the XsdTerm::Ptr exists twice, that is an error return false; } - QHashIterator::StateId> > innerTransitionIt(currentTransitions); - while (innerTransitionIt.hasNext()) { // iterate over all transitions again, as we have to compare all transitions with all - innerTransitionIt.next(); + for (auto innerTransitionIt = currentTransitions.cbegin(), end = currentTransitions.cend(); innerTransitionIt != end; ++innerTransitionIt) { if (transitionIt.key() == innerTransitionIt.key()) // do no compare with ourself continue; @@ -516,17 +508,13 @@ bool XsdParticleChecker::subsumes(const XsdParticle::Ptr &particle, const XsdPar const QPair::StateId, XsdStateMachine::StateId> set = workSet.takeFirst(); const QHash::StateId> > derivedTrans = derivedTransitions.value(set.second); - QHashIterator::StateId> > derivedIt(derivedTrans); const QHash::StateId> > baseTrans = baseTransitions.value(set.first); - while (derivedIt.hasNext()) { - derivedIt.next(); + for (auto derivedIt = derivedTrans.cbegin(), end = derivedTrans.cend(); derivedIt != end; ++derivedIt) { bool found = false; - QHashIterator::StateId> > baseIt(baseTrans); - while (baseIt.hasNext()) { - baseIt.next(); + for (auto baseIt = baseTrans.cbegin(), end = baseTrans.cend(); baseIt != end; ++baseIt) { if (derivedTermValid(baseIt.key(), derivedIt.key(), particlesHash, context, errorMsg)) { const QPair::StateId, XsdStateMachine::StateId> endSet = qMakePair::StateId, XsdStateMachine::StateId>(baseIt.value().first(), derivedIt.value().first()); @@ -546,10 +534,7 @@ bool XsdParticleChecker::subsumes(const XsdParticle::Ptr &particle, const XsdPar } // 5) - QHashIterator::StateId, XsdStateMachine::StateType> it(derivedStates); - while (it.hasNext()) { - it.next(); - + for (auto it = derivedStates.cbegin(), end = derivedStates.cend(); it != end; ++it) { if (it.value() == XsdStateMachine::EndState || it.value() == XsdStateMachine::StartEndState) { for (int i = 0; i < processedSet.count(); ++i) { if (processedSet.at(i).second == it.key() && diff --git a/src/xmlpatterns/schema/qxsdschemachecker.cpp b/src/xmlpatterns/schema/qxsdschemachecker.cpp index 0898848..5e4302d 100644 --- a/src/xmlpatterns/schema/qxsdschemachecker.cpp +++ b/src/xmlpatterns/schema/qxsdschemachecker.cpp @@ -436,11 +436,9 @@ void XsdSchemaChecker::checkSimpleTypeConstraints() // 2.2.1.2 const XsdFacet::Hash facets = simpleType->facets(); - XsdFacet::HashIterator it(facets); bool invalidFacetFound = false; - while (it.hasNext()) { - it.next(); + for (auto it = facets.cbegin(), end = facets.cend(); it != end; ++it) { if (it.key() != XsdFacet::WhiteSpace) { invalidFacetFound = true; break; @@ -476,12 +474,10 @@ void XsdSchemaChecker::checkSimpleTypeConstraints() // 2.2.2.4 const XsdFacet::Hash facets = simpleType->facets(); - XsdFacet::HashIterator it(facets); bool invalidFacetFound = false; XsdFacet::Type invalidFacetType = XsdFacet::None; - while (it.hasNext()) { - it.next(); + for (auto it = facets.cbegin(), end = facets.cend(); it != end; ++it) { const XsdFacet::Type facetType = it.key(); if (facetType != XsdFacet::Length && facetType != XsdFacet::MinimumLength && @@ -573,12 +569,10 @@ void XsdSchemaChecker::checkSimpleTypeConstraints() // 3.1.2.4 const XsdFacet::Hash facets = simpleType->facets(); - XsdFacet::HashIterator it(facets); bool invalidFacetFound = false; XsdFacet::Type invalidFacetType = XsdFacet::None; - while (it.hasNext()) { - it.next(); + for (auto it = facets.cbegin(), end = facets.cend(); it != end; ++it) { const XsdFacet::Type facetType = it.key(); if (facetType != XsdFacet::Pattern && facetType != XsdFacet::Enumeration) { @@ -1573,9 +1567,7 @@ void XsdSchemaChecker::checkConstrainingFacets(const XsdFacet::Hash &facets, con if (!baseType->isDefinedBySchema()) { const XsdSchemaSourceLocationReflection reflection(sourceLocation(simpleType)); - XsdFacet::HashIterator it(facets); - while (it.hasNext()) { - it.next(); + for (auto it = facets.cbegin(), end = facets.cend(); it != end; ++it) { const XsdFacet::Ptr facet = it.value(); if (facet->type() == XsdFacet::MaximumInclusive || facet->type() == XsdFacet::MaximumExclusive || diff --git a/src/xmlpatterns/schema/qxsdstatemachine_tpl_p.h b/src/xmlpatterns/schema/qxsdstatemachine_tpl_p.h index b989584..2583bb9 100644 --- a/src/xmlpatterns/schema/qxsdstatemachine_tpl_p.h +++ b/src/xmlpatterns/schema/qxsdstatemachine_tpl_p.h @@ -74,9 +74,7 @@ typename XsdStateMachine::StateId XsdStateMachine it(m_states); - while (it.hasNext()) { - it.next(); + for (auto it = m_states.cbegin(), end = m_states.cend(); it != end; ++it) { Q_ASSERT(it.value() != StartState && it.value() != StartEndState); } } @@ -113,9 +111,7 @@ template void XsdStateMachine::reset() { // reset the machine to the start state - QHashIterator it(m_states); - while (it.hasNext()) { - it.next(); + for (auto it = m_states.cbegin(), end = m_states.cend(); it != end; ++it) { if (it.value() == StartState || it.value() == StartEndState) { m_currentState = it.key(); return; @@ -179,9 +175,7 @@ bool XsdStateMachine::proceed(InputType input) // fetch the transition entry for the current state const QHash > &entry = m_transitions[m_currentState]; - QHashIterator > it(entry); - while (it.hasNext()) { - it.next(); + for (auto it = entry.cbegin(), end = entry.cend(); it != end; ++it) { if (inputEqualsTransition(input, it.key())) { m_currentState = it.value().first(); m_lastTransition = it.key(); @@ -218,9 +212,7 @@ TransitionType XsdStateMachine::lastTransition() const template typename XsdStateMachine::StateId XsdStateMachine::startState() const { - QHashIterator it(m_states); - while (it.hasNext()) { - it.next(); + for (auto it = m_states.cbegin(), end = m_states.cend(); it != end; ++it) { if (it.value() == StartState || it.value() == StartEndState) return it.key(); } @@ -248,49 +240,39 @@ bool XsdStateMachine::outputGraph(QIODevice *device, const QStri QByteArray graph; QTextStream s(&graph); - QHashIterator > > it(m_transitions); - QHashIterator it3(m_states); - s << "digraph " << graphName << " {\n"; s << " mindist = 2.0\n"; // draw edges - while (it.hasNext()) { - it.next(); + for (auto it = m_transitions.cbegin(), end = m_transitions.cend(); it != end; ++it) { - QHashIterator > it2(it.value()); - while (it2.hasNext()) { - it2.next(); + for (auto it2 = it.value().cbegin(), end = it.value().cend(); it2 != end; ++it2) { for (int i = 0; i < it2.value().count(); ++i) s << " " << it.key() << " -> " << it2.value().at(i) << " [label=\"" << transitionTypeToString(it2.key()) << "\"]\n"; } } - QHashIterator > it4(m_epsilonTransitions); - while (it4.hasNext()) { - it4.next(); - - const QVector states = it4.value(); + for (auto it = m_epsilonTransitions.cbegin(), end = m_epsilonTransitions.cend(); it != end; ++it) { + const QVector states = it.value(); for (int i = 0; i < states.count(); ++i) - s << " " << it4.key() << " -> " << states.at(i) << " [label=\"ε\"]\n"; + s << " " << it.key() << " -> " << states.at(i) << " [label=\"ε\"]\n"; } // draw node info - while (it3.hasNext()) { - it3.next(); + for (auto it = m_states.cbegin(), end = m_states.cend(); it != end; ++it) { QString style; - if (it3.value() == StartState) { + if (it.value() == StartState) { style = QLatin1String("shape=circle, style=filled, color=blue"); - } else if (it3.value() == StartEndState) { + } else if (it.value() == StartEndState) { style = QLatin1String("shape=doublecircle, style=filled, color=blue"); - } else if (it3.value() == InternalState) { + } else if (it.value() == InternalState) { style = QLatin1String("shape=circle, style=filled, color=red"); - } else if (it3.value() == EndState) { + } else if (it.value() == EndState) { style = QLatin1String("shape=doublecircle, style=filled, color=green"); } - s << " " << it3.key() << " [" << style << "]\n"; + s << " " << it.key() << " [" << style << "]\n"; } s << "}\n"; @@ -357,11 +339,9 @@ XsdStateMachine XsdStateMachine::toDFA() const // search the start state as the algorithm starts with it... StateId startState = -1; - QHashIterator stateTypeIt(m_states); - while (stateTypeIt.hasNext()) { - stateTypeIt.next(); - if (stateTypeIt.value() == StartState) { - startState = stateTypeIt.key(); + for (auto it = m_states.cbegin(), end = m_states.cend(); it != end; ++it) { + if (it.value() == StartState) { + startState = it.key(); break; } } diff --git a/src/xmlpatterns/schema/qxsdtypechecker.cpp b/src/xmlpatterns/schema/qxsdtypechecker.cpp index 7c554c5..2e4ce8e 100644 --- a/src/xmlpatterns/schema/qxsdtypechecker.cpp +++ b/src/xmlpatterns/schema/qxsdtypechecker.cpp @@ -206,12 +206,8 @@ XsdFacet::Hash XsdTypeChecker::mergedFacetsForType(const SchemaType::Ptr &type, const XsdFacet::Hash facets = context->facetsForType(type); XsdFacet::Hash result = baseFacets; - XsdFacet::HashIterator it(facets); - while (it.hasNext()) { - it.next(); - + for (auto it = facets.cbegin(), end = facets.cend(); it != end; ++it) result.insert(it.key(), it.value()); - } return result; } diff --git a/src/xmlpatterns/schema/qxsdvalidatinginstancereader.cpp b/src/xmlpatterns/schema/qxsdvalidatinginstancereader.cpp index bf33af1..7e60667 100644 --- a/src/xmlpatterns/schema/qxsdvalidatinginstancereader.cpp +++ b/src/xmlpatterns/schema/qxsdvalidatinginstancereader.cpp @@ -732,14 +732,11 @@ bool XsdValidatingInstanceReader::validateElementComplexType(const XsdElement::P const QSet attributes(attributeNames()); // 3 - QHashIterator usesIt(attributeUseHash); - while (usesIt.hasNext()) { - usesIt.next(); - - if (usesIt.value()->isRequired()) { - if (!attributes.contains(usesIt.key())) { + for (auto it = attributeUseHash.cbegin(), end = attributeUseHash.cend(); it != end; ++it) { + if (it.value()->isRequired()) { + if (!attributes.contains(it.key())) { error(QtXmlPatterns::tr("Element %1 is missing required attribute %2.").arg(formatKeyword(declaration->displayName(m_namePool))) - .arg(formatKeyword(m_namePool->displayName(usesIt.key())))); + .arg(formatKeyword(m_namePool->displayName(it.key())))); return false; } } -- cgit v1.2.1