From: Jelte Jansen Date: Tue, 17 Jul 2012 08:53:05 +0000 (+0200) Subject: [2089] add isSubTreeRoot helper method to rbnode X-Git-Tag: trac2351_base~157^2~27^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=910cf62a8db33a19d5b8ed08ff7a46a69be190bf;p=thirdparty%2Fkea.git [2089] add isSubTreeRoot helper method to rbnode currently it uses the property that the parent of a subtreeroot is a null node --- diff --git a/src/lib/datasrc/rbtree.h b/src/lib/datasrc/rbtree.h index ea62c2276f..b08accf156 100644 --- a/src/lib/datasrc/rbtree.h +++ b/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::nodeFission(RBNode& 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::BLACK); ++node_count_; @@ -1555,7 +1561,13 @@ RBTree::dumpTreeHelper(std::ostream& os, const RBNode* node, indent(os, depth); os << node->name_.toText() << " (" << ((node->getColor() == RBNode::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); diff --git a/src/lib/datasrc/tests/rbtree_unittest.cc b/src/lib/datasrc/tests/rbtree_unittest.cc index 507c969d1c..cfde248838 100644 --- a/src/lib/datasrc/tests/rbtree_unittest.cc +++ b/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()); }