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

View File

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