OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 #ifndef UI_GFX_GEOMETRY_R_TREE_H_ | |
6 #define UI_GFX_GEOMETRY_R_TREE_H_ | |
7 | |
8 #include <list> | |
9 #include <map> | |
10 #include <set> | |
11 #include <vector> | |
12 | |
13 #include "base/containers/linked_list.h" | |
14 #include "base/macros.h" | |
15 #include "ui/gfx/geometry/rect.h" | |
16 #include "ui/gfx/gfx_export.h" | |
17 | |
18 namespace gfx { | |
19 | |
20 // Defines a heirarchical bounding rectangle data structure for Rect objects, | |
21 // associated with a generic unique key K for efficient spatial queries. The | |
22 // R*-tree algorithm is used to build the trees. Based on the papers: | |
23 // | |
24 // A Guttman 'R-trees: a dynamic index structure for spatial searching', Proc | |
25 // ACM SIGMOD Int Conf on Management of Data, 47-57, 1984 | |
26 // | |
27 // N Beckmann, H-P Kriegel, R Schneider, B Seeger 'The R*-tree: an efficient and | |
28 // robust access method for points and rectangles', Proc ACM SIGMOD Int Conf on | |
29 // Management of Data, 322-331, 1990 | |
30 class GFX_EXPORT RTree { | |
31 public: | |
32 RTree(size_t min_children, size_t max_children); | |
33 ~RTree(); | |
34 | |
35 // Insert a new rect into the tree, associated with provided key. Note that if | |
36 // rect is empty, this rect will not actually be inserted. Duplicate keys | |
37 // overwrite old entries. | |
38 void Insert(const Rect& rect, void* key); | |
39 | |
40 // If present, remove the supplied key from the tree. | |
41 void Remove(void* key); | |
42 | |
43 // Fills a supplied list matches_out with all keys having bounding rects | |
44 // intersecting query_rect. | |
45 void Query(const Rect& query_rect, std::set<void*>* matches_out) const; | |
piman
2014/04/21 23:59:31
Why returning a set here?
Is seems like returning
luken
2014/04/28 01:33:34
The hashing containers need to be able to hash the
piman
2014/04/28 18:24:28
hash_set gives you O(1) (much better than linear).
luken
2014/04/29 21:01:04
Done.
| |
46 | |
47 // Removes all objects from the tree. | |
48 void Clear(); | |
49 | |
50 // Validates the internal state of the tree and returns false should there be | |
51 // any problem. | |
52 bool Validate() const; | |
53 | |
54 private: | |
55 // Private data structure class for storing internal nodes or leaves with keys | |
56 // of R-Trees. Note that "leaf" nodes can still have children, the children | |
57 // will all be Nodes with non-NULL record pointers. | |
58 class Node : public base::LinkNode<Node> { | |
59 public: | |
60 // Level counts from -1 for "record" Nodes, that is Nodes that wrap key | |
61 // values, to 0 for leaf Nodes, that is Nodes that only have record | |
62 // children, up to the root Node, which has level equal to the height of the | |
63 // tree. | |
64 Node(int level); | |
piman
2014/04/21 23:59:31
nit: explicit
luken
2014/04/28 01:33:34
Done.
| |
65 | |
66 // Builds a new record Node. | |
67 Node(const Rect& rect, void* key); | |
68 | |
69 virtual ~Node(); | |
70 | |
71 // Deletes all children and any attached record. | |
72 void Clear(); | |
73 | |
74 // Recursive call to build a list of rects that intersect the query_rect. | |
75 void Query(const Rect& query_rect, std::set<void*>* matches_out) const; | |
piman
2014/04/21 23:59:31
hash_set?
luken
2014/04/28 01:33:34
Not on void*. See above.
| |
76 | |
77 // Recompute our bounds by taking the union of all children rects. Will then | |
78 // call RecomputeBounds() on our parent for recursive bounds recalculation | |
79 // up to the root. | |
80 void RecomputeBounds(); | |
81 | |
82 // Removes number_to_remove nodes from this Node, and appends them to the | |
83 // supplied list. Does not repair bounds upon completion. | |
84 void RemoveNodesForReinsert(size_t number_to_remove, | |
85 std::list<Node*>* nodes); | |
86 | |
87 // Given a pointer to a child node within this Node, remove it from our | |
88 // list. If that child had any children, append them to the supplied orphan | |
89 // list. Returns the new count of this node after removal. Does not | |
90 // recompute bounds, as this node itself may be removed if it now has too | |
91 // few children. | |
92 size_t RemoveChild(Node* child_node, std::list<Node*>* orphans); | |
93 | |
94 // Does what it says on the tin. Returns NULL if no children. Does not | |
95 // recompute bounds. | |
96 Node* RemoveAndReturnFirstChild(); | |
97 | |
98 // Given a node, returns the best fit node for insertion of that node at | |
99 // the nodes level(). | |
100 Node* ChooseSubtree(Node* node); | |
101 | |
102 // Adds the provided node to this Node. Returns the new count of records | |
103 // stored in the Node. Will recompute the bounds of this node after | |
104 // addition. | |
105 size_t AddNode(Node* node); | |
106 | |
107 // Returns a sibling to this Node with at least min_children and no greater | |
108 // than max_children of this Node's children assigned to it, and having the | |
109 // same parent. Bounds will be valid on both Nodes after this call. | |
110 Node* Split(size_t min_children, size_t max_children); | |
111 | |
112 // For record nodes only, to support re-insert, allows setting the rect. | |
113 void SetRect(const Rect& rect); | |
114 | |
115 // Returns a pointer to the parent of this Node, or NULL if no parent. | |
116 Node* parent() const; | |
piman
2014/04/21 23:59:31
nit: inline
luken
2014/04/28 01:33:34
Done.
| |
117 | |
118 // 0 level() would mean that this Node is a leaf. 1 would mean that this | |
119 // Node has children that are leaves. Calling level() on root_ returns the | |
120 // height of the tree - 1. A level of -1 means that this is a Record node. | |
121 int level() const; | |
piman
2014/04/21 23:59:31
nit: inline
luken
2014/04/28 01:33:34
Done.
| |
122 | |
123 const Rect& rect() const; | |
piman
2014/04/21 23:59:31
nit: inline
luken
2014/04/28 01:33:34
Done.
| |
124 | |
125 size_t count() const; | |
piman
2014/04/21 23:59:31
nit: inline
luken
2014/04/28 01:33:34
Done.
| |
126 | |
127 bool Validate(size_t min_children, size_t max_children) const; | |
128 | |
129 private: | |
130 // Returns all records stored in this node and its children. | |
131 void GetAllValues(std::set<void*>* matches_out) const; | |
piman
2014/04/21 23:59:31
hash_set?
luken
2014/04/28 01:33:34
Not on void*. See above.
| |
132 | |
133 // Returns the increase in area of bounds if bounds were to be expanded to | |
134 // contain rect. | |
135 static int AreaChangeToAdd(const Rect& bounds, const Rect& rect); | |
136 | |
137 // Used for sorting Nodes along vertical and horizontal axes | |
138 static bool CompareVertical(Node* a, Node* b); | |
139 | |
140 static bool CompareHorizontal(Node* a, Node* b); | |
141 | |
142 static bool CompareCenterDistanceFromParent(Node* a, Node* b); | |
143 | |
144 // Used by SplitNode to calculate optimal index of split, after determening | |
145 // along which axis to sort and split the children rectangles. | |
146 size_t CalculateOptimalSplitIndex(size_t min_children, | |
147 size_t max_children, | |
148 const std::vector<Rect>* low_bounds, | |
149 const std::vector<Rect>* high_bounds); | |
150 | |
151 // Bounds recomputation without calling parents to do the same. | |
152 void RecomputeBoundsNoParents(); | |
153 | |
154 // This Node's bounding rectangle. | |
155 Rect rect_; | |
156 | |
157 // The height of the node in the tree, counting from -1 at the record node | |
158 // to 0 at the leaf up to the root node which has level equal to the height | |
159 // of the tree. | |
160 int level_; | |
161 | |
162 // A linked list of all of our children Nodes. | |
163 base::LinkedList<Node> children_; | |
piman
2014/04/21 23:59:31
What's the tradeoff here vs a std::vector? We expe
piman
2014/04/21 23:59:31
Also, it'd be nice to use the scoped types (Scoped
luken
2014/04/28 01:33:34
Done.
luken
2014/04/28 01:33:34
Done.
| |
164 | |
165 // The number of Nodes within children_, as this is not countable in | |
166 // constant time. | |
167 size_t count_; | |
168 | |
169 // A pointer to our parent Node in the RTree. The root node will have a NULL | |
170 // value for parent_. | |
171 Node* parent_; | |
172 | |
173 // If this is a record Node, then key_ will be non-NULL and will contain the | |
174 // key data. Otherwise, NULL. | |
175 void* key_; | |
176 | |
177 DISALLOW_COPY_AND_ASSIGN(Node); | |
178 }; | |
179 | |
180 // Private Insert to support re-insertion of Nodes based on the strategies | |
181 // outlined in Beckmann et al. | |
182 void Insert(Node* node, int* highset_reinsert_level); | |
piman
2014/04/21 23:59:31
nit: no overloading. InsertNode?
luken
2014/04/28 01:33:34
Done.
| |
183 // Private Remove to support removal of nodes for tree without deletion. | |
184 void Remove(Node* node); | |
piman
2014/04/21 23:59:31
nit: no overloading. RemoveNode?
luken
2014/04/28 01:33:34
Done.
| |
185 | |
186 // A pointer to the root node in the RTree, or NULL if the tree is empty. | |
187 Node* root_; | |
188 | |
189 // The parameters used to define the shape of the RTree. | |
190 size_t min_children_; | |
191 size_t max_children_; | |
192 | |
193 // A map of supplied keys to their Node representation within the RTree, for | |
194 // efficient retrieval of keys without requiring a bounding rect. | |
195 std::map<void*, Node*> record_map_; | |
piman
2014/04/21 23:59:31
Why not using a hash_map here? it should be more e
luken
2014/04/28 01:33:34
Because I don't know a good way to hash void*.
| |
196 | |
197 DISALLOW_COPY_AND_ASSIGN(RTree); | |
198 }; | |
199 | |
200 } // namespace gfx | |
201 | |
202 #endif // UI_GFX_GEOMETRY_R_TREE_H_ | |
OLD | NEW |