QJsonObject::removeAt: stop dividing and multiplying by 2

Change-Id: I89446ea06b5742efb194fffd16bb7d17182c6a2a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2021-11-27 11:13:47 -08:00
parent d263147921
commit 01fe59a3b7
3 changed files with 9 additions and 9 deletions

View File

@ -139,7 +139,6 @@ struct ObjectIterator
bool operator<=(ObjectIterator other) const { return it <= other.it; }
bool operator>=(ObjectIterator other) const { return it >= other.it; }
private:
ElementsIterator it;
};

View File

@ -310,7 +310,7 @@ static qsizetype indexOf(const QExplicitlySharedDataPointer<QCborContainerPrivat
});
*keyExists = (it != end) && o->stringEqualsElement((*it).key(), key);
return (it - begin) * 2;
return it.it - begin.it;
}
#if QT_STRINGVIEW_LEVEL < 2
@ -566,7 +566,7 @@ void QJsonObject::removeImpl(T key)
if (!keyExists)
return;
removeAt(index / 2);
removeAt(index);
}
#if QT_STRINGVIEW_LEVEL < 2
@ -619,7 +619,7 @@ QJsonValue QJsonObject::takeImpl(T key)
detach();
const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(o->extractAt(index + 1));
removeAt(index / 2);
removeAt(index);
return v;
}
@ -707,7 +707,7 @@ bool QJsonObject::operator!=(const QJsonObject &other) const
*/
QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)
{
removeAt(it.item.index);
removeAt(it.item.index * 2);
// index hasn't changed; the container pointer shouldn't have changed
// because we shouldn't have detached (detaching happens on obtaining a
@ -1461,8 +1461,8 @@ void QJsonObject::setValueAt(qsizetype i, const QJsonValue &val)
void QJsonObject::removeAt(qsizetype index)
{
detach();
o->removeAt(2 * index + 1);
o->removeAt(2 * index);
o->removeAt(index + 1);
o->removeAt(index);
}
size_t qHash(const QJsonObject &object, size_t seed)

View File

@ -1023,8 +1023,9 @@ void tst_QtJson::testObjectIteration()
for (QJsonObject::iterator it = object.begin(); it != object.end(); ++it)
QFAIL("Iterator after property add-and-remove should be empty");
for (int i = 0; i < 10; ++i)
object[QString::number(i)] = (double)i;
// insert in weird order to confirm keys are sorted
for (int i : {0, 9, 5, 7, 8, 2, 1, 3, 6, 4})
object[QString::number(i)] = double(i);
QCOMPARE(object.size(), 10);