Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Add empty() and swap() methods to KisForest

Dmitry Kazakov20a8bd8a9a00
diff --git a/libs/global/KisForest.h b/libs/global/KisForest.h
index 013b05e659..93d715cf1a 100644
--- a/libs/global/KisForest.h
+++ b/libs/global/KisForest.h
@@ -817,6 +817,14 @@ public:
using depth_first_tail_iterator = DepthFirstIterator<T, Leave, false>;
using const_depth_first_tail_iterator = DepthFirstIterator<T, Leave, true>;
+ bool empty() const {
+ return !m_root.firstChild;
+ }
+
+ void swap(Forest &other) {
+ std::swap(m_root, other.m_root);
+ }
+
iterator begin() {
return beginImpl(*this);
}
diff --git a/libs/global/tests/KisForestTest.cpp b/libs/global/tests/KisForestTest.cpp
index 12ab569dda..9b96351047 100644
--- a/libs/global/tests/KisForestTest.cpp
+++ b/libs/global/tests/KisForestTest.cpp
@@ -936,6 +936,83 @@ void KisForestTest::testCopyForest()
}
+void KisForestTest::testSwapForest()
+{
+ /**
+ * 0 1
+ * 2
+ * 3
+ **/
+
+ KisForest<int> forest1;
+
+ auto it0 = forest1.insert(childBegin(forest1), 0);
+
+ auto it1 = forest1.insert(childEnd(it0), 1);
+ auto it2 = forest1.insert(childEnd(it0), 2);
+ auto it3 = forest1.insert(childEnd(it0), 3);
+
+ QVERIFY(testForestIteration(begin(forest1),
+ end(forest1),
+ {0, 1, 2, 3}));
+
+ KisForest<int> forest2;
+
+ QVERIFY(testForestIteration(begin(forest2),
+ end(forest2),
+ {}));
+
+
+ forest1.swap(forest2);
+
+ QVERIFY(testForestIteration(begin(forest1),
+ end(forest1),
+ {}));
+
+ QVERIFY(testForestIteration(begin(forest2),
+ end(forest2),
+ {0, 1, 2, 3}));
+
+ /**
+ * After swapping the containers the iterators are guaranteed to be valid still,
+ * as per normal C++ convention.
+ */
+ QCOMPARE(childBegin(forest2), it0);
+ QCOMPARE(childBegin(it0), it1);
+ QCOMPARE(std::next(childBegin(it0)), it2);
+ QCOMPARE(std::next(std::next(childBegin(it0))), it3);
+}
+
+void KisForestTest::testForestEmpty()
+{
+ KisForest<int> forest;
+
+ /**
+ * 0 1
+ * 2
+ * 3
+ **/
+
+
+ QVERIFY(forest.empty());
+
+ auto it0 = forest.insert(childBegin(forest), 0);
+
+ [[maybe_unused]] auto it1 = forest.insert(childEnd(it0), 1);
+ [[maybe_unused]] auto it2 = forest.insert(childEnd(it0), 2);
+ [[maybe_unused]] auto it3 = forest.insert(childEnd(it0), 3);
+
+ QVERIFY(testForestIteration(begin(forest),
+ end(forest),
+ {0, 1, 2, 3}));
+
+ QVERIFY(!forest.empty());
+
+ forest.erase(it0);
+
+ QVERIFY(forest.empty());
+}
+
void KisForestTest::testSiblingsOnEndIterator()
{
KisForest<int> forest;
diff --git a/libs/global/tests/KisForestTest.h b/libs/global/tests/KisForestTest.h
index 23d8ce4c96..72f73ffea7 100644
--- a/libs/global/tests/KisForestTest.h
+++ b/libs/global/tests/KisForestTest.h
@@ -39,6 +39,8 @@ private Q_SLOTS:
void testConversionsFromEnd();
void testCopyForest();
+ void testSwapForest();
+ void testForestEmpty();
void testSiblingsOnEndIterator();
void testParentIterator();