Parcourir la source

[2089] add isSubTreeRoot helper method to rbnode

currently it uses the property that the parent of a subtreeroot is a null node
Jelte Jansen il y a 13 ans
Parent
commit
910cf62a8d
2 fichiers modifiés avec 19 ajouts et 8 suppressions
  1. 13 1
      src/lib/datasrc/rbtree.h
  2. 6 7
      src/lib/datasrc/tests/rbtree_unittest.cc

+ 13 - 1
src/lib/datasrc/rbtree.h

@@ -265,6 +265,10 @@ private:
         }
     }
 
+    bool isSubTreeRoot() const {
+        return (parent_ == NULL_NODE());
+    }
+
     /// \brief return the next node which is bigger than current node
     /// in the same subtree
     ///
@@ -1428,8 +1432,10 @@ RBTree<T>::nodeFission(RBNode<T>& node, const isc::dns::Name& base_name) {
 
     down_node->down_ = node.down_;
     node.down_ = down_node.get();
+
     // Restore the color of the node (may have gotten changed by the flags swap)
     node.setColor(down_node->getColor());
+
     // root node of sub tree, the initial color is BLACK
     down_node->setColor(RBNode<T>::BLACK);
     ++node_count_;
@@ -1555,7 +1561,13 @@ RBTree<T>::dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
     indent(os, depth);
     os << node->name_.toText() << " ("
               << ((node->getColor() == RBNode<T>::BLACK) ? "black" : "red") << ")";
-    os << ((node->isEmpty()) ? "[invisible] \n" : "\n");
+    if (node->isEmpty()) {
+        os << " [invisible]";
+    }
+    if (node->isSubTreeRoot()) {
+        os << " [subtreeroot]";
+    }
+    os << "\n";
 
     if (node->down_ != NULLNODE) {
         indent(os, depth + 1);

+ 6 - 7
src/lib/datasrc/tests/rbtree_unittest.cc

@@ -681,15 +681,15 @@ TEST_F(RBTreeTest, dumpTree) {
     std::ostringstream str2;
     rbtree.dumpTree(str);
     str2 << "tree has 14 node(s)\n" <<
-            "b. (black)\n" <<
+            "b. (black) [subtreeroot]\n" <<
             "     a. (black)\n" <<
             "          NULL\n" <<
             "          NULL\n" <<
-            "     d.e.f. (black)[invisible] \n" <<
+            "     d.e.f. (black) [invisible]\n" <<
             "          begin down from d.e.f.\n" <<
-            "          w.y. (black)[invisible] \n" <<
+            "          w.y. (black) [invisible] [subtreeroot]\n" <<
             "               begin down from w.y.\n" <<
-            "               p. (black)\n" <<
+            "               p. (black) [subtreeroot]\n" <<
             "                    o. (red)\n" <<
             "                         NULL\n" <<
             "                         NULL\n" <<
@@ -702,7 +702,7 @@ TEST_F(RBTreeTest, dumpTree) {
             "                    NULL\n" <<
             "               z. (red)\n" <<
             "                    begin down from z.\n" <<
-            "                    j. (black)\n" <<
+            "                    j. (black) [subtreeroot]\n" <<
             "                         NULL\n" <<
             "                         NULL\n" <<
             "                    end down from z.\n" <<
@@ -714,7 +714,7 @@ TEST_F(RBTreeTest, dumpTree) {
             "               NULL\n" <<
             "          g.h. (red)\n" <<
             "               begin down from g.h.\n" <<
-            "               i. (black)\n" <<
+            "               i. (black) [subtreeroot]\n" <<
             "                    NULL\n" <<
             "                    k. (red)\n" <<
             "                         NULL\n" <<
@@ -722,7 +722,6 @@ TEST_F(RBTreeTest, dumpTree) {
             "               end down from g.h.\n" <<
             "               NULL\n" <<
             "               NULL\n";
-    rbtree.dumpTree(std::cout);
     EXPECT_EQ(str.str(), str2.str());
 }