Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(873)

Side by Side Diff: ui/gfx/geometry/r_tree_base.cc

Issue 269513002: readability review for luken (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/gfx/geometry/r_tree_base.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11
12 // Helpers --------------------------------------------------------------------
13
14 namespace {
15
16 // Returns a Vector2d to allow us to do arithmetic on the result such as
17 // computing distances between centers.
18 gfx::Vector2d CenterOfRect(const gfx::Rect& rect) {
19 return rect.OffsetFromOrigin() +
20 gfx::Vector2d(rect.width() / 2, rect.height() / 2);
21 }
22
23 }
24
25 namespace gfx {
26
27
28 // RTreeBase::NodeBase --------------------------------------------------------
29
30 RTreeBase::NodeBase::~NodeBase() {
31 }
32
33 void RTreeBase::NodeBase::RecomputeBoundsUpToRoot() {
34 RecomputeLocalBounds();
35 if (parent_)
36 parent_->RecomputeBoundsUpToRoot();
37 }
38
39 RTreeBase::NodeBase::NodeBase(const Rect& rect, NodeBase* parent)
40 : rect_(rect),
41 parent_(parent) {
42 }
43
44 void RTreeBase::NodeBase::RecomputeLocalBounds() {
45 }
46
47 // RTreeBase::RecordBase ------------------------------------------------------
48
49 RTreeBase::RecordBase::RecordBase(const Rect& rect) : NodeBase(rect, NULL) {
50 }
51
52 RTreeBase::RecordBase::~RecordBase() {
53 }
54
55 void RTreeBase::RecordBase::Query(const Rect& query_rect,
56 Records* matches_out) const {
57 if (rect().Intersects(query_rect))
58 matches_out->push_back(this);
59 }
60
61 scoped_ptr<RTreeBase::NodeBase>
62 RTreeBase::RecordBase::RemoveAndReturnLastChild() {
63 return scoped_ptr<NodeBase>();
64 }
65
66 const int RTreeBase::RecordBase::Level() const {
67 return -1;
68 }
69
70 void RTreeBase::RecordBase::GetAllValues(Records* matches_out) const {
71 matches_out->push_back(this);
72 }
73
74
75 // RTreeBase::Node ------------------------------------------------------------
76
77 RTreeBase::Node::Node() : NodeBase(Rect(), NULL), level_(0) {
78 }
79
80 RTreeBase::Node::~Node() {
81 }
82
83 scoped_ptr<RTreeBase::Node> RTreeBase::Node::ConstructParent() {
84 DCHECK(!parent());
85 scoped_ptr<Node> new_parent(new Node(level_ + 1));
86 new_parent->AddChild(scoped_ptr<NodeBase>(this));
87 return new_parent.Pass();
88 }
89
90 void RTreeBase::Node::Query(const Rect& query_rect,
91 Records* matches_out) const {
92 // Check own bounding box for intersection, can cull all children if no
93 // intersection.
94 if (!rect().Intersects(query_rect))
95 return;
96
97 // Conversely if we are completely contained within the query rect we can
98 // confidently skip all bounds checks for ourselves and all our children.
99 if (query_rect.Contains(rect())) {
100 GetAllValues(matches_out);
101 return;
102 }
103
104 // We intersect the query rect but we are not are not contained within it.
105 // We must query each of our children in turn.
106 for (Nodes::const_iterator i = children_.begin(); i != children_.end(); ++i)
107 (*i)->Query(query_rect, matches_out);
108 }
109
110 void RTreeBase::Node::RemoveNodesForReinsert(size_t number_to_remove,
111 Nodes* nodes) {
112 DCHECK_LE(number_to_remove, children_.size());
113
114 std::partial_sort(children_.begin(),
115 children_.begin() + number_to_remove,
116 children_.end(),
117 &RTreeBase::Node::CompareCenterDistanceFromParent);
118
119 // Move the lowest-distance nodes to the returned vector.
120 nodes->insert(
121 nodes->end(), children_.begin(), children_.begin() + number_to_remove);
122 children_.weak_erase(children_.begin(), children_.begin() + number_to_remove);
123 }
124
125 size_t RTreeBase::Node::RemoveChild(NodeBase* child_node, Nodes* orphans) {
126 DCHECK_EQ(this, child_node->parent());
127
128 scoped_ptr<NodeBase> orphan(child_node->RemoveAndReturnLastChild());
129 while (orphan) {
130 orphans->push_back(orphan.release());
131 orphan = child_node->RemoveAndReturnLastChild();
132 }
133
134 Nodes::iterator i = std::find(children_.begin(), children_.end(), child_node);
135 DCHECK(i != children_.end());
136 children_.weak_erase(i);
137
138 return children_.size();
139 }
140
141 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::RemoveAndReturnLastChild() {
142 if (children_.empty())
143 return scoped_ptr<NodeBase>();
144
145 scoped_ptr<NodeBase> last_child(children_.back());
146 children_.weak_erase(children_.end() - 1);
147 last_child->set_parent(NULL);
148 return last_child.Pass();
149 }
150
151 RTreeBase::Node* RTreeBase::Node::ChooseSubtree(NodeBase* node) {
152 DCHECK(node);
153 // Should never be called on a node at equal or lower level in the tree than
154 // the node to insert.
155 DCHECK_GT(level_, node->Level());
156
157 // If we are a parent of nodes on the provided node level, we are done.
158 if (level_ == node->Level() + 1)
159 return this;
160
161 // Precompute a vector of expanded rects, used by both LeastOverlapIncrease
162 // and LeastAreaEnlargement.
163 Rects expanded_rects;
164 expanded_rects.reserve(children_.size());
165 for (Nodes::iterator i = children_.begin(); i != children_.end(); ++i)
166 expanded_rects.push_back(UnionRects(node->rect(), (*i)->rect()));
167
168 Node* best_candidate = NULL;
169 // For parents of leaf nodes, we pick the node that will cause the least
170 // increase in overlap by the addition of this new node. This may detect a
171 // tie, in which case it will return NULL.
172 if (level_ == 1)
173 best_candidate = LeastOverlapIncrease(node->rect(), expanded_rects);
174
175 // For non-parents of leaf nodes, or for parents of leaf nodes with ties in
176 // overlap increase, we choose the subtree with least area enlargement caused
177 // by the addition of the new node.
178 if (!best_candidate)
179 best_candidate = LeastAreaEnlargement(node->rect(), expanded_rects);
180
181 DCHECK(best_candidate);
182 return best_candidate->ChooseSubtree(node);
183 }
184
185 size_t RTreeBase::Node::AddChild(scoped_ptr<NodeBase> node) {
186 DCHECK(node);
187 // Sanity-check that the level of the child being added is one less than ours.
188 DCHECK_EQ(level_ - 1, node->Level());
189 node->set_parent(this);
190 set_rect(UnionRects(rect(), node->rect()));
191 children_.push_back(node.release());
192 return children_.size();
193 }
194
195 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::Split(size_t min_children,
196 size_t max_children) {
197 // We should have too many children to begin with.
198 DCHECK_EQ(children_.size(), max_children + 1);
Peter Kasting 2014/05/29 00:32:26 Nit: (expected, actual)
luken 2014/05/30 16:51:04 Done.
199
200 // Determine if we should split along the horizontal or vertical axis.
201 std::vector<NodeBase*> vertical_sort(children_.get());
202 std::vector<NodeBase*> horizontal_sort(children_.get());
203 std::sort(vertical_sort.begin(),
204 vertical_sort.end(),
205 &RTreeBase::Node::CompareVertical);
206 std::sort(horizontal_sort.begin(),
207 horizontal_sort.end(),
208 &RTreeBase::Node::CompareHorizontal);
209
210 Rects low_vertical_bounds;
211 Rects low_horizontal_bounds;
212 BuildLowBounds(vertical_sort,
213 horizontal_sort,
214 &low_vertical_bounds,
215 &low_horizontal_bounds);
216
217 Rects high_vertical_bounds;
218 Rects high_horizontal_bounds;
219 BuildHighBounds(vertical_sort,
220 horizontal_sort,
221 &high_vertical_bounds,
222 &high_horizontal_bounds);
223
224 // Choose |end_index| such that both Nodes after the split will have
225 // min_children <= children_.size() <= max_children.
226 size_t end_index = std::min(max_children, children_.size() - min_children);
227 bool is_vertical_split =
228 SmallestMarginSum(min_children,
229 end_index,
230 low_horizontal_bounds,
231 high_horizontal_bounds) <
232 SmallestMarginSum(min_children,
233 end_index,
234 low_vertical_bounds,
235 high_vertical_bounds);
236
237 // Choose split index along chosen axis and perform the split.
238 const Rects& low_bounds(
239 is_vertical_split ? low_vertical_bounds : low_horizontal_bounds);
240 const Rects& high_bounds(
241 is_vertical_split ? high_vertical_bounds : high_horizontal_bounds);
242 size_t split_index =
243 ChooseSplitIndex(min_children, end_index, low_bounds, high_bounds);
244
245 const std::vector<NodeBase*>& sort(
246 is_vertical_split ? vertical_sort : horizontal_sort);
247 return DivideChildren(low_bounds, high_bounds, sort, split_index);
248 }
249
250 const int RTreeBase::Node::Level() const {
251 return level_;
252 }
253
254 RTreeBase::Node::Node(int level) : NodeBase(Rect(), NULL), level_(level) {
255 }
256
257 // static
258 bool RTreeBase::Node::CompareVertical(NodeBase* a, NodeBase* b) {
259 const Rect& a_rect = a->rect();
260 const Rect& b_rect = b->rect();
261 return (a_rect.y() < b_rect.y()) ||
262 ((a_rect.y() == b_rect.y()) && (a_rect.height() < b_rect.height()));
263 }
264
265 // static
266 bool RTreeBase::Node::CompareHorizontal(NodeBase* a, NodeBase* b) {
267 const Rect& a_rect = a->rect();
268 const Rect& b_rect = b->rect();
269 return (a_rect.x() < b_rect.x()) ||
270 ((a_rect.x() == b_rect.x()) && (a_rect.width() < b_rect.width()));
271 }
272
273 // static
274 bool RTreeBase::Node::CompareCenterDistanceFromParent(NodeBase* a,
275 NodeBase* b) {
276 const NodeBase* p = a->parent();
277
278 DCHECK(p);
279 DCHECK_EQ(p, b->parent());
280
281 Vector2d p_center = CenterOfRect(p->rect());
282 Vector2d a_center = CenterOfRect(a->rect());
283 Vector2d b_center = CenterOfRect(b->rect());
284
285 // We don't bother with square roots because we are only comparing the two
286 // values for sorting purposes.
287 return (a_center - p_center).LengthSquared() <
288 (b_center - p_center).LengthSquared();
289 }
290
291 // static
292 void RTreeBase::Node::BuildLowBounds(
293 const std::vector<NodeBase*>& vertical_sort,
294 const std::vector<NodeBase*>& horizontal_sort,
295 Rects* vertical_bounds,
296 Rects* horizontal_bounds) {
297 Rect vertical_bounds_rect;
298 vertical_bounds->reserve(vertical_sort.size());
299 for (std::vector<NodeBase*>::const_iterator i = vertical_sort.begin();
300 i != vertical_sort.end();
301 ++i) {
302 vertical_bounds_rect.Union((*i)->rect());
303 vertical_bounds->push_back(vertical_bounds_rect);
304 }
305
306 Rect horizontal_bounds_rect;
307 horizontal_bounds->reserve(horizontal_sort.size());
308 for (std::vector<NodeBase*>::const_iterator i = horizontal_sort.begin();
309 i != horizontal_sort.end();
310 ++i) {
311 horizontal_bounds_rect.Union((*i)->rect());
312 horizontal_bounds->push_back(horizontal_bounds_rect);
313 }
314 }
315
316 // static
317 void RTreeBase::Node::BuildHighBounds(
318 const std::vector<NodeBase*>& vertical_sort,
319 const std::vector<NodeBase*>& horizontal_sort,
320 Rects* vertical_bounds,
321 Rects* horizontal_bounds) {
322 Rect vertical_bounds_rect;
323 vertical_bounds->reserve(vertical_sort.size());
324 for (std::vector<NodeBase*>::const_reverse_iterator i =
325 vertical_sort.rbegin();
326 i != vertical_sort.rend();
327 ++i) {
328 vertical_bounds_rect.Union((*i)->rect());
329 vertical_bounds->push_back(vertical_bounds_rect);
330 }
331 std::reverse(vertical_bounds->begin(), vertical_bounds->end());
332
333 Rect horizontal_bounds_rect;
334 horizontal_bounds->reserve(horizontal_sort.size());
335 for (std::vector<NodeBase*>::const_reverse_iterator i =
336 horizontal_sort.rbegin();
337 i != horizontal_sort.rend();
338 ++i) {
339 horizontal_bounds_rect.Union((*i)->rect());
340 horizontal_bounds->push_back(horizontal_bounds_rect);
341 }
342 std::reverse(horizontal_bounds->begin(), horizontal_bounds->end());
343 }
344
345 size_t RTreeBase::Node::ChooseSplitIndex(size_t start_index,
346 size_t end_index,
347 const Rects& low_bounds,
348 const Rects& high_bounds) {
349 DCHECK_EQ(low_bounds.size(), high_bounds.size());
350
351 int smallest_overlap_area =
352 UnionRects(low_bounds[start_index],
353 high_bounds[start_index]).size().GetArea();
Peter Kasting 2014/05/29 00:32:26 Nit: I would probably indent this line even, or pe
luken 2014/05/30 16:51:04 Done.
354 int smallest_combined_area = low_bounds[start_index].size().GetArea() +
355 high_bounds[start_index].size().GetArea();
356 size_t optimal_split_index = start_index;
357 for (size_t p = start_index + 1; p < end_index; ++p) {
358 const int overlap_area =
359 UnionRects(low_bounds[p], high_bounds[p]).size().GetArea();
360 const int combined_area =
361 low_bounds[p].size().GetArea() + high_bounds[p].size().GetArea();
362 if ((overlap_area < smallest_overlap_area) ||
363 ((overlap_area == smallest_overlap_area) &&
364 (combined_area < smallest_combined_area))) {
365 smallest_overlap_area = overlap_area;
366 smallest_combined_area = combined_area;
367 optimal_split_index = p;
368 }
369 }
370
371 // optimal_split_index currently points at the last element in the first set,
372 // so advance it by 1 to point at the first element in the second set.
373 return optimal_split_index + 1;
374 }
375
376 // static
377 int RTreeBase::Node::SmallestMarginSum(size_t start_index,
378 size_t end_index,
379 const Rects& low_bounds,
380 const Rects& high_bounds) {
381 DCHECK_EQ(low_bounds.size(), high_bounds.size());
382 DCHECK_LT(start_index, low_bounds.size());
383 DCHECK_LE(start_index, end_index);
384 DCHECK_LE(end_index, low_bounds.size());
385 Rects::const_iterator i(low_bounds.begin() + start_index);
386 Rects::const_iterator j(high_bounds.begin() + start_index);
387 int smallest_sum = i->width() + i->height() + j->width() + j->height();
388 for (; i != (low_bounds.begin() + end_index); ++i, ++j) {
389 smallest_sum = std::min(
390 smallest_sum, i->width() + i->height() + j->width() + j->height());
391 }
392 return smallest_sum;
393 }
394
395 void RTreeBase::Node::RecomputeLocalBounds() {
396 Rect bounds;
397 for (size_t i = 0; i < children_.size(); ++i) {
Peter Kasting 2014/05/29 00:32:26 Nit: No {} (since you don't use it everywhere with
luken 2014/05/30 16:51:04 Done.
398 bounds.Union(children_[i]->rect());
399 }
400 set_rect(bounds);
401 }
402
403 void RTreeBase::Node::GetAllValues(Records* matches_out) const {
404 for (Nodes::const_iterator i = children_.begin(); i != children_.end(); ++i) {
405 (*i)->GetAllValues(matches_out);
406 }
407 }
408
409 int RTreeBase::Node::OverlapIncreaseToAdd(const Rect& rect,
410 const NodeBase* candidate_node,
411 const Rect& expanded_rect) const {
412 DCHECK(candidate_node);
413
414 // Early-out when |rect| is contained completely within |candidate|.
415 if (candidate_node->rect().Contains(rect))
416 return 0;
417
418 int total_original_overlap = 0;
419 int total_expanded_overlap = 0;
420
421 // Now calculate overlap with all other rects in this node.
422 for (Nodes::const_iterator it = children_.begin();
423 it != children_.end(); ++it) {
424 // Skip calculating overlap with the candidate rect.
425 if ((*it) == candidate_node)
426 continue;
427 NodeBase* overlap_node = (*it);
428 total_original_overlap += IntersectRects(
429 candidate_node->rect(), overlap_node->rect()).size().GetArea();
430 Rect expanded_overlap_rect = expanded_rect;
431 expanded_overlap_rect.Intersect(overlap_node->rect());
432 total_expanded_overlap += expanded_overlap_rect.size().GetArea();
433 }
434
435 return total_expanded_overlap - total_original_overlap;
436 }
437
438 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::DivideChildren(
439 const Rects& low_bounds,
440 const Rects& high_bounds,
441 const std::vector<NodeBase*>& sorted_children,
442 size_t split_index) {
443 DCHECK_EQ(low_bounds.size(), high_bounds.size());
444 DCHECK_EQ(low_bounds.size(), sorted_children.size());
445 DCHECK_LT(split_index, low_bounds.size());
446 DCHECK_GT(split_index, 0U);
447
448 Node* sibling = new Node(level_);
Peter Kasting 2014/05/29 00:32:26 Declare |sibling| as a scoped_ptr here rather than
luken 2014/05/30 16:51:04 Done.
449 sibling->set_parent(parent());
450 set_rect(low_bounds[split_index - 1]);
451 sibling->set_rect(high_bounds[split_index]);
452
453 // Our own children_ vector is unsorted, so we wipe it out and divide the
454 // sorted bounds rects between ourselves and our sibling.
455 children_.weak_clear();
456 children_.insert(children_.end(),
457 sorted_children.begin(),
458 sorted_children.begin() + split_index);
459 sibling->children_.insert(sibling->children_.end(),
460 sorted_children.begin() + split_index,
461 sorted_children.end());
462
463 for (size_t i = 0; i < sibling->children_.size(); ++i)
464 sibling->children_[i]->set_parent(sibling);
465
466 return scoped_ptr<NodeBase>(sibling);
467 }
468
469 RTreeBase::Node* RTreeBase::Node::LeastOverlapIncrease(
470 const Rect& node_rect,
471 const Rects& expanded_rects) {
472 NodeBase* best_node = children_.front();
473 int least_overlap_increase =
474 OverlapIncreaseToAdd(node_rect, children_[0], expanded_rects[0]);
475 for (size_t i = 1; i < children_.size(); ++i) {
476 int overlap_increase =
477 OverlapIncreaseToAdd(node_rect, children_[i], expanded_rects[i]);
478 if (overlap_increase < least_overlap_increase) {
479 least_overlap_increase = overlap_increase;
480 best_node = children_[i];
481 } else if (overlap_increase == least_overlap_increase) {
482 // If we are tied at zero there is no possible better overlap increase,
483 // so we can report a tie early.
484 if (overlap_increase == 0)
485 return NULL;
486
487 best_node = NULL;
488 }
489 }
490
491 // Ensure that our children are always Nodes and not Records.
492 DCHECK_GE(level_, 1);
493 return static_cast<Node*>(best_node);
494 }
495
496 RTreeBase::Node* RTreeBase::Node::LeastAreaEnlargement(
497 const Rect& node_rect,
498 const Rects& expanded_rects) {
499 DCHECK(!children_.empty());
500 DCHECK_EQ(children_.size(), expanded_rects.size());
501
502 NodeBase* best_node = children_.front();
503 int least_area_enlargement =
504 expanded_rects[0].size().GetArea() - best_node->rect().size().GetArea();
505 for (size_t i = 1; i < children_.size(); ++i) {
506 NodeBase* candidate_node = children_[i];
507 int area_change = expanded_rects[i].size().GetArea() -
508 candidate_node->rect().size().GetArea();
509 DCHECK_GE(area_change, 0);
510 if (area_change < least_area_enlargement) {
511 best_node = candidate_node;
512 least_area_enlargement = area_change;
513 } else if (area_change == least_area_enlargement &&
514 candidate_node->rect().size().GetArea() <
Peter Kasting 2014/05/29 00:32:26 Nit: I would probably indent this line 4 rather th
luken 2014/05/30 16:51:04 Done.
515 best_node->rect().size().GetArea()) {
516 // Ties are broken by choosing the entry with the least area.
517 best_node = candidate_node;
518 }
519 }
520
521 // Ensure that our children are always Nodes and not Records.
522 DCHECK_GE(level_, 1);
523 return static_cast<Node*>(best_node);
524 }
525
526
527 // RTreeBase ------------------------------------------------------------------
528
529 RTreeBase::RTreeBase(size_t min_children, size_t max_children)
530 : root_(new Node()),
531 min_children_(min_children),
532 max_children_(max_children) {
533 DCHECK_GE(min_children_, 2U);
534 DCHECK_LE(min_children_, max_children_ / 2U);
535 }
536
537 RTreeBase::~RTreeBase() {
538 }
539
540 void RTreeBase::InsertNode(NodeBase* node, int* highest_reinsert_level) {
541 // Find the most appropriate parent to insert node into.
542 Node* parent = root_->ChooseSubtree(node);
543 DCHECK(parent);
544 // Verify ChooseSubtree returned a Node at the correct level.
545 DCHECK_EQ(parent->Level(), node->Level() + 1);
546 scoped_ptr<NodeBase> insert_node(node);
547 Node* insert_parent = static_cast<Node*>(parent);
548 NodeBase* needs_bounds_recomputed = insert_parent->parent();
549 Nodes reinserts;
550 // Attempt to insert the Node, if this overflows the Node we must handle it.
551 while (insert_parent &&
552 insert_parent->AddChild(insert_node.Pass()) > max_children_) {
553 // If we have yet to re-insert nodes at this level during this data insert,
554 // and we're not at the root, R*-Tree calls for re-insertion of some of the
555 // nodes, resulting in a better balance on the tree.
556 if (insert_parent->parent() &&
557 insert_parent->Level() > *highest_reinsert_level) {
558 insert_parent->RemoveNodesForReinsert(max_children_ / 3, &reinserts);
559 // Adjust highest_reinsert_level to this level.
560 *highest_reinsert_level = insert_parent->Level();
561 // RemoveNodesForReinsert() does not recompute bounds, so mark it.
562 needs_bounds_recomputed = insert_parent;
563 break;
564 }
565
566 // Split() will create a sibling to insert_parent both of which will have
567 // valid bounds, but this invalidates their parent's bounds.
568 insert_node = insert_parent->Split(min_children_, max_children_);
569 insert_parent = static_cast<Node*>(insert_parent->parent());
570 needs_bounds_recomputed = insert_parent;
571 }
572
573 // If we have a Node to insert, and we hit the root of the current tree,
574 // we create a new root which is the parent of the current root and the
575 // insert_node.
576 if (!insert_parent && insert_node) {
577 root_ = root_.release()->ConstructParent();
Peter Kasting 2014/05/29 00:32:26 Nit: Because this is subtle, it's probably worth a
luken 2014/05/30 16:51:04 Done.
578 root_->AddChild(insert_node.Pass());
579 }
580
581 // Recompute bounds along insertion path.
582 if (needs_bounds_recomputed) {
583 needs_bounds_recomputed->RecomputeBoundsUpToRoot();
584 }
585
586 // Complete re-inserts, if any.
587 for (Nodes::iterator it = reinserts.begin(); it != reinserts.end(); ++it)
588 InsertNode(*it, highest_reinsert_level);
Peter Kasting 2014/05/29 00:32:26 So, passing |highest_reinsert_level| by pointer me
luken 2014/05/30 16:51:04 That is what I want. I added a comment in the decl
589
590 // Clear out reinserts without deleting any of the children, as they have been
591 // re-inserted into the tree.
592 reinserts.weak_clear();
593 }
594
595 void RTreeBase::RemoveNode(NodeBase* node) {
596 // We need to remove this node from its parent.
597 Node* parent = static_cast<Node*>(node->parent());
598 // Record nodes are never allowed as the root, so we should always have a
599 // parent.
600 DCHECK(parent);
601 // Should always be a leaf that had the record.
602 DCHECK_EQ(0, parent->Level());
603 ScopedVector<NodeBase> orphans;
604 NodeBase* child = node;
605
606 // Traverse up the tree, removing the child from each parent and deleting
607 // parent nodes, until we either encounter the root of the tree or a parent
608 // that still has sufficient children.
609 while (parent) {
610 size_t children_remaining = parent->RemoveChild(child, &orphans);
611 if (child != node)
612 delete child;
613
614 if (children_remaining >= min_children_)
615 break;
616
617 child = parent;
618 parent = static_cast<Node*>(parent->parent());
619 }
620
621 // If we stopped deleting nodes up the tree before encountering the root,
622 // we'll need to fix up the bounds from the first parent we didn't delete
623 // up to the root.
624 if (parent) {
Peter Kasting 2014/05/29 00:32:26 Nit: No {}
luken 2014/05/30 16:51:04 really? even with the else below? if you say so..
Peter Kasting 2014/05/30 18:51:18 There are two legal options: (1) Braces on all loo
luken 2014/05/30 19:23:39 Done.
625 parent->RecomputeBoundsUpToRoot();
626 } else {
627 root_->RecomputeBoundsUpToRoot();
628 }
629
630 // Now re-insert each of the orphaned nodes back into the tree.
631 for (size_t i = 0; i < orphans.size(); ++i) {
632 int starting_level = -1;
633 InsertNode(orphans[i], &starting_level);
634 }
635
636 // Clear out the orphans list without deleting any of the children, as they
637 // have been re-inserted into the tree.
638 orphans.weak_clear();
639 }
640
641 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698