]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2750] Replace height check with getHeight() method
authorMukund Sivaraman <muks@isc.org>
Tue, 3 Sep 2013 02:29:02 +0000 (07:59 +0530)
committerMukund Sivaraman <muks@isc.org>
Tue, 3 Sep 2013 02:29:02 +0000 (07:59 +0530)
src/lib/datasrc/memory/domaintree.h
src/lib/datasrc/tests/memory/domaintree_unittest.cc

index 9e41de778aa01f36a9d931cc766c058d03d614b6..713f703c3fd4758779670a9e18aca41ff8ac534a 100644 (file)
@@ -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<T>* 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<T>::rightRotate
     return (node);
 }
 
+template <typename T>
+size_t
+DomainTree<T>::getHeightHelper(const DomainTreeNode<T>* 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 <typename T>
+size_t
+DomainTree<T>::getHeight() const {
+    return (getHeightHelper(root_.get()));
+}
 
 template <typename T>
 void
index ae0073bd7c6502db5a1afec59c5d3aaa30c48839..5a8b6edea0a62183cb9338bf98f485edd689b94f 100644 (file)
@@ -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<void*>(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<int*>(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<int*>(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) {