Browse Source

proposed change: constify things as much as possible.

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac397@3611 e5f2f494-b856-4b98-b285-d166d9295462
JINMEI Tatuya 14 years ago
parent
commit
30c9e302d9
2 changed files with 49 additions and 40 deletions
  1. 27 23
      src/bin/auth/rbt_datasrc.h
  2. 22 17
      src/bin/auth/tests/rbt_datasrc_unittest.cc

+ 27 - 23
src/bin/auth/rbt_datasrc.h

@@ -73,7 +73,7 @@ public:
     T& getData() { return (data_); }
 
     /// \brief return next node whose name is bigger than current node
-    RBNode<T>* successor();
+    const RBNode<T>* successor() const;
     //@}
     
     /// \name Modify functions
@@ -157,9 +157,9 @@ RBNode<T>::~RBNode() {
 }
 
 template <typename T>
-RBNode<T>*
-RBNode<T>::successor() {
-    RBNode<T>* current = this;
+const RBNode<T>*
+RBNode<T>::successor() const {
+    const RBNode<T>* current = this;
 
     // if it has right node, the successor is the left-most node
     if (right_ != right_->right_) {
@@ -172,7 +172,7 @@ RBNode<T>::successor() {
 
     // otherwise return the parent without left child or
     // current node is not its right child
-    RBNode<T>* s = current->parent_;
+    const RBNode<T>* s = current->parent_;
     while (s != s->left_ && current == s->right_) {
         current = s;
         s = s->parent_;
@@ -232,7 +232,7 @@ public:
     /// \param node Point to the node when the return vaule is \c not
     /// NOTFOUND, if the return value is NOTFOUND, the value of node is
     /// \c unknown
-    FindResult find(const Name& name, RBNode<T>** node) const;
+    FindResult find(const Name& name, const RBNode<T>** node) const;
 
     /// \brief Get the total node count in the tree
     /// the node count including the node created common suffix node
@@ -286,7 +286,7 @@ private:
     /// Each public function has related recursive helper function
     void eraseNode(RBNode<T>* node);
     FindResult findHelper(const Name& name, const RBTree<T>** tree,
-                          RBNode<T>** node) const;
+                          const RBNode<T>** node) const;
     int getNodeCountHelper(const RBNode<T>* node) const;
     int getNameCountHelper(const RBNode<T>* node) const;
     void printTreeHelper(std::ostream &os, const RBNode<T>* node, int depth) const;
@@ -360,7 +360,7 @@ RBTree<T>::~RBTree() {
 
 template <typename T>
 typename RBTree<T>::FindResult
-RBTree<T>::find(const Name& name, RBNode<T>** node) const {
+RBTree<T>::find(const Name& name, const RBNode<T>** node) const {
     const RBTree<T>* tree;
     return (findHelper(name, &tree, node));
 }
@@ -368,12 +368,12 @@ RBTree<T>::find(const Name& name, RBNode<T>** node) const {
 template <typename T>
 typename RBTree<T>::FindResult
 RBTree<T>::findHelper(const Name& name, const RBTree<T>** tree,
-                      RBNode<T>** ret) const
+                      const RBNode<T>** ret) const
 {
     RBNode<T>* node = root_;
     while (node != NULLNODE) {
-        NameComparisonResult compare_result = name.compare(node->name_);
-        NameComparisonResult::NameRelation relation =
+        const NameComparisonResult compare_result = name.compare(node->name_);
+        const NameComparisonResult::NameRelation relation =
             compare_result.getRelation();
         if (relation == NameComparisonResult::EQUAL) {
             if (node->is_shadow_) {
@@ -384,7 +384,7 @@ RBTree<T>::findHelper(const Name& name, const RBTree<T>** tree,
                 return (RBTree<T>::EXACTMATCH);
             }
         } else {
-            int common_label_count = compare_result.getCommonLabels();
+            const int common_label_count = compare_result.getCommonLabels();
             // common label count equal one means, there is no common between
             // two names
             if (common_label_count == 1) {
@@ -435,7 +435,8 @@ RBTree<T>::getNodeCountHelper(const RBNode<T> *node) const {
         return (0);
     }
 
-    int sub_tree_node_count = node->down_ ? node->down_->getNodeCount() : 0;
+    const int sub_tree_node_count =
+        node->down_ ? node->down_->getNodeCount() : 0;
     return (1 + sub_tree_node_count + getNodeCountHelper(node->left_) +
             getNodeCountHelper(node->right_));
 }
@@ -453,7 +454,8 @@ RBTree<T>::getNameCountHelper(const RBNode<T> *node) const {
         return (0);
     }
 
-    int sub_tree_name_count = node->down_ ? node->down_->getNameCount() : 0;
+    const int sub_tree_name_count =
+        node->down_ ? node->down_->getNameCount() : 0;
     return ((node->is_shadow_ ? 0 : 1) + sub_tree_name_count +
             getNameCountHelper(node->left_) +
             getNameCountHelper(node->right_));
@@ -469,8 +471,9 @@ RBTree<T>::insert(const Name& name, RBNode<T>** new_node) {
     while (current != NULLNODE) {
         parent = current;
 
-        NameComparisonResult compare_result = name.compare(current->name_);
-        NameComparisonResult::NameRelation relation =
+        const NameComparisonResult compare_result =
+            name.compare(current->name_);
+        const NameComparisonResult::NameRelation relation =
             compare_result.getRelation();
         if (relation == NameComparisonResult::EQUAL) {
             if (new_node != NULL) {
@@ -485,7 +488,7 @@ RBTree<T>::insert(const Name& name, RBNode<T>** new_node) {
                 return (1);
             }
         } else {
-            int common_label_count = compare_result.getCommonLabels();
+            const int common_label_count = compare_result.getCommonLabels();
             if (common_label_count == 1) {
                 order = compare_result.getOrder();
                 current = order < 0 ? current->left_ : current->right_;
@@ -515,10 +518,10 @@ RBTree<T>::insert(const Name& name, RBNode<T>** new_node) {
                     // for super domain or has common label domain, create
                     // common node first then insert current name and new name
                     // into the sub tree
-                    Name common_ancestor = name.split(
+                    const Name common_ancestor = name.split(
                         name.getLabelCount() - common_label_count,
                         common_label_count);
-                    Name sub_name = current->name_ - common_ancestor;
+                    const Name sub_name = current->name_ - common_ancestor;
                     try {
                         // create new sub domain tree, and ty to insert 
                         // (current_name - common_ancestor) and (name - common_ancestor)
@@ -691,13 +694,14 @@ RBTree<T>::rightRotate(RBNode<T>* p) {
 template <typename T>
 int
 RBTree<T>::erase(const Name& name) {
-    RBNode<T>* node;
+    const RBNode<T>* cnode;
     const RBTree<T>* ctree;
-    if (findHelper(name, &ctree, &node) != RBTree<T>::EXACTMATCH) {
+    if (findHelper(name, &ctree, &cnode) != RBTree<T>::EXACTMATCH) {
         return (1);
     }
 
     // for node with downpointer, set it to shadow
+    RBNode<T>* node = const_cast<RBNode<T>*>(cnode);
     if (node->down_ != NULL) {
         assert(node->is_shadow_ == false);
         node->is_shadow_ = true;
@@ -711,7 +715,7 @@ RBTree<T>::erase(const Name& name) {
         // merge down to up
         if (1 == tree->node_count_ && tree->up_->is_shadow_) {
             RBNode<T>* up = tree->up_;
-            Name merged_name = tree->root_->name_.concatenate(up->name_);
+            const Name merged_name = tree->root_->name_.concatenate(up->name_);
             tree->root_->cloneDNSData(*up);
             up->setDownTree(tree->root_->down_);
             tree->root_->setDownTree(NULL);
@@ -735,7 +739,7 @@ RBTree<T>::eraseNode(RBNode<T> *node) {
     if (node->left_ == NULLNODE || node->right_ == NULLNODE) {
         y = node;
     } else {
-        y = node->successor();
+        y = const_cast<RBNode<T>*>(node->successor());
     }
 
     if (y->left_ != NULLNODE) {

+ 22 - 17
src/bin/auth/tests/rbt_datasrc_unittest.cc

@@ -64,6 +64,7 @@ protected:
     }
     RBTree<int> rbtree;
     RBNode<int> *rbtnode;
+    const RBNode<int>* crbtnode;
 };
 
 
@@ -135,23 +136,23 @@ TEST_F(RBTreeTest, insertNames) {
 
 TEST_F(RBTreeTest, findName) {
     // exact match
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &rbtnode));
-    EXPECT_EQ(Name("a"), rbtnode->getName());
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &crbtnode));
+    EXPECT_EQ(Name("a"), crbtnode->getName());
 
     // not found
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &rbtnode));
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("x"), &rbtnode));
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("m.n"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &crbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("x"), &crbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("m.n"), &crbtnode));
 
     // partial match
-    EXPECT_EQ(RBTree<int>::PARTIALMATCH, rbtree.find(Name("m.b"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::PARTIALMATCH, rbtree.find(Name("m.b"), &crbtnode));
     EXPECT_EQ(Name("b"), rbtnode->getName());
 }
 
 TEST_F(RBTreeTest, successor) {
     // traverse the trees
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &rbtnode));
-    RBNode<int> *successor_node = rbtnode->successor();
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("a"), &crbtnode));
+    const RBNode<int> *successor_node = rbtnode->successor();
     EXPECT_EQ(Name("b"), successor_node->getName());
     successor_node = successor_node->successor();
     EXPECT_EQ(Name("c"), successor_node->getName());
@@ -161,14 +162,15 @@ TEST_F(RBTreeTest, successor) {
     EXPECT_EQ(Name("g.h"), successor_node->getName());
     successor_node = successor_node->successor();
 
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("x.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("x.d.e.f"), &crbtnode));
     EXPECT_EQ(Name("x"), rbtnode->getName());
     successor_node = rbtnode->successor();
     EXPECT_EQ(Name("w.y"), successor_node->getName());
     successor_node = successor_node->successor();
     EXPECT_EQ(Name("z"), successor_node->getName());
 
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("o.w.y.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("o.w.y.d.e.f"),
+                                                   &crbtnode));
     EXPECT_EQ(Name("o"), rbtnode->getName());
     successor_node = rbtnode->successor();
     EXPECT_EQ(Name("p"), successor_node->getName());
@@ -205,19 +207,22 @@ TEST_F(RBTreeTest, eraseName) {
 
     // can't delete shadow node
     EXPECT_EQ(1, rbtree.erase(Name("d.e.f")));
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("w.y.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("w.y.d.e.f"), &crbtnode));
     EXPECT_EQ(0, rbtree.erase(Name("p.w.y.d.e.f")));
     EXPECT_EQ(14, rbtree.getNodeCount());
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("p.w.y.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("p.w.y.d.e.f"),
+                                                 &crbtnode));
 
     EXPECT_EQ(0, rbtree.erase(Name("q.w.y.d.e.f")));
     EXPECT_EQ(12, rbtree.getNodeCount());
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("q.w.y.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("q.w.y.d.e.f"),
+                                                 &crbtnode));
 
     // o would not be rejoined with w.y if w.y had data
     // associated with the key
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("o.w.y.d.e.f"), &rbtnode));
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("w.y.d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("o.w.y.d.e.f"),
+                                                   &crbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("w.y.d.e.f"), &crbtnode));
     /*
      *               d.e.f
      *              /  |  \
@@ -270,9 +275,9 @@ TEST_F(RBTreeTest, eraseName) {
      */
     // can't delete shadow node
     EXPECT_EQ(0, rbtree.insert(Name("d.e.f"), &rbtnode));
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::EXACTMATCH, rbtree.find(Name("d.e.f"), &crbtnode));
     EXPECT_EQ(0, rbtree.erase(Name("d.e.f")));
-    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::NOTFOUND, rbtree.find(Name("d.e.f"), &crbtnode));
     // d.e.f node become shadow
     EXPECT_EQ(1, rbtree.erase(Name("d.e.f")));
     // z is a shdow node