QJsonObject: minor refactoring

Applied DRY principle.

Change-Id: Ic3035552c6174167b4fe19fd4c825500dff16ded
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mat Sutcliffe 2019-07-02 02:30:26 +01:00
parent 8010e906d3
commit a4e9fa03ca
2 changed files with 26 additions and 16 deletions

View File

@ -525,8 +525,7 @@ QJsonObject::iterator QJsonObject::insertAt(int pos, const QString &key, const Q
if (valueSize)
QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue);
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
compactIfNeeded();
return iterator(this, pos);
}
@ -546,11 +545,7 @@ void QJsonObject::remove(const QString &key)
if (!keyExists)
return;
detach2();
o->removeItems(index, 1);
++d->compactionCounter;
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
removeAt(index);
}
/*!
@ -573,11 +568,7 @@ QJsonValue QJsonObject::take(const QString &key)
return QJsonValue(QJsonValue::Undefined);
QJsonValue v(d, o, o->entryAt(index)->value);
detach2();
o->removeItems(index, 1);
++d->compactionCounter;
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
removeAt(index);
return v;
}
@ -659,10 +650,7 @@ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)
int index = it.i;
o->removeItems(index, 1);
++d->compactionCounter;
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
removeAt(index);
// iterator hasn't changed
return it;
@ -1265,6 +1253,15 @@ void QJsonObject::compact()
o = static_cast<QJsonPrivate::Object *>(d->header->root());
}
/*!
\internal
*/
void QJsonObject::compactIfNeeded()
{
if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
compact();
}
/*!
\internal
*/
@ -1299,6 +1296,17 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val)
insertAt(i, e->key(), val, true);
}
/*!
\internal
*/
void QJsonObject::removeAt(int index)
{
detach2();
o->removeItems(index, 1);
++d->compactionCounter;
compactIfNeeded();
}
uint qHash(const QJsonObject &object, uint seed)
{
QtPrivate::QHashCombine hash;

View File

@ -247,10 +247,12 @@ private:
void detach(uint reserve = 0);
bool detach2(uint reserve = 0);
void compact();
void compactIfNeeded();
QString keyAt(int i) const;
QJsonValue valueAt(int i) const;
void setValueAt(int i, const QJsonValue &val);
void removeAt(int i);
iterator insertAt(int i, const QString &key, const QJsonValue &val, bool exists);
QJsonPrivate::Data *d;