QHash/QSet - fix QHash::erase when the hash is shared
Before a call to erase on a shared instance would imply that the item was removed from the shared data (i.e all instances) This patch improves the behavior to detach and erase the item specified by the iterator (i.e same behavior as QVector) Since QSet uses QHash it improves QSet the same way. Change-Id: I850b1efcf7bdfc85ceddb23128b048af95f75063 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
dacf9961da
commit
93ffb81df6
|
|
@ -850,6 +850,22 @@ Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::erase(itera
|
|||
if (it == iterator(e))
|
||||
return it;
|
||||
|
||||
if (d->ref.isShared()) {
|
||||
int bucketNum = (it.i->h % d->numBuckets);
|
||||
const_iterator bucketIterator(*(d->buckets + bucketNum));
|
||||
int stepsFromBucketStartToIte = 0;
|
||||
while (bucketIterator != it) {
|
||||
++stepsFromBucketStartToIte;
|
||||
++bucketIterator;
|
||||
}
|
||||
detach();
|
||||
it = iterator(*(d->buckets + bucketNum));
|
||||
while (stepsFromBucketStartToIte > 0) {
|
||||
--stepsFromBucketStartToIte;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
iterator ret = it;
|
||||
++ret;
|
||||
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ private slots:
|
|||
|
||||
void qthash_data();
|
||||
void qthash();
|
||||
void eraseValidIteratorOnSharedHash();
|
||||
};
|
||||
|
||||
struct Foo {
|
||||
|
|
@ -1352,5 +1353,34 @@ void tst_QHash::qthash()
|
|||
QTEST(result, "hash");
|
||||
}
|
||||
|
||||
void tst_QHash::eraseValidIteratorOnSharedHash()
|
||||
{
|
||||
QHash<int, int> a, b;
|
||||
a.insert(10, 10);
|
||||
a.insertMulti(10, 25);
|
||||
a.insertMulti(10, 30);
|
||||
a.insert(20, 20);
|
||||
a.insert(40, 40);
|
||||
|
||||
QHash<int, int>::iterator i = a.begin();
|
||||
while (i.value() != 25)
|
||||
++i;
|
||||
|
||||
b = a;
|
||||
a.erase(i);
|
||||
|
||||
QCOMPARE(b.size(), 5);
|
||||
QCOMPARE(a.size(), 4);
|
||||
|
||||
for (i = a.begin(); i != a.end(); ++i)
|
||||
QVERIFY(i.value() != 25);
|
||||
|
||||
int itemsWith10 = 0;
|
||||
for (i = b.begin(); i != b.end(); ++i)
|
||||
itemsWith10 += (i.key() == 10);
|
||||
|
||||
QCOMPARE(itemsWith10, 3);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QHash)
|
||||
#include "tst_qhash.moc"
|
||||
|
|
|
|||
Loading…
Reference in New Issue