QSimplex: scope iterators tighter in for loops

This is in preparation of breaking QSimplexVariable up into
QSimplexVariable (with protected dtor) and trivial subclass
QConcreteSimplexVariable, to statically ensure that we're not deleting
derived classes (AnchorData) through QSimplexVariable pointers (which
is UB, and subject to a Coverity complaint).

This is basically the same program we did for QBrushData, culminating
in 3bbc9e29ef59683351cf35c19a8bd4a030615c64.

This first step scopes iterators of for loops in the for-loop, and, as
drive-bys, makes them use auto and the shorter cbegin()/cend(), so
they fit into a single line, and fixes the extra {} around single-line
bodies (only in touched lines).

Pick-to: 6.8 6.5
Coverity-Id: 390828
Change-Id: I439e0a10ebb316a33e92c42c255ee209139d47a2
Reviewed-by: Mate Barany <mate.barany@qt.io>
(cherry picked from commit 49809c6e41101a11b5a264c7e8b4d4ce05d3c04a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-03-12 13:41:44 +01:00 committed by Qt Cherry-pick Bot
parent 18f0dd9bdf
commit e02348117c
2 changed files with 6 additions and 18 deletions

View File

@ -230,12 +230,8 @@ bool QSimplex::setConstraints(const QList<QSimplexConstraint *> &newConstraints)
setValueAt(i, c->helper.first->index, 1.0);
}
QHash<QSimplexVariable *, qreal>::const_iterator iter;
for (iter = c->variables.constBegin();
iter != c->variables.constEnd();
++iter) {
for (auto iter = c->variables.cbegin(); iter != c->variables.cend(); ++iter)
setValueAt(i, iter.key()->index, iter.value());
}
setValueAt(i, columns - 1, c->constant);
}
@ -490,10 +486,7 @@ qreal QSimplex::solver(SolverFactor factor)
// Set new objective in the first row of the simplex matrix
qreal resultOffset = 0;
QHash<QSimplexVariable *, qreal>::const_iterator iter;
for (iter = objective->variables.constBegin();
iter != objective->variables.constEnd();
++iter) {
for (auto iter = objective->variables.cbegin(); iter != objective->variables.cend(); ++iter) {
// Check if the variable was removed in the simplification process.
// If so, we save its offset to the objective function and skip adding
@ -594,8 +587,7 @@ bool QSimplex::simplifyConstraints(QList<QSimplexConstraint *> *constraints)
}
// Replace known values among their variables
QHash<QSimplexVariable *, qreal>::const_iterator r;
for (r = results.constBegin(); r != results.constEnd(); ++r) {
for (auto r = results.cbegin(); r != results.cend(); ++r) {
if (c->variables.contains(r.key())) {
c->constant -= r.value() * c->variables.take(r.key());
modified = true;
@ -630,10 +622,8 @@ void QSimplexConstraint::invert()
constant = -constant;
ratio = Ratio(2 - ratio);
QHash<QSimplexVariable *, qreal>::iterator iter;
for (iter = variables.begin(); iter != variables.end(); ++iter) {
for (auto iter = variables.begin(); iter != variables.end(); ++iter)
iter.value() = -iter.value();
}
}
QT_END_NAMESPACE

View File

@ -65,8 +65,7 @@ struct QSimplexConstraint
bool isSatisfied() {
qreal leftHandSide(0);
QHash<QSimplexVariable *, qreal>::const_iterator iter;
for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) {
leftHandSide += iter.value() * iter.key()->result;
}
@ -90,8 +89,7 @@ struct QSimplexConstraint
QString result;
result += QString::fromLatin1("-- QSimplexConstraint %1 --").arg(quintptr(this), 0, 16);
QHash<QSimplexVariable *, qreal>::const_iterator iter;
for (iter = variables.constBegin(); iter != variables.constEnd(); ++iter) {
for (auto iter = variables.cbegin(); iter != variables.cend(); ++iter) {
result += QString::fromLatin1(" %1 x %2").arg(iter.value()).arg(quintptr(iter.key()), 0, 16);
}