OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/geometry/r_tree.h" | 5 #include "ui/gfx/geometry/r_tree.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 Node* node = it->second; | 620 Node* node = it->second; |
621 // Remove this node from the map. | 621 // Remove this node from the map. |
622 record_map_.erase(it); | 622 record_map_.erase(it); |
623 // Now remove it from the RTree. | 623 // Now remove it from the RTree. |
624 RemoveNode(node); | 624 RemoveNode(node); |
625 delete node; | 625 delete node; |
626 | 626 |
627 // Lastly check the root. If it has only one non-leaf child, delete it and | 627 // Lastly check the root. If it has only one non-leaf child, delete it and |
628 // replace it with its child. | 628 // replace it with its child. |
629 if (root_->count() == 1 && root_->level() > 0) { | 629 if (root_->count() == 1 && root_->level() > 0) { |
630 scoped_ptr<Node> new_root(root_->RemoveAndReturnLastChild()); | 630 root_ = root_->RemoveAndReturnLastChild(); |
631 root_.swap(new_root); | |
632 } | 631 } |
633 } | 632 } |
634 | 633 |
635 void RTree::Query(const Rect& query_rect, | 634 void RTree::Query(const Rect& query_rect, |
636 base::hash_set<intptr_t>* matches_out) const { | 635 base::hash_set<intptr_t>* matches_out) const { |
637 root_->Query(query_rect, matches_out); | 636 root_->Query(query_rect, matches_out); |
638 } | 637 } |
639 | 638 |
640 void RTree::Clear() { | 639 void RTree::Clear() { |
641 record_map_.clear(); | 640 record_map_.clear(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 // parent. | 708 // parent. |
710 DCHECK(parent); | 709 DCHECK(parent); |
711 // Should always be a leaf that had the record. | 710 // Should always be a leaf that had the record. |
712 DCHECK_EQ(parent->level(), 0); | 711 DCHECK_EQ(parent->level(), 0); |
713 ScopedVector<Node> orphans; | 712 ScopedVector<Node> orphans; |
714 Node* child = node; | 713 Node* child = node; |
715 | 714 |
716 // Traverse up the tree, removing the child from each parent and deleting | 715 // Traverse up the tree, removing the child from each parent and deleting |
717 // parent nodes, until we either encounter the root of the tree or a parent | 716 // parent nodes, until we either encounter the root of the tree or a parent |
718 // that still has sufficient children. | 717 // that still has sufficient children. |
719 while (parent && parent->RemoveChild(child, &orphans) < min_children_) { | 718 while (parent) { |
720 if (child != node) { | 719 size_t children_remaining = parent->RemoveChild(child, &orphans); |
| 720 if (child != node) |
721 delete child; | 721 delete child; |
722 } | 722 |
| 723 if (children_remaining >= min_children_) |
| 724 break; |
| 725 |
723 child = parent; | 726 child = parent; |
724 parent = parent->parent(); | 727 parent = parent->parent(); |
725 } | 728 } |
726 | 729 |
727 // If we stopped deleting nodes up the tree before encountering the root, | 730 // If we stopped deleting nodes up the tree before encountering the root, |
728 // we'll need to fix up the bounds from the first parent we didn't delete | 731 // we'll need to fix up the bounds from the first parent we didn't delete |
729 // up to the root. | 732 // up to the root. |
730 if (parent) { | 733 if (parent) { |
731 parent->RecomputeBounds(); | 734 parent->RecomputeBounds(); |
732 } | 735 } |
733 | 736 |
734 // Now re-insert each of the orphaned nodes back into the tree. | 737 // Now re-insert each of the orphaned nodes back into the tree. |
735 for (size_t i = 0; i < orphans.size(); ++i) { | 738 for (size_t i = 0; i < orphans.size(); ++i) { |
736 int starting_level = -1; | 739 int starting_level = -1; |
737 InsertNode(orphans[i], &starting_level); | 740 InsertNode(orphans[i], &starting_level); |
738 } | 741 } |
739 | 742 |
740 // Clear out the orphans list without deleting any of the children, as they | 743 // Clear out the orphans list without deleting any of the children, as they |
741 // have been re-inserted into the tree. | 744 // have been re-inserted into the tree. |
742 orphans.weak_clear(); | 745 orphans.weak_clear(); |
743 } | 746 } |
744 | 747 |
745 } // namespace gfx | 748 } // namespace gfx |
OLD | NEW |