Bug 983538 (part 1) - Convert mfbt/SplayTree.h to Mozilla style. r=froydnj.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 13 Mar 2014 22:35:45 -0700
changeset 203739 8b52179e47eec9d23ba6eaf8696de581f38a6108
parent 203738 5e18fd30243f4e6709a77be6ce612ea6ca671f42
child 203740 677d12a5d608f6313c84de0f86f651a98e669c74
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs983538
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 983538 (part 1) - Convert mfbt/SplayTree.h to Mozilla style. r=froydnj.
mfbt/SplayTree.h
--- a/mfbt/SplayTree.h
+++ b/mfbt/SplayTree.h
@@ -18,266 +18,279 @@
 namespace mozilla {
 
 template<class T, class C>
 class SplayTree;
 
 template<typename T>
 class SplayTreeNode
 {
-  public:
-    template<class A, class B>
-    friend class SplayTree;
+public:
+  template<class A, class B>
+  friend class SplayTree;
 
-    SplayTreeNode()
-      : left(nullptr), right(nullptr), parent(nullptr)
-    {}
+  SplayTreeNode()
+    : mLeft(nullptr)
+    , mRight(nullptr)
+    , mParent(nullptr)
+  {}
 
-  private:
-    T* left;
-    T* right;
-    T* parent;
+private:
+  T* mLeft;
+  T* mRight;
+  T* mParent;
 };
 
 
 /**
  * Class which represents a splay tree.
  * Splay trees are balanced binary search trees for which search, insert and
  * remove are all amortized O(log n), but where accessing a node makes it
  * faster to access that node in the future.
  *
  * T indicates the type of tree elements, Comparator must have a static
  * compare(const T&, const T&) method ordering the elements. The compare
  * method must be free from side effects.
  */
 template<typename T, class Comparator>
 class SplayTree
 {
-    T* root;
-
-  public:
-    SplayTree()
-      : root(nullptr)
-    {}
+  T* mRoot;
 
-    bool empty() const {
-      return !root;
-    }
-
-    T* find(const T& v)
-    {
-      if (empty())
-        return nullptr;
+public:
+  SplayTree()
+    : mRoot(nullptr)
+  {}
 
-      T* last = lookup(v);
-      splay(last);
-      checkCoherency(root, nullptr);
-      return Comparator::compare(v, *last) == 0 ? last : nullptr;
-    }
-
-    bool insert(T* v)
-    {
-      MOZ_ASSERT(!find(*v), "Duplicate elements are not allowed.");
+  bool empty() const
+  {
+    return !mRoot;
+  }
 
-      if (!root) {
-        root = v;
-        return true;
-      }
-      T* last = lookup(*v);
-      int cmp = Comparator::compare(*v, *last);
-
-      T** parentPointer = (cmp < 0) ? &last->left : &last->right;
-      MOZ_ASSERT(!*parentPointer);
-      *parentPointer = v;
-      v->parent = last;
-
-      splay(v);
-      checkCoherency(root, nullptr);
-      return true;
+  T* find(const T& aValue)
+  {
+    if (empty()) {
+      return nullptr;
     }
 
-    T* remove(const T& v)
-    {
-      T* last = lookup(v);
-      MOZ_ASSERT(last, "This tree must contain the element being removed.");
-      MOZ_ASSERT(Comparator::compare(v, *last) == 0);
+    T* last = lookup(aValue);
+    splay(last);
+    checkCoherency(mRoot, nullptr);
+    return Comparator::compare(aValue, *last) == 0 ? last : nullptr;
+  }
+
+  bool insert(T* aValue)
+  {
+    MOZ_ASSERT(!find(*aValue), "Duplicate elements are not allowed.");
 
-      // Splay the tree so that the item to remove is the root.
-      splay(last);
-      MOZ_ASSERT(last == root);
+    if (!mRoot) {
+      mRoot = aValue;
+      return true;
+    }
+    T* last = lookup(*aValue);
+    int cmp = Comparator::compare(*aValue, *last);
+
+    T** parentPointer = (cmp < 0) ? &last->mLeft : &last->mRight;
+    MOZ_ASSERT(!*parentPointer);
+    *parentPointer = aValue;
+    aValue->mParent = last;
+
+    splay(aValue);
+    checkCoherency(mRoot, nullptr);
+    return true;
+  }
 
-      // Find another node which can be swapped in for the root: either the
-      // rightmost child of the root's left, or the leftmost child of the
-      // root's right.
-      T* swap;
-      T* swapChild;
-      if (root->left) {
-        swap = root->left;
-        while (swap->right)
-          swap = swap->right;
-        swapChild = swap->left;
-      } else if (root->right) {
-        swap = root->right;
-        while (swap->left)
-          swap = swap->left;
-        swapChild = swap->right;
-      } else {
-        T* result = root;
-        root = nullptr;
-        return result;
-      }
+  T* remove(const T& aValue)
+  {
+    T* last = lookup(aValue);
+    MOZ_ASSERT(last, "This tree must contain the element being removed.");
+    MOZ_ASSERT(Comparator::compare(aValue, *last) == 0);
+
+    // Splay the tree so that the item to remove is the root.
+    splay(last);
+    MOZ_ASSERT(last == mRoot);
 
-      // The selected node has at most one child, in swapChild. Detach it
-      // from the subtree by replacing it with that child.
-      if (swap == swap->parent->left)
-        swap->parent->left = swapChild;
-      else
-        swap->parent->right = swapChild;
-      if (swapChild)
-        swapChild->parent = swap->parent;
-
-      // Make the selected node the new root.
-      root = swap;
-      root->parent = nullptr;
-      root->left = last->left;
-      root->right = last->right;
-      if (root->left) {
-        root->left->parent = root;
+    // Find another node which can be swapped in for the root: either the
+    // rightmost child of the root's left, or the leftmost child of the
+    // root's right.
+    T* swap;
+    T* swapChild;
+    if (mRoot->mLeft) {
+      swap = mRoot->mLeft;
+      while (swap->mRight) {
+        swap = swap->mRight;
       }
-      if (root->right) {
-        root->right->parent = root;
+      swapChild = swap->mLeft;
+    } else if (mRoot->mRight) {
+      swap = mRoot->mRight;
+      while (swap->mLeft) {
+        swap = swap->mLeft;
       }
-
-      checkCoherency(root, nullptr);
-      return last;
+      swapChild = swap->mRight;
+    } else {
+      T* result = mRoot;
+      mRoot = nullptr;
+      return result;
     }
 
-    T* removeMin()
-    {
-      MOZ_ASSERT(root, "No min to remove!");
+    // The selected node has at most one child, in swapChild. Detach it
+    // from the subtree by replacing it with that child.
+    if (swap == swap->mParent->mLeft) {
+      swap->mParent->mLeft = swapChild;
+    } else {
+      swap->mParent->mRight = swapChild;
+    }
+    if (swapChild) {
+      swapChild->mParent = swap->mParent;
+    }
 
-      T* min = root;
-      while (min->left)
-        min = min->left;
-      return remove(*min);
+    // Make the selected node the new root.
+    mRoot = swap;
+    mRoot->mParent = nullptr;
+    mRoot->mLeft = last->mLeft;
+    mRoot->mRight = last->mRight;
+    if (mRoot->mLeft) {
+      mRoot->mLeft->mParent = mRoot;
+    }
+    if (mRoot->mRight) {
+      mRoot->mRight->mParent = mRoot;
     }
 
-  private:
-    /**
-     * Returns the node in this comparing equal to |v|, or a node just greater or
-     * just less than |v| if there is no such node.
-     */
-    T* lookup(const T& v)
-    {
-      MOZ_ASSERT(!empty());
+    checkCoherency(mRoot, nullptr);
+    return last;
+  }
+
+  T* removeMin()
+  {
+    MOZ_ASSERT(mRoot, "No min to remove!");
 
-      T* node = root;
-      T* parent;
-      do {
-        parent = node;
-        int c = Comparator::compare(v, *node);
-        if (c == 0)
-          return node;
-        else if (c < 0)
-          node = node->left;
-        else
-          node = node->right;
-      } while (node);
-      return parent;
+    T* min = mRoot;
+    while (min->mLeft) {
+      min = min->mLeft;
     }
+    return remove(*min);
+  }
+
+private:
+  /**
+   * Returns the node in this comparing equal to |aValue|, or a node just
+   * greater or just less than |aValue| if there is no such node.
+   */
+  T* lookup(const T& aValue)
+  {
+    MOZ_ASSERT(!empty());
 
-    /**
-     * Rotate the tree until |node| is at the root of the tree. Performing
-     * the rotations in this fashion preserves the amortized balancing of
-     * the tree.
-     */
-    void splay(T* node)
-    {
-      MOZ_ASSERT(node);
+    T* node = mRoot;
+    T* parent;
+    do {
+      parent = node;
+      int c = Comparator::compare(aValue, *node);
+      if (c == 0) {
+        return node;
+      } else if (c < 0) {
+        node = node->mLeft;
+      } else {
+        node = node->mRight;
+      }
+    } while (node);
+    return parent;
+  }
 
-      while (node != root) {
-        T* parent = node->parent;
-        if (parent == root) {
-          // Zig rotation.
-          rotate(node);
-          MOZ_ASSERT(node == root);
-          return;
-        }
-        T* grandparent = parent->parent;
-        if ((parent->left == node) == (grandparent->left == parent)) {
-          // Zig-zig rotation.
-          rotate(parent);
-          rotate(node);
-        } else {
-          // Zig-zag rotation.
-          rotate(node);
-          rotate(node);
-        }
+  /**
+   * Rotate the tree until |node| is at the root of the tree. Performing
+   * the rotations in this fashion preserves the amortized balancing of
+   * the tree.
+   */
+  void splay(T* aNode)
+  {
+    MOZ_ASSERT(aNode);
+
+    while (aNode != mRoot) {
+      T* parent = aNode->mParent;
+      if (parent == mRoot) {
+        // Zig rotation.
+        rotate(aNode);
+        MOZ_ASSERT(aNode == mRoot);
+        return;
+      }
+      T* grandparent = parent->mParent;
+      if ((parent->mLeft == aNode) == (grandparent->mLeft == parent)) {
+        // Zig-zig rotation.
+        rotate(parent);
+        rotate(aNode);
+      } else {
+        // Zig-zag rotation.
+        rotate(aNode);
+        rotate(aNode);
       }
     }
+  }
 
-    void rotate(T* node)
-    {
-      // Rearrange nodes so that node becomes the parent of its current
-      // parent, while preserving the sortedness of the tree.
-      T* parent = node->parent;
-      if (parent->left == node) {
-        //     x          y
-        //   y  c  ==>  a  x
-        //  a b           b c
-        parent->left = node->right;
-        if (node->right)
-          node->right->parent = parent;
-        node->right = parent;
-      } else {
-        MOZ_ASSERT(parent->right == node);
-        //   x             y
-        //  a  y   ==>   x  c
-        //    b c       a b
-        parent->right = node->left;
-        if (node->left)
-          node->left->parent = parent;
-        node->left = parent;
+  void rotate(T* aNode)
+  {
+    // Rearrange nodes so that aNode becomes the parent of its current
+    // parent, while preserving the sortedness of the tree.
+    T* parent = aNode->mParent;
+    if (parent->mLeft == aNode) {
+      //     x          y
+      //   y  c  ==>  a  x
+      //  a b           b c
+      parent->mLeft = aNode->mRight;
+      if (aNode->mRight) {
+        aNode->mRight->mParent = parent;
       }
-      node->parent = parent->parent;
-      parent->parent = node;
-      if (T* grandparent = node->parent) {
-        if (grandparent->left == parent)
-          grandparent->left = node;
-        else
-          grandparent->right = node;
+      aNode->mRight = parent;
+    } else {
+      MOZ_ASSERT(parent->mRight == aNode);
+      //   x             y
+      //  a  y   ==>   x  c
+      //    b c       a b
+      parent->mRight = aNode->mLeft;
+      if (aNode->mLeft) {
+        aNode->mLeft->mParent = parent;
+      }
+      aNode->mLeft = parent;
+    }
+    aNode->mParent = parent->mParent;
+    parent->mParent = aNode;
+    if (T* grandparent = aNode->mParent) {
+      if (grandparent->mLeft == parent) {
+        grandparent->mLeft = aNode;
       } else {
-        root = node;
-      }
-    }
-
-    T* checkCoherency(T* node, T* minimum)
-    {
-#ifdef DEBUG
-      MOZ_ASSERT_IF(root, !root->parent);
-      if (!node) {
-        MOZ_ASSERT(!root);
-        return nullptr;
+        grandparent->mRight = aNode;
       }
-      MOZ_ASSERT_IF(!node->parent, node == root);
-      MOZ_ASSERT_IF(minimum, Comparator::compare(*minimum, *node) < 0);
-      if (node->left) {
-        MOZ_ASSERT(node->left->parent == node);
-        T* leftMaximum = checkCoherency(node->left, minimum);
-        MOZ_ASSERT(Comparator::compare(*leftMaximum, *node) < 0);
-      }
-      if (node->right) {
-        MOZ_ASSERT(node->right->parent == node);
-        return checkCoherency(node->right, node);
-      }
-      return node;
+    } else {
+      mRoot = aNode;
+    }
+  }
+
+  T* checkCoherency(T* aNode, T* aMinimum)
+  {
+#ifdef DEBUG
+    MOZ_ASSERT_IF(mRoot, !mRoot->mParent);
+    if (!aNode) {
+      MOZ_ASSERT(!mRoot);
+      return nullptr;
+    }
+    MOZ_ASSERT_IF(!aNode->mParent, aNode == mRoot);
+    MOZ_ASSERT_IF(aMinimum, Comparator::compare(*aMinimum, *aNode) < 0);
+    if (aNode->mLeft) {
+      MOZ_ASSERT(aNode->mLeft->mParent == aNode);
+      T* leftMaximum = checkCoherency(aNode->mLeft, aMinimum);
+      MOZ_ASSERT(Comparator::compare(*leftMaximum, *aNode) < 0);
+    }
+    if (aNode->mRight) {
+      MOZ_ASSERT(aNode->mRight->mParent == aNode);
+      return checkCoherency(aNode->mRight, aNode);
+    }
+    return aNode;
 #else
-      return nullptr;
+    return nullptr;
 #endif
-    }
+  }
 
-    SplayTree(const SplayTree&) MOZ_DELETE;
-    void operator=(const SplayTree&) MOZ_DELETE;
+  SplayTree(const SplayTree&) MOZ_DELETE;
+  void operator=(const SplayTree&) MOZ_DELETE;
 };
 
 }  /* namespace mozilla */
 
 #endif /* mozilla_SplayTree_h */