Browse Source

some minor proposed fixes. most are purely editorial.

JINMEI Tatuya 14 years ago
parent
commit
2f151e47e2
2 changed files with 47 additions and 36 deletions
  1. 24 23
      src/lib/datasrc/rbtree.h
  2. 23 13
      src/lib/datasrc/tests/rbtree_unittest.cc

+ 24 - 23
src/lib/datasrc/rbtree.h

@@ -146,7 +146,7 @@ public:
 
     /// \brief return the next node which is bigger than current node
     /// in the same tree
-    const RBNode<T> *successor() const;
+    const RBNode<T>* successor() const;
     //@}
 
     /// \name Setter functions.
@@ -247,7 +247,7 @@ RBNode<T>::~RBNode() {
 template <typename T>
 const RBNode<T> *
 RBNode<T>::successor()const {
-    const RBNode<T> *current = this;
+    const RBNode<T>* current = this;
     // If it has right node, the successor is the left-most node of the right
     // subtree.
     if (right_ != NULL_NODE()) {
@@ -350,7 +350,7 @@ public:
     };
 
     /// save the nodes along the path from root to the target node
-    typedef typename std::stack<const RBNode<T> *> NodeChain;
+    typedef typename std::stack<const RBNode<T>*> NodeChain;
 
     /// \name Constructor and Destructor
     //@{
@@ -477,7 +477,7 @@ public:
 
     /// \brief Simple find returning immutable node.
     ///
-    /// Acts as described in the \ref findEx section, but returns immutable 
+    /// Acts as described in the \ref findEx section, but returns immutable
     /// node pointer.
     template <typename CBARG>
     Result findEx(const isc::dns::Name& name, NodeChain &node_path,
@@ -577,7 +577,7 @@ private:
     /// Internal searching function.
     ///
    void dumpTreeHelper(std::ostream& os, const RBNode<T>* node,
-                        unsigned int depth) const;
+                       unsigned int depth) const;
     /// \brief Indentation helper function for dumpTree
     static void indent(std::ostream& os, unsigned int depth);
 
@@ -588,20 +588,21 @@ private:
     void nodeFission(RBNode<T>& node, const isc::dns::Name& sub_name);
     //@}
 
-    RBNode<T>*  root_;
     RBNode<T>*  NULLNODE;
+    RBNode<T>*  root_;
     /// the node count of current tree
     unsigned int node_count_;
     /// search policy for rbtree
-    bool needsReturnEmptyNode_;
+    const bool needsReturnEmptyNode_;
 };
 
 template <typename T>
-RBTree<T>::RBTree(bool returnEmptyNode) {
-    NULLNODE = RBNode<T>::NULL_NODE();
-    root_ = NULLNODE;
-    node_count_ = 0;
-    needsReturnEmptyNode_ = returnEmptyNode;
+RBTree<T>::RBTree(bool returnEmptyNode) :
+    NULLNODE(RBNode<T>::NULL_NODE()),
+    root_(NULLNODE),
+    node_count_(0),
+    needsReturnEmptyNode_(returnEmptyNode)
+{
 }
 
 template <typename T>
@@ -612,18 +613,18 @@ RBTree<T>::~RBTree() {
 
 template <typename T>
 void
-RBTree<T>::deleteHelper(RBNode<T> *root) {
+RBTree<T>::deleteHelper(RBNode<T>* root) {
     if (root == NULLNODE) {
         return;
     }
 
-    RBNode<T> *node = root;
+    RBNode<T>* node = root;
     while (root->left_ != NULLNODE || root->right_ != NULLNODE) {
         while (node->left_ != NULLNODE || node->right_ != NULLNODE) {
             node = (node->left_ != NULLNODE) ? node->left_ : node->right_;
         }
 
-        RBNode<T> *parent = node->parent_;
+        RBNode<T>* parent = node->parent_;
         if (parent->left_ == node) {
             parent->left_ = NULLNODE;
         } else {
@@ -733,7 +734,7 @@ RBTree<T>::findEx(const isc::dns::Name& target_name,
                   bool (*callback)(const RBNode<T>&, CBARG),
                   CBARG callback_arg) const
 {
-    RBNode<T> *node = NULLNODE;
+    RBNode<T>* node = NULLNODE;
     const Result ret =
         findEx(target_name, node_path, &node, callback, callback_arg);
     if (ret != NOTFOUND) {
@@ -743,16 +744,16 @@ RBTree<T>::findEx(const isc::dns::Name& target_name,
 }
 
 template <typename T>
-const RBNode<T> *
-RBTree<T>::nextNode(const RBNode<T> *node, const NodeChain &node_path,
-                    NodeChain &next_node_path) const
+const RBNode<T>*
+RBTree<T>::nextNode(const RBNode<T>* node, const NodeChain& node_path,
+                    NodeChain& next_node_path) const
 {
     next_node_path = node_path;
-    // if node has sub domain, the next domain is the samllest
+    // if node has sub domain, the next domain is the smallest
     // domain in sub domain tree
     if (node->down_ != NULLNODE) {
         next_node_path.push(node);
-        const RBNode<T> *left_most = node->down_;
+        const RBNode<T>* left_most = node->down_;
         while (left_most->left_ != NULLNODE) {
             left_most = left_most->left_;
         }
@@ -760,7 +761,7 @@ RBTree<T>::nextNode(const RBNode<T> *node, const NodeChain &node_path,
     }
 
     // otherwise found the successor node in current level
-    const RBNode<T> *successor = node->successor();
+    const RBNode<T>* successor = node->successor();
     if (successor != NULLNODE) {
         return (successor);
     }
@@ -770,7 +771,7 @@ RBTree<T>::nextNode(const RBNode<T> *node, const NodeChain &node_path,
     // up node doesn't has successor we gonna keep moving to up
     // level
     while (!next_node_path.empty()) {
-        const RBNode<T> *up_node_successor = next_node_path.top()->successor();
+        const RBNode<T>* up_node_successor = next_node_path.top()->successor();
         next_node_path.pop();
         if (up_node_successor != NULLNODE) {
             return (up_node_successor);

+ 23 - 13
src/lib/datasrc/tests/rbtree_unittest.cc

@@ -51,8 +51,9 @@ namespace {
 class RBTreeTest : public::testing::Test {
 protected:
     RBTreeTest() : rbtree_expose_empty_node(true) {
-        const char * domain_names[] = {"c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h",
-            "o.w.y.d.e.f", "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f"};
+        const char* const domain_names[] = {
+            "c", "b", "a", "x.d.e.f", "z.d.e.f", "g.h", "i.g.h", "o.w.y.d.e.f",
+            "j.z.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f"};
         int name_count = sizeof(domain_names) / sizeof(domain_names[0]);
         for (int i = 0; i < name_count; ++i) {
             rbtree.insert(Name(domain_names[i]), &rbtnode);
@@ -81,7 +82,8 @@ TEST_F(RBTreeTest, setGetData) {
 }
 
 TEST_F(RBTreeTest, insertNames) {
-    EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("d.e.f"), &rbtnode));
+    EXPECT_EQ(RBTree<int>::ALREADYEXISTS, rbtree.insert(Name("d.e.f"),
+                                                        &rbtnode));
     EXPECT_EQ(Name("d.e.f"), rbtnode->getName());
     EXPECT_EQ(13, rbtree.getNodeCount());
 
@@ -232,7 +234,7 @@ TEST_F(RBTreeTest, callback) {
 
 /*
  *the domain order should be:
- * a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f, q.w.y.d.e.f, 
+ * a, b, c, d.e.f, x.d.e.f, w.y.d.e.f, o.w.y.d.e.f, p.w.y.d.e.f, q.w.y.d.e.f,
  * z.d.e.f, j.z.d.e.f, g.h, i.g.h
  *             b
  *           /   \
@@ -247,9 +249,11 @@ TEST_F(RBTreeTest, callback) {
  *                 p   j
  *               /   \
  *              o     q
- 
- * */
-Name nodeAbsoluteName(const RBNode<int> *node, const RBTree<int>::NodeChain &node_path) {
+ */
+Name
+nodeAbsoluteName(const RBNode<int> *node,
+                 const RBTree<int>::NodeChain &node_path)
+{
     isc::dns::Name absoluteName = node->getName();
     RBTree<int>::NodeChain node_path_copy = node_path;
     while (!node_path_copy.empty()) {
@@ -259,22 +263,28 @@ Name nodeAbsoluteName(const RBNode<int> *node, const RBTree<int>::NodeChain &nod
     return (absoluteName);
 }
 
-void testNodeAdjacentHelper(const RBTree<int> &tree, const Name &currentDomain, const Name &nextDomain) {
+void
+testNodeAdjacentHelper(const RBTree<int> &tree, const Name &currentDomain,
+                       const Name &nextDomain)
+{
     RBTree<int>::NodeChain node_path;
     RBTree<int>::NodeChain next_node_path;
     const RBNode<int> *node;
-    EXPECT_EQ(RBTree<int>::EXACTMATCH, tree.findEx<void *>(currentDomain, node_path, &node, NULL, NULL));
+    EXPECT_EQ(RBTree<int>::EXACTMATCH,
+              tree.findEx<void*>(currentDomain, node_path, &node, NULL, NULL));
     node = tree.nextNode(node, node_path, next_node_path);
     EXPECT_EQ(nextDomain, nodeAbsoluteName(node,  next_node_path));
 }
 
 TEST_F(RBTreeTest, nextNode) {
-    const char *names[] = {"a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f", "p.w.y.d.e.f", "q.w.y.d.e.f",
-       "z.d.e.f", "j.z.d.e.f", "g.h", "i.g.h"};
-    int name_count = sizeof(names) / sizeof(names[0]);
+    const char* const names[] = {
+        "a", "b", "c", "d.e.f", "x.d.e.f", "w.y.d.e.f", "o.w.y.d.e.f",
+        "p.w.y.d.e.f", "q.w.y.d.e.f", "z.d.e.f", "j.z.d.e.f", "g.h", "i.g.h"};
+    const int name_count = sizeof(names) / sizeof(names[0]);
     int i = 0;
     for (; i < name_count - 1; ++i) {
-        testNodeAdjacentHelper(rbtree_expose_empty_node, Name(names[i]), Name(names[i + 1]));
+        testNodeAdjacentHelper(rbtree_expose_empty_node, Name(names[i]),
+                               Name(names[i + 1]));
     }
 }