QT_BEGIN_NAMESPACE
-const QMapDataBase QMapDataBase::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, { 0, 0, 0 } };
+const QMapDataBase QMapDataBase::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, { 0, 0, 0 }, 0 };
const QMapNodeBase *QMapNodeBase::nextNode() const
{
QMapNodeBase *x_parent;
if (y->left == 0) {
x = y->right;
+ if (y == mostLeftNode) {
+ if (x)
+ mostLeftNode = x; // It cannot have (left) children due the red black invariant.
+ else
+ mostLeftNode = y->parent();
+ }
} else {
if (y->right == 0) {
x = y->left;
--size;
}
+void QMapDataBase::recalcMostLeftNode()
+{
+ mostLeftNode = &header;
+ while (mostLeftNode->left)
+ mostLeftNode = mostLeftNode->left;
+}
+
static inline int qMapAlignmentThreshold()
{
// malloc on 32-bit platforms should return pointers that are 8-byte
if (parent) {
if (left) {
parent->left = node;
+ if (parent == mostLeftNode)
+ mostLeftNode = node;
} else {
parent->right = node;
}
d->header.p = 0;
d->header.left = 0;
d->header.right = 0;
+ d->mostLeftNode = &(d->header);
return d;
}
QtPrivate::RefCount ref;
int size;
QMapNodeBase header;
+ QMapNodeBase *mostLeftNode;
void rotateLeft(QMapNodeBase *x);
void rotateRight(QMapNodeBase *x);
void rebalance(QMapNodeBase *x);
void freeNodeAndRebalance(QMapNodeBase *z);
+ void recalcMostLeftNode();
QMapNodeBase *createNode(int size, int alignment, QMapNodeBase *parent, bool left);
void freeTree(QMapNodeBase *root, int alignment);
const Node *end() const { return static_cast<const Node *>(&header); }
Node *end() { return static_cast<Node *>(&header); }
- const Node *begin() const { if (root()) return root()->minimumNode(); return end(); }
- Node *begin() { if (root()) return root()->minimumNode(); return end(); }
+ const Node *begin() const { if (root()) return static_cast<const Node*>(mostLeftNode); return end(); }
+ Node *begin() { if (root()) return static_cast<Node*>(mostLeftNode); return end(); }
void deleteNode(Node *z);
Node *findNode(const Key &akey) const;
if (other.d->header.left) {
d->header.left = static_cast<Node *>(other.d->header.left)->copy(d);
d->header.left->setParent(&d->header);
+ d->recalcMostLeftNode();
}
}
}
if (!d->ref.deref())
d->destroy();
d = x;
+ d->recalcMostLeftNode();
}
template <class Key, class T>
class tst_QMap : public QObject
{
Q_OBJECT
+protected:
+ template <class KEY, class VALUE>
+ void sanityCheckTree(const QMap<KEY, VALUE> &m, int calledFromLine);
public slots:
void init();
private slots:
void setSharable();
void insert();
+ void checkMostLeftNode();
};
typedef QMap<QString, QString> StringMap;
return d;
}
+template <class KEY, class VALUE>
+void tst_QMap::sanityCheckTree(const QMap<KEY, VALUE> &m, int calledFromLine)
+{
+ QString possibleFrom;
+ possibleFrom.setNum(calledFromLine);
+ possibleFrom = "Called from line: " + possibleFrom;
+ int count = 0;
+ typename QMap<KEY, VALUE>::const_iterator oldite = m.constBegin();
+ for (typename QMap<KEY, VALUE>::const_iterator i = m.constBegin(); i != m.constEnd(); ++i) {
+ count++;
+ bool oldIteratorIsLarger = i.key() < oldite.key();
+ QVERIFY2(!oldIteratorIsLarger, possibleFrom.toUtf8());
+ oldite = i;
+ }
+ if (m.size() != count) { // Fail
+ qDebug() << possibleFrom;
+ QCOMPARE(m.size(), count);
+ }
+ if (m.size() == 0)
+ QVERIFY(m.constBegin() == m.constEnd());
+}
+
void tst_QMap::init()
{
MyClass::count = 0;
map.insert( "key0", MyClass( "value1" ) );
map.insert( "key1", MyClass( "value2" ) );
map.clear();
+ sanityCheckTree(map, __LINE__);
QVERIFY( map.isEmpty() );
}
QCOMPARE( MyClass::count, int(0) );
m1.swap(m2);
QCOMPARE(m1.value(1),QLatin1String("m2[1]"));
QCOMPARE(m2.value(0),QLatin1String("m1[0]"));
+ sanityCheckTree(m1, __LINE__);
+ sanityCheckTree(m2, __LINE__);
}
void tst_QMap::operator_eq()
void tst_QMap::mergeCompare()
{
- QMap<int, QString> map1, map2, map3;
+ QMap<int, QString> map1, map2, map3, map1b, map2b;
map1.insert(1,"ett");
map1.insert(3,"tre");
map2.insert(4,"fyra");
map1.unite(map2);
+ sanityCheckTree(map1, __LINE__);
+
+ map1b = map1;
+ map2b = map2;
+ map2b.insert(0, "nul");
+ map1b.unite(map2b);
+ sanityCheckTree(map1b, __LINE__);
QVERIFY(map1.value(1) == "ett");
QVERIFY(map1.value(2) == "tvo");
QVERIFY(!map.isDetached());
QVERIFY(copy.isSharedWith(map));
+ sanityCheckTree(copy, __LINE__);
}
map.setSharable(false);
+ sanityCheckTree(map, __LINE__);
QVERIFY(map.isDetached());
QCOMPARE(map.size(), 4);
QCOMPARE(const_(map)[4], QString("quatro"));
QCOMPARE(const_(copy)[4], QString("quatro"));
QCOMPARE(map, copy);
+ sanityCheckTree(map, __LINE__);
+ sanityCheckTree(copy, __LINE__);
}
map.setSharable(true);
}
}
+void tst_QMap::checkMostLeftNode()
+{
+ QMap<int, int> map;
+
+ map.insert(100, 1);
+ sanityCheckTree(map, __LINE__);
+
+ // insert
+ map.insert(99, 1);
+ sanityCheckTree(map, __LINE__);
+ map.insert(98, 1);
+ sanityCheckTree(map, __LINE__);
+ map.insert(97, 1);
+ sanityCheckTree(map, __LINE__);
+ map.insert(96, 1);
+ sanityCheckTree(map, __LINE__);
+ map.insert(95, 1);
+
+ // remove
+ sanityCheckTree(map, __LINE__);
+ map.take(95);
+ sanityCheckTree(map, __LINE__);
+ map.remove(96);
+ sanityCheckTree(map, __LINE__);
+ map.erase(map.begin());
+ sanityCheckTree(map, __LINE__);
+ map.remove(97);
+ sanityCheckTree(map, __LINE__);
+ map.remove(98);
+ sanityCheckTree(map, __LINE__);
+ map.remove(99);
+ sanityCheckTree(map, __LINE__);
+ map.remove(100);
+ sanityCheckTree(map, __LINE__);
+ map.insert(200, 1);
+ QCOMPARE(map.constBegin().key(), 200);
+ sanityCheckTree(map, __LINE__);
+ // remove the non left most node
+ map.insert(202, 2);
+ map.insert(203, 3);
+ map.insert(204, 4);
+ map.remove(202);
+ sanityCheckTree(map, __LINE__);
+ map.remove(203);
+ sanityCheckTree(map, __LINE__);
+ map.remove(204);
+ sanityCheckTree(map, __LINE__);
+ // erase last item
+ map.erase(map.begin());
+ sanityCheckTree(map, __LINE__);
+}
+
QTEST_APPLESS_MAIN(tst_QMap)
#include "tst_qmap.moc"