From: Mukund Sivaraman Date: Tue, 3 Sep 2013 02:29:02 +0000 (+0530) Subject: [2750] Replace height check with getHeight() method X-Git-Tag: bind10-1.2.0beta1-release~193^2~4^2~26 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=da0e9a81e8a4e4fac77caf257607bf4fe1bb0f0d;p=thirdparty%2Fkea.git [2750] Replace height check with getHeight() method --- diff --git a/src/lib/datasrc/memory/domaintree.h b/src/lib/datasrc/memory/domaintree.h index 9e41de778a..713f703c3f 100644 --- a/src/lib/datasrc/memory/domaintree.h +++ b/src/lib/datasrc/memory/domaintree.h @@ -1593,6 +1593,23 @@ public: /// This function is mainly intended to be used for debugging. uint32_t getNodeCount() const { return (node_count_); } +private: + /// \brief Helper method for getHeight() + size_t getHeightHelper(const DomainTreeNode* node) const; + +public: + /// \brief Return the maximum height of sub-root nodes found in the + /// DomainTree forest. + /// + /// The height of a node is defined as the number of nodes in the + /// longest path from the node to a leaf. For each subtree in the + /// DomainTree forest, this method determines the height of its root + /// node. Then it returns the maximum such height in the forest. + /// + /// Note: This method exists for testing purposes. Non-test code + /// must not use it. + size_t getHeight() const; + /// \name Debug function //@{ /// \brief Print the nodes in the trees. @@ -3003,6 +3020,27 @@ DomainTree::rightRotate return (node); } +template +size_t +DomainTree::getHeightHelper(const DomainTreeNode* node) const { + if (node == NULL) { + return (0); + } + + const size_t dl = getHeightHelper(node->getLeft()); + const size_t dr = getHeightHelper(node->getRight()); + + const size_t this_height = (dl > dr) ? (dl + 1) : (dr + 1); + const size_t down_height = getHeightHelper(node->getDown()); + + return ((this_height > down_height) ? this_height : down_height); +} + +template +size_t +DomainTree::getHeight() const { + return (getHeightHelper(root_.get())); +} template void diff --git a/src/lib/datasrc/tests/memory/domaintree_unittest.cc b/src/lib/datasrc/tests/memory/domaintree_unittest.cc index ae0073bd7c..5a8b6edea0 100644 --- a/src/lib/datasrc/tests/memory/domaintree_unittest.cc +++ b/src/lib/datasrc/tests/memory/domaintree_unittest.cc @@ -191,22 +191,6 @@ TEST_F(DomainTreeTest, getDistance) { } } -void -checkDistances(const TestDomainTree& tree, int distance) { - TestDomainTreeNodeChain node_path; - const TestDomainTreeNode* node = NULL; - - // Try to find a node left of the left-most node, and start from its - // next node (which is the left-most node in its subtree). - EXPECT_EQ(TestDomainTree::NOTFOUND, - tree.find(Name("0"), &node, node_path, NULL, NULL)); - while ((node = tree.nextNode(node_path)) != NULL) { - // The distance from each node to its sub-tree root must be less - // than 2 * log(n). - EXPECT_GE(2 * distance, node->getDistance()); - } -} - TEST_F(DomainTreeTest, checkDistanceRandom) { // This test checks an important performance-related property of the // DomainTree (a red-black tree), which is important for us: the @@ -247,7 +231,9 @@ TEST_F(DomainTreeTest, checkDistanceRandom) { EXPECT_EQ(static_cast(NULL), dtnode->setData(new int(i + 1))); } - checkDistances(mytree, log_num_nodes); + // The distance from each node to its sub-tree root must be less + // than 2 * log(n). + EXPECT_GE(2 * log_num_nodes, mytree.getHeight()); } TEST_F(DomainTreeTest, checkDistanceSorted) { @@ -276,7 +262,9 @@ TEST_F(DomainTreeTest, checkDistanceSorted) { EXPECT_EQ(static_cast(NULL), dtnode->setData(new int(i + 1))); } - checkDistances(mytree, log_num_nodes); + // The distance from each node to its sub-tree root must be less + // than 2 * log(n). + EXPECT_GE(2 * log_num_nodes, mytree.getHeight()); } TEST_F(DomainTreeTest, setGetData) {