1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.lang.reflect.Array;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.Iterator;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.coor.QuadTiling;
|
---|
14 |
|
---|
15 | public class QuadBuckets<T extends OsmPrimitive> implements Collection<T>
|
---|
16 | {
|
---|
17 | private static boolean debug = false;
|
---|
18 | private static final boolean consistency_testing = false;
|
---|
19 | /*
|
---|
20 | * Functions prefixed with __ need locking before
|
---|
21 | * being called.
|
---|
22 | */
|
---|
23 | private final Object split_lock = new Object();
|
---|
24 |
|
---|
25 | static void abort(String s)
|
---|
26 | {
|
---|
27 | throw new AssertionError(s);
|
---|
28 | }
|
---|
29 | static void out(String s)
|
---|
30 | {
|
---|
31 | System.out.println(s);
|
---|
32 | }
|
---|
33 | // periodic output
|
---|
34 | long last_out = -1;
|
---|
35 | void pout(String s)
|
---|
36 | {
|
---|
37 | long now = System.currentTimeMillis();
|
---|
38 | if (now - last_out < 300)
|
---|
39 | return;
|
---|
40 | last_out = now;
|
---|
41 | System.out.print(s + "\r");
|
---|
42 | }
|
---|
43 | void pout(String s, int i, int total)
|
---|
44 | {
|
---|
45 | long now = System.currentTimeMillis();
|
---|
46 | if ((now - last_out < 300) &&
|
---|
47 | ((i+1) < total))
|
---|
48 | return;
|
---|
49 | last_out = now;
|
---|
50 | // cast to float to keep the output size down
|
---|
51 | System.out.print(s + " " + (float)((i+1)*100.0/total) + "% done \r");
|
---|
52 | }
|
---|
53 | // 24 levels of buckets gives us bottom-level
|
---|
54 | // buckets that are about 2 meters on a side.
|
---|
55 | // That should do. :)
|
---|
56 | public static int NR_LEVELS = 24;
|
---|
57 | public static double WORLD_PARTS = (1 << NR_LEVELS);
|
---|
58 |
|
---|
59 | public static int MAX_OBJECTS_PER_LEVEL = 16;
|
---|
60 | class QBLevel
|
---|
61 | {
|
---|
62 | int level;
|
---|
63 | long quad;
|
---|
64 | QBLevel parent;
|
---|
65 |
|
---|
66 | public List<T> content;
|
---|
67 | public QBLevel children[];
|
---|
68 | @Override
|
---|
69 | public String toString()
|
---|
70 | {
|
---|
71 | return super.toString()+ "["+level+"]: " + bbox();
|
---|
72 | }
|
---|
73 | public QBLevel(QBLevel parent)
|
---|
74 | {
|
---|
75 | init(parent);
|
---|
76 | }
|
---|
77 | boolean remove_content(T o)
|
---|
78 | {
|
---|
79 | synchronized (split_lock) {
|
---|
80 | // If two threads try to remove item at the same time from different buckets of this QBLevel,
|
---|
81 | // it might happen that one thread removes bucket but don't remove parent because it still sees
|
---|
82 | // another bucket set. Second thread do the same. Due to thread memory caching, it's possible that
|
---|
83 | // changes made by threads will show up in children array too late, leading to QBLevel with all children
|
---|
84 | // set to null
|
---|
85 | boolean ret = this.content.remove(o);
|
---|
86 | if (this.content.size() == 0) {
|
---|
87 | this.content = null;
|
---|
88 | }
|
---|
89 | if (this.canRemove()) {
|
---|
90 | this.remove_from_parent();
|
---|
91 | }
|
---|
92 | return ret;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | QBLevel[] newChildren()
|
---|
96 | {
|
---|
97 | // This is ugly and hackish. But, it seems to work,
|
---|
98 | // and using an ArrayList here seems to cost us
|
---|
99 | // a significant performance penalty -- 50% in my
|
---|
100 | // testing. Child access is one of the single
|
---|
101 | // hottest code paths in this entire class.
|
---|
102 | return (QBLevel[])Array.newInstance(this.getClass(), QuadTiling.TILES_PER_LEVEL);
|
---|
103 | }
|
---|
104 | // Get the correct index for the given primitive
|
---|
105 | // at the given level. If the primitive can not
|
---|
106 | // fit into a single quad at this level, return -1
|
---|
107 | int get_index(T o, int level)
|
---|
108 | {
|
---|
109 | if (debug) {
|
---|
110 | out("getting index for " + o + " at level: " + level);
|
---|
111 | }
|
---|
112 | int index = -1;
|
---|
113 | for (LatLon c : o.getBBox().points()) {
|
---|
114 | if (debug) {
|
---|
115 | out("getting index for point: " + c);
|
---|
116 | }
|
---|
117 | if (index == -1) {
|
---|
118 | index = QuadTiling.index(c, level);
|
---|
119 | if (debug) {
|
---|
120 | out("set initial index to: " + index);
|
---|
121 | }
|
---|
122 | continue;
|
---|
123 | }
|
---|
124 | int another_index = QuadTiling.index(c, level);
|
---|
125 | if (debug) {
|
---|
126 | out("other point index: " + another_index);
|
---|
127 | }
|
---|
128 | if (another_index != index) {
|
---|
129 | // This happens at level 0 sometimes when a way has no
|
---|
130 | // nodes present.
|
---|
131 | if (debug) {
|
---|
132 | out("primitive ("+o.getId()+") would have gone across two quads "
|
---|
133 | + another_index + "/" + index + " at level: " + level + " ");
|
---|
134 | }
|
---|
135 | return -1;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | return index;
|
---|
139 | }
|
---|
140 | /*
|
---|
141 | * There is a race between this and qb.nextContentNode().
|
---|
142 | * If nextContentNode() runs into this bucket, it may
|
---|
143 | * attempt to null out 'children' because it thinks this
|
---|
144 | * is a dead end.
|
---|
145 | */
|
---|
146 | void __split()
|
---|
147 | {
|
---|
148 | if (debug) {
|
---|
149 | out("splitting "+this.bbox()+" level "+level+" with "
|
---|
150 | + content.size() + " entries (my dimensions: "
|
---|
151 | + this.bbox().width()+", "+this.bbox().height()+")");
|
---|
152 | }
|
---|
153 | // deferring allocation of children until use
|
---|
154 | // seems a bit faster
|
---|
155 | //for (int i = 0; i < TILES_PER_LEVEL; i++)
|
---|
156 | // children[i] = new QBLevel(this, i);
|
---|
157 | List<T> tmpcontent = content;
|
---|
158 | content = null;
|
---|
159 | for (T o : tmpcontent) {
|
---|
160 | int new_index = get_index(o, level);
|
---|
161 | if (new_index == -1) {
|
---|
162 | this.__add_content(o);
|
---|
163 | //out("adding content to branch: " + this);
|
---|
164 | continue;
|
---|
165 | }
|
---|
166 | if (children == null) {
|
---|
167 | children = newChildren();
|
---|
168 | }
|
---|
169 | if (children[new_index] == null) {
|
---|
170 | children[new_index] = new QBLevel(this, new_index);
|
---|
171 | }
|
---|
172 | QBLevel child = children[new_index];
|
---|
173 | if (debug) {
|
---|
174 | out("putting "+o+"(q:"+Long.toHexString(QuadTiling.quadTile(o.getBBox().points().get(0)))+") into ["+new_index+"] " + child.bbox());
|
---|
175 | }
|
---|
176 | child.add(o);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | void split() {
|
---|
180 | synchronized(split_lock) {
|
---|
181 | __split();
|
---|
182 | }
|
---|
183 | }
|
---|
184 | boolean __add_content(T o)
|
---|
185 | {
|
---|
186 | boolean ret = false;
|
---|
187 | // The split_lock will keep two concurrent
|
---|
188 | // calls from overwriting content
|
---|
189 | if (content == null) {
|
---|
190 | content = new ArrayList<T>();
|
---|
191 | }
|
---|
192 | ret = content.add(o);
|
---|
193 | if (debug && !this.isLeaf()) {
|
---|
194 | pout("added "+o.getClass().getName()+" to non-leaf with size: " + content.size());
|
---|
195 | }
|
---|
196 | return ret;
|
---|
197 | }
|
---|
198 | void __add_to_leaf(T o)
|
---|
199 | {
|
---|
200 | __add_content(o);
|
---|
201 | if (content.size() > MAX_OBJECTS_PER_LEVEL) {
|
---|
202 | if (debug) {
|
---|
203 | out("["+level+"] deciding to split");
|
---|
204 | }
|
---|
205 | if (level >= NR_LEVELS-1) {
|
---|
206 | if (debug) {
|
---|
207 | out("can not split, but too deep: " + level + " size: " + content.size());
|
---|
208 | }
|
---|
209 | return;
|
---|
210 | }
|
---|
211 | int before_size = -1;
|
---|
212 | if (consistency_testing) {
|
---|
213 | before_size = this.size();
|
---|
214 | }
|
---|
215 | this.split();
|
---|
216 | if (consistency_testing) {
|
---|
217 | int after_size = this.size();
|
---|
218 | if (before_size != after_size) {
|
---|
219 | abort("["+level+"] split done before: " + before_size + " after: " + after_size);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | return;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | boolean matches(T o, BBox search_bbox)
|
---|
227 | {
|
---|
228 | // This can be optimized when o is a node
|
---|
229 | //return search_bbox.bounds(coor));
|
---|
230 | return o.getBBox().intersects(search_bbox);
|
---|
231 | }
|
---|
232 | private List<T> search_contents(BBox search_bbox)
|
---|
233 | {
|
---|
234 | if (debug) {
|
---|
235 | out("searching contents (size: " + content == null?"<null>":content.size() + ") for " + search_bbox);
|
---|
236 | }
|
---|
237 | /*
|
---|
238 | * It is possible that this was created in a split
|
---|
239 | * but never got any content populated.
|
---|
240 | */
|
---|
241 | if (content == null)
|
---|
242 | return null;
|
---|
243 | // We could delay allocating this. But, the way it is currently
|
---|
244 | // used, we almost always have a node in the area that we're
|
---|
245 | // searching since we're searching around other nodes.
|
---|
246 | //
|
---|
247 | // the iterator's calls to ArrayList.size() were showing up in
|
---|
248 | // some profiles, so I'm trying a LinkedList instead
|
---|
249 | List<T> ret = new LinkedList<T>();
|
---|
250 | for (T o : content) {
|
---|
251 | if (matches(o, search_bbox)) {
|
---|
252 | ret.add(o);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | if (debug) {
|
---|
256 | out("done searching quad " + Long.toHexString(this.quad));
|
---|
257 | }
|
---|
258 | return ret;
|
---|
259 | }
|
---|
260 | /*
|
---|
261 | * This is stupid. I tried to have a QBLeaf and QBBranch
|
---|
262 | * class decending from a QBLevel. It's more than twice
|
---|
263 | * as slow. So, this throws OO out the window, but it
|
---|
264 | * is fast. Runtime type determination must be slow.
|
---|
265 | */
|
---|
266 | boolean isLeaf()
|
---|
267 | {
|
---|
268 | if (children == null)
|
---|
269 | return true;
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 | QBLevel next_sibling()
|
---|
273 | {
|
---|
274 | boolean found_me = false;
|
---|
275 | if (parent == null)
|
---|
276 | return null;
|
---|
277 | int __nr = 0;
|
---|
278 | for (QBLevel sibling : parent.children) {
|
---|
279 | __nr++;
|
---|
280 | int nr = __nr-1;
|
---|
281 | if (sibling == null) {
|
---|
282 | if (debug) {
|
---|
283 | out("[" + this.level + "] null child nr: " + nr);
|
---|
284 | }
|
---|
285 | continue;
|
---|
286 | }
|
---|
287 | // We're looking for the *next* child
|
---|
288 | // after us.
|
---|
289 | if (sibling == this) {
|
---|
290 | if (debug) {
|
---|
291 | out("[" + this.level + "] I was child nr: " + nr);
|
---|
292 | }
|
---|
293 | found_me = true;
|
---|
294 | continue;
|
---|
295 | }
|
---|
296 | if (found_me) {
|
---|
297 | if (debug) {
|
---|
298 | out("[" + this.level + "] next sibling was child nr: " + nr);
|
---|
299 | }
|
---|
300 | return sibling;
|
---|
301 | }
|
---|
302 | if (debug) {
|
---|
303 | out("[" + this.level + "] nr: " + nr + " is before me, ignoring...");
|
---|
304 | }
|
---|
305 | }
|
---|
306 | return null;
|
---|
307 | }
|
---|
308 | boolean hasContent()
|
---|
309 | {
|
---|
310 | return content != null;
|
---|
311 | }
|
---|
312 | QBLevel nextSibling()
|
---|
313 | {
|
---|
314 | QBLevel next = this;
|
---|
315 | QBLevel sibling = next.next_sibling();
|
---|
316 | // Walk back up the tree to find the
|
---|
317 | // next sibling node. It may be either
|
---|
318 | // a leaf or branch.
|
---|
319 | while (sibling == null) {
|
---|
320 | if (debug) {
|
---|
321 | out("no siblings at level["+next.level+"], moving to parent");
|
---|
322 | }
|
---|
323 | next = next.parent;
|
---|
324 | if (next == null) {
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | sibling = next.next_sibling();
|
---|
328 | }
|
---|
329 | next = sibling;
|
---|
330 | return next;
|
---|
331 | }
|
---|
332 | QBLevel firstChild()
|
---|
333 | {
|
---|
334 | QBLevel ret = null;
|
---|
335 | for (QBLevel child : this.children) {
|
---|
336 | if (child == null) {
|
---|
337 | continue;
|
---|
338 | }
|
---|
339 | ret = child;
|
---|
340 | break;
|
---|
341 | }
|
---|
342 | return ret;
|
---|
343 | }
|
---|
344 | QBLevel nextNode()
|
---|
345 | {
|
---|
346 | if (this.isLeaf())
|
---|
347 | return this.nextSibling();
|
---|
348 | return this.firstChild();
|
---|
349 | }
|
---|
350 | QBLevel nextContentNode()
|
---|
351 | {
|
---|
352 | QBLevel next = this.nextNode();
|
---|
353 | if (next == null)
|
---|
354 | return next;
|
---|
355 | if (next.hasContent())
|
---|
356 | return next;
|
---|
357 | return next.nextContentNode();
|
---|
358 | }
|
---|
359 | int size()
|
---|
360 | {
|
---|
361 | if (isLeaf())
|
---|
362 | return size_leaf();
|
---|
363 | return size_branch();
|
---|
364 | }
|
---|
365 | private int size_leaf()
|
---|
366 | {
|
---|
367 | if (content == null) {
|
---|
368 | if (debug) {
|
---|
369 | out("["+level+"] leaf size: null content, children? " + Arrays.toString(children));
|
---|
370 | }
|
---|
371 | return 0;
|
---|
372 | }
|
---|
373 | if (debug) {
|
---|
374 | out("["+level+"] leaf size: " + content.size());
|
---|
375 | }
|
---|
376 | return content.size();
|
---|
377 | }
|
---|
378 | private int size_branch()
|
---|
379 | {
|
---|
380 | int ret = 0;
|
---|
381 | for (QBLevel l : children) {
|
---|
382 | if (l == null) {
|
---|
383 | continue;
|
---|
384 | }
|
---|
385 | ret += l.size();
|
---|
386 | }
|
---|
387 | if (content != null) {
|
---|
388 | ret += content.size();
|
---|
389 | }
|
---|
390 | if (debug) {
|
---|
391 | out("["+level+"] branch size: " + ret);
|
---|
392 | }
|
---|
393 | return ret;
|
---|
394 | }
|
---|
395 | boolean contains(T n)
|
---|
396 | {
|
---|
397 | QBLevel res = find_exact(n);
|
---|
398 | if (res == null)
|
---|
399 | return false;
|
---|
400 | return true;
|
---|
401 | }
|
---|
402 | QBLevel find_exact(T n)
|
---|
403 | {
|
---|
404 | if (content != null && content.contains(n))
|
---|
405 | return this;
|
---|
406 | return find_exact_child(n);
|
---|
407 | }
|
---|
408 | private QBLevel find_exact_child(T n)
|
---|
409 | {
|
---|
410 | QBLevel ret = null;
|
---|
411 | if (children == null)
|
---|
412 | return ret;
|
---|
413 | for (QBLevel l : children) {
|
---|
414 | if (l == null) {
|
---|
415 | continue;
|
---|
416 | }
|
---|
417 | ret = l.find_exact(n);
|
---|
418 | if (ret != null) {
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | }
|
---|
422 | return ret;
|
---|
423 | }
|
---|
424 | boolean add(T n)
|
---|
425 | {
|
---|
426 | if (consistency_testing) {
|
---|
427 | if (!matches(n, this.bbox())) {
|
---|
428 | out("-----------------------------");
|
---|
429 | debug = true;
|
---|
430 | get_index(n, level);
|
---|
431 | get_index(n, level-1);
|
---|
432 | int nr = 0;
|
---|
433 | for (QBLevel sibling : parent.children) {
|
---|
434 | out("sibling["+ (nr++) +"]: " + sibling.bbox() + " this: " + (this==sibling));
|
---|
435 | }
|
---|
436 | abort("\nobject " + n + " does not belong in node at level: " + level + " bbox: " + this.bbox());
|
---|
437 | }
|
---|
438 | }
|
---|
439 | synchronized (split_lock) {
|
---|
440 | if (isLeaf()) {
|
---|
441 | __add_to_leaf(n);
|
---|
442 | } else {
|
---|
443 | __add_to_branch(n);
|
---|
444 | }
|
---|
445 | }
|
---|
446 | return true;
|
---|
447 | }
|
---|
448 | QBLevel __add_to_branch(T n)
|
---|
449 | {
|
---|
450 | int index = get_index(n, level);
|
---|
451 | if (index == -1) {
|
---|
452 | if (debug) {
|
---|
453 | out("unable to get index for " + n + "at level: " + level + ", adding content to branch: " + this);
|
---|
454 | }
|
---|
455 | this.__add_content(n);
|
---|
456 | return this;
|
---|
457 | }
|
---|
458 | if (debug) {
|
---|
459 | out("[" + level + "]: " + n +
|
---|
460 | " index " + index + " levelquad:" + this.quads() + " level bbox:" + this.bbox());
|
---|
461 | out(" put in child["+index+"]");
|
---|
462 | }
|
---|
463 | if (children[index] == null) {
|
---|
464 | children[index] = new QBLevel(this, index);
|
---|
465 | }
|
---|
466 | children[index].add(n);
|
---|
467 | return this;
|
---|
468 | }
|
---|
469 | private List<T> search(BBox search_bbox)
|
---|
470 | {
|
---|
471 | List<T> ret = null;
|
---|
472 | if (debug) {
|
---|
473 | System.out.print("[" + level + "] qb bbox: " + this.bbox() + " ");
|
---|
474 | }
|
---|
475 | if (!this.bbox().intersects(search_bbox)) {
|
---|
476 | if (debug) {
|
---|
477 | out("miss " + Long.toHexString(this.quad));
|
---|
478 | //QuadTiling.tile2xy(this.quad);
|
---|
479 | }
|
---|
480 | return ret;
|
---|
481 | }
|
---|
482 | if (this.hasContent()) {
|
---|
483 | ret = this.search_contents(search_bbox);
|
---|
484 | }
|
---|
485 | if (this.isLeaf())
|
---|
486 | return ret;
|
---|
487 | //if (this.hasContent())
|
---|
488 | // abort("branch had stuff");
|
---|
489 | if (debug) {
|
---|
490 | out("hit " + this.quads());
|
---|
491 | }
|
---|
492 |
|
---|
493 | if (debug) {
|
---|
494 | out("[" + level + "] not leaf, moving down");
|
---|
495 | }
|
---|
496 | for (int i = 0; i < children.length; i++) {
|
---|
497 | QBLevel q = children[i];
|
---|
498 | if (debug) {
|
---|
499 | out("child["+i+"]: " + q);
|
---|
500 | }
|
---|
501 | if (q == null) {
|
---|
502 | continue;
|
---|
503 | }
|
---|
504 | if (debug) {
|
---|
505 | System.out.print(i+": ");
|
---|
506 | }
|
---|
507 | List<T> coors = q.search(search_bbox);
|
---|
508 | if (coors == null) {
|
---|
509 | continue;
|
---|
510 | }
|
---|
511 | if (ret == null) {
|
---|
512 | ret = coors;
|
---|
513 | } else {
|
---|
514 | ret.addAll(coors);
|
---|
515 | }
|
---|
516 | if (q.bbox().bounds(search_bbox)) {
|
---|
517 | search_cache = q;
|
---|
518 | // optimization: do not continue searching
|
---|
519 | // other tiles if this one wholly contained
|
---|
520 | // what we were looking for.
|
---|
521 | if (coors.size() > 0 ) {
|
---|
522 | if (debug) {
|
---|
523 | out("break early");
|
---|
524 | }
|
---|
525 | break;
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 | return ret;
|
---|
530 | }
|
---|
531 | public String quads()
|
---|
532 | {
|
---|
533 | return Long.toHexString(quad);
|
---|
534 | }
|
---|
535 | public void init(QBLevel parent)
|
---|
536 | {
|
---|
537 | this.parent = parent;
|
---|
538 | if (parent == null) {
|
---|
539 | this.level = 0;
|
---|
540 | } else {
|
---|
541 | this.level = parent.level + 1;
|
---|
542 | }
|
---|
543 | this.quad = 0;
|
---|
544 | }
|
---|
545 | int index_of(QBLevel find_this)
|
---|
546 | {
|
---|
547 | if (this.isLeaf())
|
---|
548 | return -1;
|
---|
549 |
|
---|
550 | for (int i = 0; i < QuadTiling.TILES_PER_LEVEL; i++) {
|
---|
551 | if (children[i] == find_this)
|
---|
552 | return i;
|
---|
553 | }
|
---|
554 | return -1;
|
---|
555 | }
|
---|
556 | public QBLevel(QBLevel parent, int parent_index)
|
---|
557 | {
|
---|
558 | this.init(parent);
|
---|
559 | int shift = (QuadBuckets.NR_LEVELS - level) * 2;
|
---|
560 | long mult = 1;
|
---|
561 | // Java blows the big one. It seems to wrap when
|
---|
562 | // you shift by > 31
|
---|
563 | if (shift >= 30) {
|
---|
564 | shift -= 30;
|
---|
565 | mult = 1<<30;
|
---|
566 | }
|
---|
567 | long this_quadpart = mult * (parent_index << shift);
|
---|
568 | this.quad = parent.quad | this_quadpart;
|
---|
569 | if (debug) {
|
---|
570 | out("new level["+this.level+"] bbox["+parent_index+"]: " + this.bbox()
|
---|
571 | + " coor: " + this.coor()
|
---|
572 | + " quadpart: " + Long.toHexString(this_quadpart)
|
---|
573 | + " quad: " + Long.toHexString(this.quad));
|
---|
574 | }
|
---|
575 | }
|
---|
576 | /*
|
---|
577 | * Surely we can calculate these efficiently instead of storing
|
---|
578 | */
|
---|
579 | double width = Double.NEGATIVE_INFINITY;
|
---|
580 | double width()
|
---|
581 | {
|
---|
582 | if (width != Double.NEGATIVE_INFINITY)
|
---|
583 | return this.width;
|
---|
584 | if (level == 0) {
|
---|
585 | width = this.bbox().width();
|
---|
586 | } else {
|
---|
587 | width = parent.width()/2;
|
---|
588 | }
|
---|
589 | return width;
|
---|
590 | }
|
---|
591 | double height()
|
---|
592 | {
|
---|
593 | return width()/2;
|
---|
594 | }
|
---|
595 | private BBox bbox = null;
|
---|
596 | public BBox bbox()
|
---|
597 | {
|
---|
598 | if (bbox != null)
|
---|
599 | return bbox;
|
---|
600 | if (level == 0) {
|
---|
601 | bbox = new BBox(-180, 90, 180, -90);
|
---|
602 | } else {
|
---|
603 | LatLon bottom_left = this.coor();
|
---|
604 | double lat = bottom_left.lat() + this.height();
|
---|
605 | double lon = bottom_left.lon() + this.width();
|
---|
606 | LatLon top_right = new LatLon(lat, lon);
|
---|
607 | bbox = new BBox(bottom_left, top_right);
|
---|
608 | }
|
---|
609 | return bbox;
|
---|
610 | }
|
---|
611 | /*
|
---|
612 | * This gives the coordinate of the bottom-left
|
---|
613 | * corner of the box
|
---|
614 | */
|
---|
615 | LatLon coor()
|
---|
616 | {
|
---|
617 | return QuadTiling.tile2LatLon(this.quad);
|
---|
618 | }
|
---|
619 | boolean hasChildren()
|
---|
620 | {
|
---|
621 | if (children == null)
|
---|
622 | return false;
|
---|
623 |
|
---|
624 | for (QBLevel child : children) {
|
---|
625 | if (child != null)
|
---|
626 | return true;
|
---|
627 | }
|
---|
628 | return false;
|
---|
629 | }
|
---|
630 | void remove_from_parent()
|
---|
631 | {
|
---|
632 | if (parent == null)
|
---|
633 | return;
|
---|
634 |
|
---|
635 | int nr_siblings = 0;
|
---|
636 | for (int i = 0; i < parent.children.length; i++) {
|
---|
637 | QBLevel sibling = parent.children[i];
|
---|
638 | if (sibling != null) {
|
---|
639 | nr_siblings++;
|
---|
640 | }
|
---|
641 | if (sibling != this) {
|
---|
642 | continue;
|
---|
643 | }
|
---|
644 | if (!canRemove()) {
|
---|
645 | abort("attempt to remove non-empty child: " + this.content + " " + Arrays.toString(this.children));
|
---|
646 | }
|
---|
647 | parent.children[i] = null;
|
---|
648 | nr_siblings--;
|
---|
649 | }
|
---|
650 | if (nr_siblings == 0) {
|
---|
651 | parent.children = null;
|
---|
652 | }
|
---|
653 | if (parent.canRemove()) {
|
---|
654 | parent.remove_from_parent();
|
---|
655 | }
|
---|
656 | }
|
---|
657 | boolean canRemove()
|
---|
658 | {
|
---|
659 | if (content != null && content.size() > 0)
|
---|
660 | return false;
|
---|
661 | if (this.hasChildren())
|
---|
662 | return false;
|
---|
663 | return true;
|
---|
664 | }
|
---|
665 | }
|
---|
666 |
|
---|
667 | private QBLevel root;
|
---|
668 | private QBLevel search_cache;
|
---|
669 | public QuadBuckets()
|
---|
670 | {
|
---|
671 | clear();
|
---|
672 | }
|
---|
673 | public void clear()
|
---|
674 | {
|
---|
675 | root = new QBLevel(null);
|
---|
676 | search_cache = null;
|
---|
677 | if (debug) {
|
---|
678 | out("QuadBuckets() cleared: " + this);
|
---|
679 | out("root: " + root + " level: " + root.level + " bbox: " + root.bbox());
|
---|
680 | }
|
---|
681 | }
|
---|
682 | public boolean add(T n)
|
---|
683 | {
|
---|
684 | if (debug) {
|
---|
685 | out("QuadBuckets() add: " + n + " size now: " + this.size());
|
---|
686 | }
|
---|
687 | int before_size = -1;
|
---|
688 | if (consistency_testing) {
|
---|
689 | before_size = root.size();
|
---|
690 | }
|
---|
691 | boolean ret;
|
---|
692 | // A way with no nodes will have an infinitely large
|
---|
693 | // bounding box. Just stash it in the root node.
|
---|
694 | if (!n.isUsable()) {
|
---|
695 | synchronized (split_lock) {
|
---|
696 | ret = root.__add_content(n);
|
---|
697 | }
|
---|
698 | } else {
|
---|
699 | ret = root.add(n);
|
---|
700 | }
|
---|
701 | if (consistency_testing) {
|
---|
702 | int end_size = root.size();
|
---|
703 | if (ret) {
|
---|
704 | end_size--;
|
---|
705 | }
|
---|
706 | if (before_size != end_size) {
|
---|
707 | abort("size inconsistency before add: " + before_size + " after: " + end_size);
|
---|
708 | }
|
---|
709 | }
|
---|
710 | if (debug) {
|
---|
711 | out("QuadBuckets() add: " + n + " size after: " + this.size());
|
---|
712 | }
|
---|
713 | return ret;
|
---|
714 | }
|
---|
715 | public void reindex()
|
---|
716 | {
|
---|
717 | QBLevel newroot = new QBLevel(null);
|
---|
718 | Iterator<T> i = this.iterator();
|
---|
719 | while (i.hasNext()) {
|
---|
720 | T o = i.next();
|
---|
721 | newroot.add(o);
|
---|
722 | }
|
---|
723 | root = newroot;
|
---|
724 | }
|
---|
725 | public void unsupported()
|
---|
726 | {
|
---|
727 | out("unsupported operation");
|
---|
728 | throw new UnsupportedOperationException();
|
---|
729 | }
|
---|
730 | public boolean retainAll(Collection<?> objects)
|
---|
731 | {
|
---|
732 | for (T o : this) {
|
---|
733 | if (objects.contains(o)) {
|
---|
734 | continue;
|
---|
735 | }
|
---|
736 | if (!this.remove(o))
|
---|
737 | return false;
|
---|
738 | }
|
---|
739 | return true;
|
---|
740 | }
|
---|
741 | public boolean removeAll(Collection<?> objects)
|
---|
742 | {
|
---|
743 | boolean changed = false;
|
---|
744 | for (Object o : objects) {
|
---|
745 | changed = changed | remove(o);
|
---|
746 | }
|
---|
747 | return changed;
|
---|
748 | }
|
---|
749 | public boolean addAll(Collection<? extends T> objects)
|
---|
750 | {
|
---|
751 | boolean changed = false;
|
---|
752 | for (Object o : objects) {
|
---|
753 | changed = changed | this.add(convert(o));
|
---|
754 | }
|
---|
755 | return changed;
|
---|
756 | }
|
---|
757 | public boolean containsAll(Collection<?> objects)
|
---|
758 | {
|
---|
759 | for (Object o : objects) {
|
---|
760 | if (!this.contains(o))
|
---|
761 | return false;
|
---|
762 | }
|
---|
763 | return true;
|
---|
764 | }
|
---|
765 | // If anyone has suggestions for how to fix
|
---|
766 | // this properly, I'm listening :)
|
---|
767 | @SuppressWarnings("unchecked")
|
---|
768 | private T convert(Object raw)
|
---|
769 | {
|
---|
770 | return (T)raw;
|
---|
771 | }
|
---|
772 | public boolean remove(Object o)
|
---|
773 | {
|
---|
774 | return this.remove(convert(o));
|
---|
775 | }
|
---|
776 | private boolean remove_slow(T removeme)
|
---|
777 | {
|
---|
778 | boolean ret = false;
|
---|
779 | Iterator<T> i = this.iterator();
|
---|
780 | while (i.hasNext()) {
|
---|
781 | T o = i.next();
|
---|
782 | if (o != removeme) {
|
---|
783 | continue;
|
---|
784 | }
|
---|
785 | i.remove();
|
---|
786 | ret = true;
|
---|
787 | break;
|
---|
788 | }
|
---|
789 | if (debug) {
|
---|
790 | out("qb slow remove result: " + ret);
|
---|
791 | }
|
---|
792 | return ret;
|
---|
793 | }
|
---|
794 | public boolean remove(T o)
|
---|
795 | {
|
---|
796 | /*
|
---|
797 | * We first try a locational search
|
---|
798 | */
|
---|
799 | QBLevel bucket = root.find_exact(o);
|
---|
800 | /*
|
---|
801 | * That may fail because the object was
|
---|
802 | * moved or changed in some way, so we
|
---|
803 | * resort to an iterative search:
|
---|
804 | */
|
---|
805 | if (bucket == null)
|
---|
806 | return remove_slow(o);
|
---|
807 | boolean ret = bucket.remove_content(o);
|
---|
808 | if (debug) {
|
---|
809 | out("qb remove result: " + ret);
|
---|
810 | }
|
---|
811 | search_cache = null; // Search cache might point to one of removed buckets
|
---|
812 | return ret;
|
---|
813 | }
|
---|
814 | public boolean contains(Object o)
|
---|
815 | {
|
---|
816 | QBLevel bucket = root.find_exact(convert(o));
|
---|
817 | if (bucket == null)
|
---|
818 | return false;
|
---|
819 | return true;
|
---|
820 | }
|
---|
821 | public ArrayList<T> toArrayList()
|
---|
822 | {
|
---|
823 | ArrayList<T> a = new ArrayList<T>();
|
---|
824 | for (T n : this) {
|
---|
825 | a.add(n);
|
---|
826 | }
|
---|
827 | if (debug) {
|
---|
828 | out("returning array list with size: " + a.size());
|
---|
829 | }
|
---|
830 | return a;
|
---|
831 | }
|
---|
832 | public Object[] toArray()
|
---|
833 | {
|
---|
834 | return this.toArrayList().toArray();
|
---|
835 | }
|
---|
836 | public <A> A[] toArray(A[] template)
|
---|
837 | {
|
---|
838 | return this.toArrayList().toArray(template);
|
---|
839 | }
|
---|
840 | class QuadBucketIterator implements Iterator<T>
|
---|
841 | {
|
---|
842 | QBLevel current_node;
|
---|
843 | int content_index;
|
---|
844 | int iterated_over;
|
---|
845 | QBLevel next_content_node(QBLevel q)
|
---|
846 | {
|
---|
847 | if (q == null)
|
---|
848 | return null;
|
---|
849 | QBLevel orig = q;
|
---|
850 | QBLevel next;
|
---|
851 | synchronized (split_lock) {
|
---|
852 | next = q.nextContentNode();
|
---|
853 | }
|
---|
854 | //if (consistency_testing && (orig == next))
|
---|
855 | if (orig == next) {
|
---|
856 | abort("got same leaf back leaf: " + q.isLeaf());
|
---|
857 | }
|
---|
858 | return next;
|
---|
859 | }
|
---|
860 | public QuadBucketIterator(QuadBuckets<T> qb)
|
---|
861 | {
|
---|
862 | if (debug) {
|
---|
863 | out(this + " is a new iterator qb: " + qb + " size: " + qb.size());
|
---|
864 | }
|
---|
865 | if (qb.root.isLeaf() || qb.root.hasContent()) {
|
---|
866 | current_node = qb.root;
|
---|
867 | } else {
|
---|
868 | current_node = next_content_node(qb.root);
|
---|
869 | }
|
---|
870 | if (debug) {
|
---|
871 | out("\titerator first leaf: " + current_node);
|
---|
872 | }
|
---|
873 | iterated_over = 0;
|
---|
874 | }
|
---|
875 | public boolean hasNext()
|
---|
876 | {
|
---|
877 | if (this.peek() == null) {
|
---|
878 | if (debug) {
|
---|
879 | out(this + " no hasNext(), but iterated over so far: " + iterated_over);
|
---|
880 | }
|
---|
881 | return false;
|
---|
882 | }
|
---|
883 | return true;
|
---|
884 | }
|
---|
885 | T peek()
|
---|
886 | {
|
---|
887 | if (current_node == null) {
|
---|
888 | if (debug) {
|
---|
889 | out("null current leaf, nowhere to go");
|
---|
890 | }
|
---|
891 | return null;
|
---|
892 | }
|
---|
893 | while((current_node.content == null) ||
|
---|
894 | (content_index >= current_node.content.size())) {
|
---|
895 | if (debug) {
|
---|
896 | out("moving to next leaf");
|
---|
897 | }
|
---|
898 | content_index = 0;
|
---|
899 | current_node = next_content_node(current_node);
|
---|
900 | if (current_node == null) {
|
---|
901 | break;
|
---|
902 | }
|
---|
903 | }
|
---|
904 | if (current_node == null || current_node.content == null) {
|
---|
905 | if (debug) {
|
---|
906 | out("late nowhere to go " + current_node);
|
---|
907 | }
|
---|
908 | return null;
|
---|
909 | }
|
---|
910 | return current_node.content.get(content_index);
|
---|
911 | }
|
---|
912 | public T next()
|
---|
913 | {
|
---|
914 | T ret = peek();
|
---|
915 | content_index++;
|
---|
916 | if (debug) {
|
---|
917 | out("iteration["+iterated_over+"] " + content_index + " leaf: " + current_node);
|
---|
918 | }
|
---|
919 | iterated_over++;
|
---|
920 | if (ret == null) {
|
---|
921 | if (debug) {
|
---|
922 | out(this + " no next node, but iterated over so far: " + iterated_over);
|
---|
923 | }
|
---|
924 | }
|
---|
925 | return ret;
|
---|
926 | }
|
---|
927 | public void remove()
|
---|
928 | {
|
---|
929 | // two uses
|
---|
930 | // 1. Back up to the thing we just returned
|
---|
931 | // 2. move the index back since we removed
|
---|
932 | // an element
|
---|
933 | content_index--;
|
---|
934 | T object = peek(); //TODO Is the call to peek() necessary?
|
---|
935 | current_node.remove_content(object);
|
---|
936 | }
|
---|
937 | }
|
---|
938 | public Iterator<T> iterator()
|
---|
939 | {
|
---|
940 | return new QuadBucketIterator(this);
|
---|
941 | }
|
---|
942 | public int size()
|
---|
943 | {
|
---|
944 | // This can certainly by optimized
|
---|
945 | int ret = root.size();
|
---|
946 | if (debug) {
|
---|
947 | out(this + " QuadBuckets size: " + ret);
|
---|
948 | }
|
---|
949 | return ret;
|
---|
950 | }
|
---|
951 | public int size_iter()
|
---|
952 | {
|
---|
953 | int count = 0;
|
---|
954 | Iterator<T> i = this.iterator();
|
---|
955 | while (i.hasNext()) {
|
---|
956 | i.next();
|
---|
957 | count++;
|
---|
958 | }
|
---|
959 | return count;
|
---|
960 | }
|
---|
961 | public boolean isEmpty()
|
---|
962 | {
|
---|
963 | if (this.size() == 0)
|
---|
964 | return true;
|
---|
965 | return false;
|
---|
966 | }
|
---|
967 | public static BBox search_to_bbox(LatLon point, double radius)
|
---|
968 | {
|
---|
969 | BBox bbox = new BBox(point.lon() - radius, point.lat() - radius,
|
---|
970 | point.lon() + radius, point.lat() + radius);
|
---|
971 | if (debug) {
|
---|
972 | out("search bbox before sanity: " + bbox);
|
---|
973 | }
|
---|
974 | if (debug) {
|
---|
975 | out("search bbox after sanity: " + bbox);
|
---|
976 | }
|
---|
977 | return bbox;
|
---|
978 | }
|
---|
979 | List<T> search(Way w)
|
---|
980 | {
|
---|
981 | BBox way_bbox = new BBox(w);
|
---|
982 | return this.search(way_bbox);
|
---|
983 | }
|
---|
984 | public List<T> search(Node n, double radius)
|
---|
985 | {
|
---|
986 | return this.search(n.getCoor(), radius);
|
---|
987 | }
|
---|
988 | public List<T> search(LatLon point, double radius)
|
---|
989 | {
|
---|
990 | if (point == null)
|
---|
991 | return Collections.emptyList();
|
---|
992 | return this.search(search_to_bbox(point, radius));
|
---|
993 | }
|
---|
994 | public List<T> search(LatLon b1, LatLon b2)
|
---|
995 | {
|
---|
996 | BBox bbox = new BBox(b1.lon(), b1.lat(), b2.lon(), b2.lat());
|
---|
997 | return this.search(bbox);
|
---|
998 | }
|
---|
999 | List<T> search(BBox search_bbox)
|
---|
1000 | {
|
---|
1001 | if (debug) {
|
---|
1002 | out("qb root search at " + search_bbox);
|
---|
1003 | out("root bbox: " + root.bbox());
|
---|
1004 | }
|
---|
1005 | List<T> ret;
|
---|
1006 | // Doing this cuts down search cost on a real-life data
|
---|
1007 | // set by about 25%
|
---|
1008 | boolean cache_searches = true;
|
---|
1009 | if (cache_searches) {
|
---|
1010 | if (search_cache == null) {
|
---|
1011 | search_cache = root;
|
---|
1012 | }
|
---|
1013 | // Walk back up the tree when the last
|
---|
1014 | // search spot can not cover the current
|
---|
1015 | // search
|
---|
1016 | while (!search_cache.bbox().bounds(search_bbox)) {
|
---|
1017 | if (debug) {
|
---|
1018 | out("bbox: " + search_bbox);
|
---|
1019 | }
|
---|
1020 | if (debug) {
|
---|
1021 | out("search_cache: " + search_cache + " level: " + search_cache.level);
|
---|
1022 | out("search_cache.bbox(): " + search_cache.bbox());
|
---|
1023 | }
|
---|
1024 | search_cache = search_cache.parent;
|
---|
1025 | if (debug) {
|
---|
1026 | out("new search_cache: " + search_cache);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | } else {
|
---|
1030 | search_cache = root;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | // Save parent because search_cache might change during search call
|
---|
1034 | QBLevel tmp = search_cache.parent;
|
---|
1035 |
|
---|
1036 | ret = search_cache.search(search_bbox);
|
---|
1037 | if (ret == null) {
|
---|
1038 | ret = new ArrayList<T>();
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | // A way that spans this bucket may be stored in one
|
---|
1042 | // of the nodes which is a parent of the search cache
|
---|
1043 | while (tmp != null) {
|
---|
1044 | List<T> content_result = tmp.search_contents(search_bbox);
|
---|
1045 | if (content_result != null) {
|
---|
1046 | ret.addAll(content_result);
|
---|
1047 | }
|
---|
1048 | tmp = tmp.parent;
|
---|
1049 | }
|
---|
1050 | if (debug) {
|
---|
1051 | out("search of QuadBuckets for " + search_bbox + " ret len: " + ret.size());
|
---|
1052 | }
|
---|
1053 | return ret;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | public void printTree() {
|
---|
1057 | printTreeRecursive(root, 0);
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | private void printTreeRecursive(QBLevel level, int indent) {
|
---|
1061 | if (level == null) {
|
---|
1062 | printIndented(indent, "<empty child>");
|
---|
1063 | return;
|
---|
1064 | }
|
---|
1065 | printIndented(indent, level);
|
---|
1066 | if (level.hasContent()) {
|
---|
1067 | for (T o:level.content) {
|
---|
1068 | printIndented(indent, o);
|
---|
1069 | }
|
---|
1070 | }
|
---|
1071 | if (level.children != null) {
|
---|
1072 | for (QBLevel child:level.children) {
|
---|
1073 | printTreeRecursive(child, indent + 2);
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | private void printIndented(int indent, Object msg) {
|
---|
1079 | for (int i=0; i<indent; i++) {
|
---|
1080 | System.out.print(' ');
|
---|
1081 | }
|
---|
1082 | System.out.println(msg);
|
---|
1083 | }
|
---|
1084 | }
|
---|