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

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: Remove*() returns a scoped ptr Created 6 years, 6 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
« no previous file with comments | « ui/gfx/geometry/r_tree_base.h ('k') | ui/gfx/geometry/r_tree_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::AppendIntersectingRecords(
56 const Rect& query_rect, Records* matches_out) const {
57 if (rect().Intersects(query_rect))
58 matches_out->push_back(this);
59 }
60
61 void RTreeBase::RecordBase::AppendAllRecords(Records* matches_out) const {
62 matches_out->push_back(this);
63 }
64
65 scoped_ptr<RTreeBase::NodeBase>
66 RTreeBase::RecordBase::RemoveAndReturnLastChild() {
67 return scoped_ptr<NodeBase>();
68 }
69
70 const int RTreeBase::RecordBase::Level() const {
71 return -1;
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::AppendIntersectingRecords(
91 const Rect& query_rect, 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 AppendAllRecords(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)->AppendIntersectingRecords(query_rect, matches_out);
108 }
109
110 void RTreeBase::Node::AppendAllRecords(Records* matches_out) const {
111 for (Nodes::const_iterator i = children_.begin(); i != children_.end(); ++i)
112 (*i)->AppendAllRecords(matches_out);
113 }
114
115 void RTreeBase::Node::RemoveNodesForReinsert(size_t number_to_remove,
116 Nodes* nodes) {
117 DCHECK_LE(number_to_remove, children_.size());
118
119 std::partial_sort(children_.begin(),
120 children_.begin() + number_to_remove,
121 children_.end(),
122 &RTreeBase::Node::CompareCenterDistanceFromParent);
123
124 // Move the lowest-distance nodes to the returned vector.
125 nodes->insert(
126 nodes->end(), children_.begin(), children_.begin() + number_to_remove);
127 children_.weak_erase(children_.begin(), children_.begin() + number_to_remove);
128 }
129
130 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::RemoveChild(
131 NodeBase* child_node, Nodes* orphans) {
132 DCHECK_EQ(this, child_node->parent());
133
134 scoped_ptr<NodeBase> orphan(child_node->RemoveAndReturnLastChild());
135 while (orphan) {
136 orphans->push_back(orphan.release());
137 orphan = child_node->RemoveAndReturnLastChild();
138 }
139
140 Nodes::iterator i = std::find(children_.begin(), children_.end(), child_node);
141 DCHECK(i != children_.end());
142 children_.weak_erase(i);
143
144 return scoped_ptr<NodeBase>(child_node);
145 }
146
147 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::RemoveAndReturnLastChild() {
148 if (children_.empty())
149 return scoped_ptr<NodeBase>();
150
151 scoped_ptr<NodeBase> last_child(children_.back());
152 children_.weak_erase(children_.end() - 1);
153 last_child->set_parent(NULL);
154 return last_child.Pass();
155 }
156
157 RTreeBase::Node* RTreeBase::Node::ChooseSubtree(NodeBase* node) {
158 DCHECK(node);
159 // Should never be called on a node at equal or lower level in the tree than
160 // the node to insert.
161 DCHECK_GT(level_, node->Level());
162
163 // If we are a parent of nodes on the provided node level, we are done.
164 if (level_ == node->Level() + 1)
165 return this;
166
167 // Precompute a vector of expanded rects, used by both LeastOverlapIncrease
168 // and LeastAreaEnlargement.
169 Rects expanded_rects;
170 expanded_rects.reserve(children_.size());
171 for (Nodes::iterator i = children_.begin(); i != children_.end(); ++i)
172 expanded_rects.push_back(UnionRects(node->rect(), (*i)->rect()));
173
174 Node* best_candidate = NULL;
175 // For parents of leaf nodes, we pick the node that will cause the least
176 // increase in overlap by the addition of this new node. This may detect a
177 // tie, in which case it will return NULL.
178 if (level_ == 1)
179 best_candidate = LeastOverlapIncrease(node->rect(), expanded_rects);
180
181 // For non-parents of leaf nodes, or for parents of leaf nodes with ties in
182 // overlap increase, we choose the subtree with least area enlargement caused
183 // by the addition of the new node.
184 if (!best_candidate)
185 best_candidate = LeastAreaEnlargement(node->rect(), expanded_rects);
186
187 DCHECK(best_candidate);
188 return best_candidate->ChooseSubtree(node);
189 }
190
191 size_t RTreeBase::Node::AddChild(scoped_ptr<NodeBase> node) {
192 DCHECK(node);
193 // Sanity-check that the level of the child being added is one less than ours.
194 DCHECK_EQ(level_ - 1, node->Level());
195 node->set_parent(this);
196 set_rect(UnionRects(rect(), node->rect()));
197 children_.push_back(node.release());
198 return children_.size();
199 }
200
201 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::Split(size_t min_children,
202 size_t max_children) {
203 // We should have too many children to begin with.
204 DCHECK_EQ(max_children + 1, children_.size());
205
206 // Determine if we should split along the horizontal or vertical axis.
207 std::vector<NodeBase*> vertical_sort(children_.get());
208 std::vector<NodeBase*> horizontal_sort(children_.get());
209 std::sort(vertical_sort.begin(),
210 vertical_sort.end(),
211 &RTreeBase::Node::CompareVertical);
212 std::sort(horizontal_sort.begin(),
213 horizontal_sort.end(),
214 &RTreeBase::Node::CompareHorizontal);
215
216 Rects low_vertical_bounds;
217 Rects low_horizontal_bounds;
218 BuildLowBounds(vertical_sort,
219 horizontal_sort,
220 &low_vertical_bounds,
221 &low_horizontal_bounds);
222
223 Rects high_vertical_bounds;
224 Rects high_horizontal_bounds;
225 BuildHighBounds(vertical_sort,
226 horizontal_sort,
227 &high_vertical_bounds,
228 &high_horizontal_bounds);
229
230 // Choose |end_index| such that both Nodes after the split will have
231 // min_children <= children_.size() <= max_children.
232 size_t end_index = std::min(max_children, children_.size() - min_children);
233 bool is_vertical_split =
234 SmallestMarginSum(min_children,
235 end_index,
236 low_horizontal_bounds,
237 high_horizontal_bounds) <
238 SmallestMarginSum(min_children,
239 end_index,
240 low_vertical_bounds,
241 high_vertical_bounds);
242
243 // Choose split index along chosen axis and perform the split.
244 const Rects& low_bounds(
245 is_vertical_split ? low_vertical_bounds : low_horizontal_bounds);
246 const Rects& high_bounds(
247 is_vertical_split ? high_vertical_bounds : high_horizontal_bounds);
248 size_t split_index =
249 ChooseSplitIndex(min_children, end_index, low_bounds, high_bounds);
250
251 const std::vector<NodeBase*>& sort(
252 is_vertical_split ? vertical_sort : horizontal_sort);
253 return DivideChildren(low_bounds, high_bounds, sort, split_index);
254 }
255
256 const int RTreeBase::Node::Level() const {
257 return level_;
258 }
259
260 RTreeBase::Node::Node(int level) : NodeBase(Rect(), NULL), level_(level) {
261 }
262
263 // static
264 bool RTreeBase::Node::CompareVertical(const NodeBase* a, const NodeBase* b) {
265 const Rect& a_rect = a->rect();
266 const Rect& b_rect = b->rect();
267 return (a_rect.y() < b_rect.y()) ||
268 ((a_rect.y() == b_rect.y()) && (a_rect.height() < b_rect.height()));
269 }
270
271 // static
272 bool RTreeBase::Node::CompareHorizontal(const NodeBase* a, const NodeBase* b) {
273 const Rect& a_rect = a->rect();
274 const Rect& b_rect = b->rect();
275 return (a_rect.x() < b_rect.x()) ||
276 ((a_rect.x() == b_rect.x()) && (a_rect.width() < b_rect.width()));
277 }
278
279 // static
280 bool RTreeBase::Node::CompareCenterDistanceFromParent(const NodeBase* a,
281 const NodeBase* b) {
282 const NodeBase* p = a->const_parent();
283
284 DCHECK(p);
285 DCHECK_EQ(p, b->const_parent());
286
287 Vector2d p_center = CenterOfRect(p->rect());
288 Vector2d a_center = CenterOfRect(a->rect());
289 Vector2d b_center = CenterOfRect(b->rect());
290
291 // We don't bother with square roots because we are only comparing the two
292 // values for sorting purposes.
293 return (a_center - p_center).LengthSquared() <
294 (b_center - p_center).LengthSquared();
295 }
296
297 // static
298 void RTreeBase::Node::BuildLowBounds(
299 const std::vector<NodeBase*>& vertical_sort,
300 const std::vector<NodeBase*>& horizontal_sort,
301 Rects* vertical_bounds,
302 Rects* horizontal_bounds) {
303 Rect vertical_bounds_rect;
304 vertical_bounds->reserve(vertical_sort.size());
305 for (std::vector<NodeBase*>::const_iterator i = vertical_sort.begin();
306 i != vertical_sort.end();
307 ++i) {
308 vertical_bounds_rect.Union((*i)->rect());
309 vertical_bounds->push_back(vertical_bounds_rect);
310 }
311
312 Rect horizontal_bounds_rect;
313 horizontal_bounds->reserve(horizontal_sort.size());
314 for (std::vector<NodeBase*>::const_iterator i = horizontal_sort.begin();
315 i != horizontal_sort.end();
316 ++i) {
317 horizontal_bounds_rect.Union((*i)->rect());
318 horizontal_bounds->push_back(horizontal_bounds_rect);
319 }
320 }
321
322 // static
323 void RTreeBase::Node::BuildHighBounds(
324 const std::vector<NodeBase*>& vertical_sort,
325 const std::vector<NodeBase*>& horizontal_sort,
326 Rects* vertical_bounds,
327 Rects* horizontal_bounds) {
328 Rect vertical_bounds_rect;
329 vertical_bounds->reserve(vertical_sort.size());
330 for (std::vector<NodeBase*>::const_reverse_iterator i =
331 vertical_sort.rbegin();
332 i != vertical_sort.rend();
333 ++i) {
334 vertical_bounds_rect.Union((*i)->rect());
335 vertical_bounds->push_back(vertical_bounds_rect);
336 }
337 std::reverse(vertical_bounds->begin(), vertical_bounds->end());
338
339 Rect horizontal_bounds_rect;
340 horizontal_bounds->reserve(horizontal_sort.size());
341 for (std::vector<NodeBase*>::const_reverse_iterator i =
342 horizontal_sort.rbegin();
343 i != horizontal_sort.rend();
344 ++i) {
345 horizontal_bounds_rect.Union((*i)->rect());
346 horizontal_bounds->push_back(horizontal_bounds_rect);
347 }
348 std::reverse(horizontal_bounds->begin(), horizontal_bounds->end());
349 }
350
351 size_t RTreeBase::Node::ChooseSplitIndex(size_t start_index,
352 size_t end_index,
353 const Rects& low_bounds,
354 const Rects& high_bounds) {
355 DCHECK_EQ(low_bounds.size(), high_bounds.size());
356
357 int smallest_overlap_area = UnionRects(low_bounds[start_index],
358 high_bounds[start_index]).size().GetArea();
359 int smallest_combined_area = low_bounds[start_index].size().GetArea() +
360 high_bounds[start_index].size().GetArea();
361 size_t optimal_split_index = start_index;
362 for (size_t p = start_index + 1; p < end_index; ++p) {
363 const int overlap_area =
364 UnionRects(low_bounds[p], high_bounds[p]).size().GetArea();
365 const int combined_area =
366 low_bounds[p].size().GetArea() + high_bounds[p].size().GetArea();
367 if ((overlap_area < smallest_overlap_area) ||
368 ((overlap_area == smallest_overlap_area) &&
369 (combined_area < smallest_combined_area))) {
370 smallest_overlap_area = overlap_area;
371 smallest_combined_area = combined_area;
372 optimal_split_index = p;
373 }
374 }
375
376 // optimal_split_index currently points at the last element in the first set,
377 // so advance it by 1 to point at the first element in the second set.
378 return optimal_split_index + 1;
379 }
380
381 // static
382 int RTreeBase::Node::SmallestMarginSum(size_t start_index,
383 size_t end_index,
384 const Rects& low_bounds,
385 const Rects& high_bounds) {
386 DCHECK_EQ(low_bounds.size(), high_bounds.size());
387 DCHECK_LT(start_index, low_bounds.size());
388 DCHECK_LE(start_index, end_index);
389 DCHECK_LE(end_index, low_bounds.size());
390 Rects::const_iterator i(low_bounds.begin() + start_index);
391 Rects::const_iterator j(high_bounds.begin() + start_index);
392 int smallest_sum = i->width() + i->height() + j->width() + j->height();
393 for (; i != (low_bounds.begin() + end_index); ++i, ++j)
394 smallest_sum = std::min(
395 smallest_sum, i->width() + i->height() + j->width() + j->height());
396
397 return smallest_sum;
398 }
399
400 void RTreeBase::Node::RecomputeLocalBounds() {
401 Rect bounds;
402 for (size_t i = 0; i < children_.size(); ++i)
403 bounds.Union(children_[i]->rect());
404
405 set_rect(bounds);
406 }
407
408 int RTreeBase::Node::OverlapIncreaseToAdd(const Rect& rect,
409 const NodeBase* candidate_node,
410 const Rect& expanded_rect) const {
411 DCHECK(candidate_node);
412
413 // Early-out when |rect| is contained completely within |candidate|.
414 if (candidate_node->rect().Contains(rect))
415 return 0;
416
417 int total_original_overlap = 0;
418 int total_expanded_overlap = 0;
419
420 // Now calculate overlap with all other rects in this node.
421 for (Nodes::const_iterator it = children_.begin();
422 it != children_.end(); ++it) {
423 // Skip calculating overlap with the candidate rect.
424 if ((*it) == candidate_node)
425 continue;
426 NodeBase* overlap_node = (*it);
427 total_original_overlap += IntersectRects(
428 candidate_node->rect(), overlap_node->rect()).size().GetArea();
429 Rect expanded_overlap_rect = expanded_rect;
430 expanded_overlap_rect.Intersect(overlap_node->rect());
431 total_expanded_overlap += expanded_overlap_rect.size().GetArea();
432 }
433
434 return total_expanded_overlap - total_original_overlap;
435 }
436
437 scoped_ptr<RTreeBase::NodeBase> RTreeBase::Node::DivideChildren(
438 const Rects& low_bounds,
439 const Rects& high_bounds,
440 const std::vector<NodeBase*>& sorted_children,
441 size_t split_index) {
442 DCHECK_EQ(low_bounds.size(), high_bounds.size());
443 DCHECK_EQ(low_bounds.size(), sorted_children.size());
444 DCHECK_LT(split_index, low_bounds.size());
445 DCHECK_GT(split_index, 0U);
446
447 scoped_ptr<Node> sibling(new Node(level_));
448 sibling->set_parent(parent());
449 set_rect(low_bounds[split_index - 1]);
450 sibling->set_rect(high_bounds[split_index]);
451
452 // Our own children_ vector is unsorted, so we wipe it out and divide the
453 // sorted bounds rects between ourselves and our sibling.
454 children_.weak_clear();
455 children_.insert(children_.end(),
456 sorted_children.begin(),
457 sorted_children.begin() + split_index);
458 sibling->children_.insert(sibling->children_.end(),
459 sorted_children.begin() + split_index,
460 sorted_children.end());
461
462 for (size_t i = 0; i < sibling->children_.size(); ++i)
463 sibling->children_[i]->set_parent(sibling.get());
464
465 return sibling.Pass();
466 }
467
468 RTreeBase::Node* RTreeBase::Node::LeastOverlapIncrease(
469 const Rect& node_rect,
470 const Rects& expanded_rects) {
471 NodeBase* best_node = children_.front();
472 int least_overlap_increase =
473 OverlapIncreaseToAdd(node_rect, children_[0], expanded_rects[0]);
474 for (size_t i = 1; i < children_.size(); ++i) {
475 int overlap_increase =
476 OverlapIncreaseToAdd(node_rect, children_[i], expanded_rects[i]);
477 if (overlap_increase < least_overlap_increase) {
478 least_overlap_increase = overlap_increase;
479 best_node = children_[i];
480 } else if (overlap_increase == least_overlap_increase) {
481 // If we are tied at zero there is no possible better overlap increase,
482 // so we can report a tie early.
483 if (overlap_increase == 0)
484 return NULL;
485
486 best_node = NULL;
487 }
488 }
489
490 // Ensure that our children are always Nodes and not Records.
491 DCHECK_GE(level_, 1);
492 return static_cast<Node*>(best_node);
493 }
494
495 RTreeBase::Node* RTreeBase::Node::LeastAreaEnlargement(
496 const Rect& node_rect,
497 const Rects& expanded_rects) {
498 DCHECK(!children_.empty());
499 DCHECK_EQ(children_.size(), expanded_rects.size());
500
501 NodeBase* best_node = children_.front();
502 int least_area_enlargement =
503 expanded_rects[0].size().GetArea() - best_node->rect().size().GetArea();
504 for (size_t i = 1; i < children_.size(); ++i) {
505 NodeBase* candidate_node = children_[i];
506 int area_change = expanded_rects[i].size().GetArea() -
507 candidate_node->rect().size().GetArea();
508 DCHECK_GE(area_change, 0);
509 if (area_change < least_area_enlargement) {
510 best_node = candidate_node;
511 least_area_enlargement = area_change;
512 } else if (area_change == least_area_enlargement &&
513 candidate_node->rect().size().GetArea() <
514 best_node->rect().size().GetArea()) {
515 // Ties are broken by choosing the entry with the least area.
516 best_node = candidate_node;
517 }
518 }
519
520 // Ensure that our children are always Nodes and not Records.
521 DCHECK_GE(level_, 1);
522 return static_cast<Node*>(best_node);
523 }
524
525
526 // RTreeBase ------------------------------------------------------------------
527
528 RTreeBase::RTreeBase(size_t min_children, size_t max_children)
529 : root_(new Node()),
530 min_children_(min_children),
531 max_children_(max_children) {
532 DCHECK_GE(min_children_, 2U);
533 DCHECK_LE(min_children_, max_children_ / 2U);
534 }
535
536 RTreeBase::~RTreeBase() {
537 }
538
539 void RTreeBase::InsertNode(
540 scoped_ptr<NodeBase> node, int* highest_reinsert_level) {
541 // Find the most appropriate parent to insert node into.
542 Node* parent = root_->ChooseSubtree(node.get());
543 DCHECK(parent);
544 // Verify ChooseSubtree returned a Node at the correct level.
545 DCHECK_EQ(parent->Level(), node->Level() + 1);
546 Node* insert_parent = static_cast<Node*>(parent);
547 NodeBase* needs_bounds_recomputed = insert_parent->parent();
548 Nodes reinserts;
549 // Attempt to insert the Node, if this overflows the Node we must handle it.
550 while (insert_parent &&
551 insert_parent->AddChild(node.Pass()) > max_children_) {
552 // If we have yet to re-insert nodes at this level during this data insert,
553 // and we're not at the root, R*-Tree calls for re-insertion of some of the
554 // nodes, resulting in a better balance on the tree.
555 if (insert_parent->parent() &&
556 insert_parent->Level() > *highest_reinsert_level) {
557 insert_parent->RemoveNodesForReinsert(max_children_ / 3, &reinserts);
558 // Adjust highest_reinsert_level to this level.
559 *highest_reinsert_level = insert_parent->Level();
560 // RemoveNodesForReinsert() does not recompute bounds, so mark it.
561 needs_bounds_recomputed = insert_parent;
562 break;
563 }
564
565 // Split() will create a sibling to insert_parent both of which will have
566 // valid bounds, but this invalidates their parent's bounds.
567 node = insert_parent->Split(min_children_, max_children_);
568 insert_parent = static_cast<Node*>(insert_parent->parent());
569 needs_bounds_recomputed = insert_parent;
570 }
571
572 // If we have a Node to insert, and we hit the root of the current tree,
573 // we create a new root which is the parent of the current root and the
574 // insert_node. Note that we must release() the |root_| since
575 // ConstructParent() will take ownership of it.
576 if (!insert_parent && node) {
577 root_ = root_.release()->ConstructParent();
578 root_->AddChild(node.Pass());
579 }
580
581 // Recompute bounds along insertion path.
582 if (needs_bounds_recomputed)
583 needs_bounds_recomputed->RecomputeBoundsUpToRoot();
584
585 // Complete re-inserts, if any. The algorithm only allows for one invocation
586 // of RemoveNodesForReinsert() per level of the tree in an overall call to
587 // Insert().
588 while (reinserts.size()) {
589 Nodes::iterator last_element = reinserts.end() - 1;
590 scoped_ptr<NodeBase> reinsert_node(*last_element);
591 reinserts.weak_erase(last_element);
592 InsertNode(reinsert_node.Pass(), highest_reinsert_level);
luken 2014/06/03 21:04:59 so this technique of extracting a pointer from a S
Peter Kasting 2014/06/03 21:20:57 Yeah, this is one downside of the ScopedVector API
luken 2014/06/03 21:25:01 Thanks, I switched it.
593 }
594 }
595
596 scoped_ptr<RTreeBase::NodeBase> RTreeBase::RemoveNode(NodeBase* node) {
597 // We need to remove this node from its parent.
598 Node* parent = static_cast<Node*>(node->parent());
599 // Record nodes are never allowed as the root, so we should always have a
600 // parent.
601 DCHECK(parent);
602 // Should always be a leaf that had the record.
603 DCHECK_EQ(0, parent->Level());
604
605 Nodes orphans;
606 scoped_ptr<NodeBase> removed_node(parent->RemoveChild(node, &orphans));
607
608 // It's possible that by removing |node| from |parent| we have made |parent|
609 // have less than the minimum number of children, in which case we will need
610 // to remove and delete |parent| while reinserting any other children that it
611 // had. We traverse up the tree doing this until we remove a child from a
612 // parent that still has greater than or equal to the minimum number of Nodes.
613 while (parent->count() < min_children_) {
614 NodeBase* child = parent;
615 parent = static_cast<Node*>(parent->parent());
616
617 // If we've hit the root, stop.
618 if (!parent)
619 break;
620
621 scoped_ptr<NodeBase> removed_child(parent->RemoveChild(child, &orphans));
622 }
623
624 // If we stopped deleting nodes up the tree before encountering the root,
625 // we'll need to fix up the bounds from the first parent we didn't delete
626 // up to the root.
627 if (parent)
628 parent->RecomputeBoundsUpToRoot();
629 else
630 root_->RecomputeBoundsUpToRoot();
631
632 while (orphans.size()) {
633 Nodes::iterator last_element = orphans.end() - 1;
634 scoped_ptr<NodeBase> orphan(*last_element);
635 orphans.weak_erase(last_element);
636 int starting_level = -1;
637 InsertNode(orphan.Pass(), &starting_level);
638 }
639
640 return removed_node.Pass();
641 }
642
643 void RTreeBase::PruneRoot() {
644 root_.reset(
645 static_cast<Node*>(root_->RemoveAndReturnLastChild().release()));
646 }
647
648 void RTreeBase::ResetRoot() {
649 root_.reset(new Node());
650 }
651
652 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/r_tree_base.h ('k') | ui/gfx/geometry/r_tree_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698