[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2165] | 2 | package org.openstreetmap.josm.data.osm;
|
---|
[2427] | 3 |
|
---|
[2165] | 4 | import java.util.ArrayList;
|
---|
[2420] | 5 | import java.util.Arrays;
|
---|
[2165] | 6 | import java.util.Collection;
|
---|
[2388] | 7 | import java.util.Iterator;
|
---|
| 8 | import java.util.List;
|
---|
[2165] | 9 |
|
---|
[6171] | 10 | import org.openstreetmap.josm.Main;
|
---|
[2388] | 11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[2165] | 12 | import org.openstreetmap.josm.data.coor.QuadTiling;
|
---|
| 13 |
|
---|
[3154] | 14 | /**
|
---|
| 15 | * Note: bbox of primitives added to QuadBuckets has to stay the same. In case of coordinate change, primitive must
|
---|
| 16 | * be removed and readded.
|
---|
| 17 | *
|
---|
[3584] | 18 | * This class is (no longer) thread safe.
|
---|
| 19 | *
|
---|
[3154] | 20 | */
|
---|
[6178] | 21 | public class QuadBuckets<T extends OsmPrimitive> implements Collection<T> {
|
---|
[2896] | 22 | private static final boolean consistency_testing = false;
|
---|
[3476] | 23 | private static final int NW_INDEX = 1;
|
---|
| 24 | private static final int NE_INDEX = 3;
|
---|
| 25 | private static final int SE_INDEX = 2;
|
---|
| 26 | private static final int SW_INDEX = 0;
|
---|
[2165] | 27 |
|
---|
[6171] | 28 | static void abort(String s) {
|
---|
[2420] | 29 | throw new AssertionError(s);
|
---|
[2165] | 30 | }
|
---|
| 31 |
|
---|
[4869] | 32 | public static final int MAX_OBJECTS_PER_LEVEL = 16;
|
---|
[6178] | 33 |
|
---|
| 34 | static class QBLevel<T extends OsmPrimitive> {
|
---|
| 35 | private final int level;
|
---|
| 36 | private final int index;
|
---|
[3145] | 37 | private final BBox bbox;
|
---|
[6178] | 38 | private final long quad;
|
---|
| 39 | private final QBLevel<T> parent;
|
---|
[3476] | 40 | private boolean isLeaf = true;
|
---|
[2165] | 41 |
|
---|
[6178] | 42 | private List<T> content;
|
---|
| 43 | // child order by index is sw, nw, se, ne
|
---|
| 44 | private QBLevel<T> nw, ne, sw, se;
|
---|
[3476] | 45 |
|
---|
[6178] | 46 | private final QuadBuckets<T> buckets;
|
---|
| 47 |
|
---|
| 48 | private QBLevel<T> getChild(int index) {
|
---|
[3476] | 49 | switch (index) {
|
---|
| 50 | case NE_INDEX:
|
---|
| 51 | if (ne == null) {
|
---|
[7005] | 52 | ne = new QBLevel<>(this, index, buckets);
|
---|
[3476] | 53 | }
|
---|
| 54 | return ne;
|
---|
| 55 | case NW_INDEX:
|
---|
| 56 | if (nw == null) {
|
---|
[7005] | 57 | nw = new QBLevel<>(this, index, buckets);
|
---|
[3476] | 58 | }
|
---|
| 59 | return nw;
|
---|
| 60 | case SE_INDEX:
|
---|
| 61 | if (se == null) {
|
---|
[7005] | 62 | se = new QBLevel<>(this, index, buckets);
|
---|
[3476] | 63 | }
|
---|
| 64 | return se;
|
---|
| 65 | case SW_INDEX:
|
---|
| 66 | if (sw == null) {
|
---|
[7005] | 67 | sw = new QBLevel<>(this, index, buckets);
|
---|
[3476] | 68 | }
|
---|
| 69 | return sw;
|
---|
| 70 | default:
|
---|
| 71 | return null;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[6178] | 75 | @SuppressWarnings("unchecked")
|
---|
| 76 | private QBLevel<T>[] getChildren() {
|
---|
| 77 | return new QBLevel[] {sw, nw, se, ne};
|
---|
[3476] | 78 | }
|
---|
| 79 |
|
---|
[2388] | 80 | @Override
|
---|
[6178] | 81 | public String toString() {
|
---|
| 82 | return super.toString() + "[" + level + "]: " + bbox();
|
---|
[2263] | 83 | }
|
---|
[6178] | 84 |
|
---|
[3145] | 85 | /**
|
---|
| 86 | * Constructor for root node
|
---|
| 87 | */
|
---|
[6178] | 88 | public QBLevel(final QuadBuckets<T> buckets) {
|
---|
[3145] | 89 | level = 0;
|
---|
[6178] | 90 | index = 0;
|
---|
[3145] | 91 | quad = 0;
|
---|
| 92 | parent = null;
|
---|
| 93 | bbox = new BBox(-180, 90, 180, -90);
|
---|
[6178] | 94 | this.buckets = buckets;
|
---|
[2165] | 95 | }
|
---|
[3145] | 96 |
|
---|
[6178] | 97 | public QBLevel(QBLevel<T> parent, int parent_index, final QuadBuckets<T> buckets) {
|
---|
[3145] | 98 | this.parent = parent;
|
---|
| 99 | this.level = parent.level + 1;
|
---|
[6178] | 100 | this.index = parent_index;
|
---|
| 101 | this.buckets = buckets;
|
---|
| 102 |
|
---|
[3150] | 103 | int shift = (QuadTiling.NR_LEVELS - level) * 2;
|
---|
[3145] | 104 | long mult = 1;
|
---|
[6171] | 105 | // Java blows the big one. It seems to wrap when you shift by > 31
|
---|
[3145] | 106 | if (shift >= 30) {
|
---|
| 107 | shift -= 30;
|
---|
[6178] | 108 | mult = 1 << 30;
|
---|
[3145] | 109 | }
|
---|
| 110 | long this_quadpart = mult * (parent_index << shift);
|
---|
| 111 | this.quad = parent.quad | this_quadpart;
|
---|
| 112 | this.bbox = calculateBBox(); // calculateBBox reference quad
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private BBox calculateBBox() {
|
---|
| 116 | LatLon bottom_left = this.coor();
|
---|
| 117 | double lat = bottom_left.lat() + parent.height() / 2;
|
---|
| 118 | double lon = bottom_left.lon() + parent.width() / 2;
|
---|
[6203] | 119 | return new BBox(bottom_left.lon(), bottom_left.lat(), lon, lat);
|
---|
[3145] | 120 | }
|
---|
| 121 |
|
---|
[6178] | 122 | QBLevel<T> findBucket(BBox bbox) {
|
---|
[3603] | 123 | if (!hasChildren())
|
---|
[3150] | 124 | return this;
|
---|
| 125 | else {
|
---|
[6235] | 126 | int idx = bbox.getIndex(level);
|
---|
| 127 | if (idx == -1)
|
---|
[3150] | 128 | return this;
|
---|
[6235] | 129 | return getChild(idx).findBucket(bbox);
|
---|
[3150] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[6171] | 133 | boolean remove_content(T o) {
|
---|
[3584] | 134 | // If two threads try to remove item at the same time from different buckets of this QBLevel,
|
---|
| 135 | // it might happen that one thread removes bucket but don't remove parent because it still sees
|
---|
| 136 | // another bucket set. Second thread do the same. Due to thread memory caching, it's possible that
|
---|
| 137 | // changes made by threads will show up in children array too late, leading to QBLevel with all children
|
---|
| 138 | // set to null
|
---|
| 139 | if (content == null)
|
---|
| 140 | return false;
|
---|
| 141 | boolean ret = this.content.remove(o);
|
---|
[6093] | 142 | if (this.content.isEmpty()) {
|
---|
[3584] | 143 | this.content = null;
|
---|
[2388] | 144 | }
|
---|
[3584] | 145 | if (this.canRemove()) {
|
---|
| 146 | this.remove_from_parent();
|
---|
| 147 | }
|
---|
| 148 | return ret;
|
---|
[2263] | 149 | }
|
---|
[6171] | 150 |
|
---|
[2440] | 151 | /*
|
---|
| 152 | * There is a race between this and qb.nextContentNode().
|
---|
| 153 | * If nextContentNode() runs into this bucket, it may
|
---|
| 154 | * attempt to null out 'children' because it thinks this
|
---|
| 155 | * is a dead end.
|
---|
| 156 | */
|
---|
[3150] | 157 | void __split() {
|
---|
[2263] | 158 | List<T> tmpcontent = content;
|
---|
[2165] | 159 | content = null;
|
---|
[3476] | 160 |
|
---|
[6178] | 161 | for (T o : tmpcontent) {
|
---|
[6235] | 162 | int idx = o.getBBox().getIndex(level);
|
---|
| 163 | if (idx == -1) {
|
---|
[3476] | 164 | __add_content(o);
|
---|
| 165 | } else {
|
---|
[6235] | 166 | getChild(idx).doAdd(o);
|
---|
[3476] | 167 | }
|
---|
[2165] | 168 | }
|
---|
[3476] | 169 | isLeaf = false; // It's not enough to check children because all items could end up in this level (index == -1)
|
---|
[2440] | 170 | }
|
---|
[3150] | 171 |
|
---|
[6171] | 172 | boolean __add_content(T o) {
|
---|
| 173 | // The split_lock will keep two concurrent calls from overwriting content
|
---|
[2388] | 174 | if (content == null) {
|
---|
[7005] | 175 | content = new ArrayList<>();
|
---|
[2388] | 176 | }
|
---|
[8375] | 177 | return content.add(o);
|
---|
[2263] | 178 | }
|
---|
[6178] | 179 |
|
---|
| 180 | boolean matches(final T o, final BBox search_bbox) {
|
---|
[8510] | 181 | if (o instanceof Node) {
|
---|
| 182 | final LatLon latLon = ((Node) o).getCoor();
|
---|
[6178] | 183 | // node without coords -> bbox[0,0,0,0]
|
---|
| 184 | return search_bbox.bounds(latLon != null ? latLon : LatLon.ZERO);
|
---|
| 185 | }
|
---|
[2427] | 186 | return o.getBBox().intersects(search_bbox);
|
---|
[2165] | 187 | }
|
---|
[6178] | 188 |
|
---|
[6171] | 189 | private void search_contents(BBox search_bbox, List<T> result) {
|
---|
[2165] | 190 | /*
|
---|
| 191 | * It is possible that this was created in a split
|
---|
| 192 | * but never got any content populated.
|
---|
| 193 | */
|
---|
| 194 | if (content == null)
|
---|
[3151] | 195 | return;
|
---|
| 196 |
|
---|
[2263] | 197 | for (T o : content) {
|
---|
[2388] | 198 | if (matches(o, search_bbox)) {
|
---|
[3151] | 199 | result.add(o);
|
---|
[2388] | 200 | }
|
---|
[2165] | 201 | }
|
---|
| 202 | }
|
---|
[6178] | 203 |
|
---|
[2165] | 204 | /*
|
---|
[6171] | 205 | * This is stupid. I tried to have a QBLeaf and QBBranch
|
---|
[6178] | 206 | * class descending from a QBLevel. It's more than twice
|
---|
[6171] | 207 | * as slow. So, this throws OO out the window, but it
|
---|
| 208 | * is fast. Runtime type determination must be slow.
|
---|
[2165] | 209 | */
|
---|
[3476] | 210 | boolean isLeaf() {
|
---|
| 211 | return isLeaf;
|
---|
[2165] | 212 | }
|
---|
[6178] | 213 |
|
---|
[3476] | 214 | boolean hasChildren() {
|
---|
[6181] | 215 | return nw != null || ne != null || sw != null || se != null;
|
---|
[3476] | 216 | }
|
---|
| 217 |
|
---|
[6178] | 218 | QBLevel<T> next_sibling() {
|
---|
| 219 | return (parent == null) ? null : parent.firstSiblingOf(this);
|
---|
[2165] | 220 | }
|
---|
[6178] | 221 |
|
---|
[6171] | 222 | boolean hasContent() {
|
---|
[2441] | 223 | return content != null;
|
---|
[2263] | 224 | }
|
---|
[6178] | 225 |
|
---|
| 226 | QBLevel<T> nextSibling() {
|
---|
| 227 | QBLevel<T> next = this;
|
---|
| 228 | QBLevel<T> sibling = next.next_sibling();
|
---|
[8375] | 229 | // Walk back up the tree to find the next sibling node.
|
---|
| 230 | // It may be either a leaf or branch.
|
---|
[2423] | 231 | while (sibling == null) {
|
---|
| 232 | next = next.parent;
|
---|
| 233 | if (next == null) {
|
---|
| 234 | break;
|
---|
[2427] | 235 | }
|
---|
[2423] | 236 | sibling = next.next_sibling();
|
---|
[2165] | 237 | }
|
---|
[8375] | 238 | return sibling;
|
---|
[2423] | 239 | }
|
---|
[6178] | 240 |
|
---|
| 241 | QBLevel<T> firstChild() {
|
---|
| 242 | if (sw != null)
|
---|
| 243 | return sw;
|
---|
| 244 | if (nw != null)
|
---|
| 245 | return nw;
|
---|
| 246 | if (se != null)
|
---|
| 247 | return se;
|
---|
| 248 | return ne;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | QBLevel<T> firstSiblingOf(final QBLevel<T> child) {
|
---|
| 252 | switch (child.index) {
|
---|
| 253 | case SW_INDEX:
|
---|
| 254 | if (nw != null)
|
---|
| 255 | return nw;
|
---|
| 256 | case NW_INDEX:
|
---|
| 257 | if (se != null)
|
---|
| 258 | return se;
|
---|
| 259 | case SE_INDEX:
|
---|
| 260 | return ne;
|
---|
[2440] | 261 | }
|
---|
[6178] | 262 | return null;
|
---|
[2440] | 263 | }
|
---|
[6178] | 264 |
|
---|
| 265 | QBLevel<T> nextNode() {
|
---|
[3603] | 266 | if (!this.hasChildren())
|
---|
[2449] | 267 | return this.nextSibling();
|
---|
| 268 | return this.firstChild();
|
---|
| 269 | }
|
---|
[6178] | 270 |
|
---|
| 271 | QBLevel<T> nextContentNode() {
|
---|
| 272 | QBLevel<T> next = this.nextNode();
|
---|
[2165] | 273 | if (next == null)
|
---|
[2449] | 274 | return next;
|
---|
| 275 | if (next.hasContent())
|
---|
| 276 | return next;
|
---|
| 277 | return next.nextContentNode();
|
---|
[2165] | 278 | }
|
---|
[3150] | 279 |
|
---|
| 280 | void doAdd(T o) {
|
---|
[2263] | 281 | if (consistency_testing) {
|
---|
[3150] | 282 | if (!matches(o, this.bbox())) {
|
---|
[6171] | 283 | o.getBBox().getIndex(level);
|
---|
[6178] | 284 | o.getBBox().getIndex(level - 1);
|
---|
[3150] | 285 | abort("\nobject " + o + " does not belong in node at level: " + level + " bbox: " + this.bbox());
|
---|
[2263] | 286 | }
|
---|
| 287 | }
|
---|
[3584] | 288 | __add_content(o);
|
---|
| 289 | if (isLeaf() && content.size() > MAX_OBJECTS_PER_LEVEL && level < QuadTiling.NR_LEVELS) {
|
---|
| 290 | __split();
|
---|
[2388] | 291 | }
|
---|
[2165] | 292 | }
|
---|
[3150] | 293 |
|
---|
| 294 | void add(T o) {
|
---|
[3584] | 295 | findBucket(o.getBBox()).doAdd(o);
|
---|
[2165] | 296 | }
|
---|
[3150] | 297 |
|
---|
[6171] | 298 | private void search(BBox search_bbox, List<T> result) {
|
---|
[4869] | 299 | if (!this.bbox().intersects(search_bbox))
|
---|
[3151] | 300 | return;
|
---|
[4869] | 301 | else if (bbox().bounds(search_bbox)) {
|
---|
[8346] | 302 | buckets.searchCache = this;
|
---|
[2165] | 303 | }
|
---|
[3476] | 304 |
|
---|
[2388] | 305 | if (this.hasContent()) {
|
---|
[3151] | 306 | search_contents(search_bbox, result);
|
---|
[2388] | 307 | }
|
---|
[3151] | 308 |
|
---|
[3476] | 309 | //TODO Coincidence vector should be calculated here and only buckets that match search_bbox should be checked
|
---|
| 310 |
|
---|
| 311 | if (nw != null) {
|
---|
| 312 | nw.search(search_bbox, result);
|
---|
[2165] | 313 | }
|
---|
[3476] | 314 | if (ne != null) {
|
---|
| 315 | ne.search(search_bbox, result);
|
---|
| 316 | }
|
---|
| 317 | if (se != null) {
|
---|
| 318 | se.search(search_bbox, result);
|
---|
| 319 | }
|
---|
| 320 | if (sw != null) {
|
---|
| 321 | sw.search(search_bbox, result);
|
---|
| 322 | }
|
---|
[2165] | 323 | }
|
---|
[6178] | 324 |
|
---|
[6171] | 325 | public String quads() {
|
---|
[2165] | 326 | return Long.toHexString(quad);
|
---|
| 327 | }
|
---|
[6178] | 328 |
|
---|
| 329 | int index_of(QBLevel<T> find_this) {
|
---|
| 330 | QBLevel<T>[] children = getChildren();
|
---|
[2165] | 331 | for (int i = 0; i < QuadTiling.TILES_PER_LEVEL; i++) {
|
---|
| 332 | if (children[i] == find_this)
|
---|
| 333 | return i;
|
---|
| 334 | }
|
---|
| 335 | return -1;
|
---|
| 336 | }
|
---|
[6178] | 337 |
|
---|
[3145] | 338 | double width() {
|
---|
| 339 | return bbox.width();
|
---|
[2165] | 340 | }
|
---|
[3145] | 341 |
|
---|
| 342 | double height() {
|
---|
| 343 | return bbox.height();
|
---|
[2165] | 344 | }
|
---|
[3145] | 345 |
|
---|
| 346 | public BBox bbox() {
|
---|
[2165] | 347 | return bbox;
|
---|
| 348 | }
|
---|
[6178] | 349 |
|
---|
[2165] | 350 | /*
|
---|
| 351 | * This gives the coordinate of the bottom-left
|
---|
| 352 | * corner of the box
|
---|
| 353 | */
|
---|
[6890] | 354 | final LatLon coor() {
|
---|
[2165] | 355 | return QuadTiling.tile2LatLon(this.quad);
|
---|
| 356 | }
|
---|
[6178] | 357 |
|
---|
[6171] | 358 | void remove_from_parent() {
|
---|
[2423] | 359 | if (parent == null)
|
---|
| 360 | return;
|
---|
| 361 |
|
---|
[3476] | 362 | if (!canRemove()) {
|
---|
| 363 | abort("attempt to remove non-empty child: " + this.content + " " + Arrays.toString(this.getChildren()));
|
---|
[2423] | 364 | }
|
---|
[3476] | 365 |
|
---|
| 366 | if (parent.nw == this) {
|
---|
| 367 | parent.nw = null;
|
---|
| 368 | } else if (parent.ne == this) {
|
---|
| 369 | parent.ne = null;
|
---|
| 370 | } else if (parent.sw == this) {
|
---|
| 371 | parent.sw = null;
|
---|
| 372 | } else if (parent.se == this) {
|
---|
| 373 | parent.se = null;
|
---|
[2911] | 374 | }
|
---|
[3476] | 375 |
|
---|
[2427] | 376 | if (parent.canRemove()) {
|
---|
[2423] | 377 | parent.remove_from_parent();
|
---|
[2427] | 378 | }
|
---|
[2423] | 379 | }
|
---|
[6178] | 380 |
|
---|
[6171] | 381 | boolean canRemove() {
|
---|
[6093] | 382 | if (content != null && !content.isEmpty())
|
---|
[2423] | 383 | return false;
|
---|
[2425] | 384 | if (this.hasChildren())
|
---|
| 385 | return false;
|
---|
[2423] | 386 | return true;
|
---|
| 387 | }
|
---|
[2165] | 388 | }
|
---|
| 389 |
|
---|
[6178] | 390 | private QBLevel<T> root;
|
---|
[8346] | 391 | private QBLevel<T> searchCache;
|
---|
[3157] | 392 | private int size;
|
---|
| 393 |
|
---|
[6171] | 394 | /**
|
---|
| 395 | * Constructs a new {@code QuadBuckets}.
|
---|
| 396 | */
|
---|
| 397 | public QuadBuckets() {
|
---|
[2165] | 398 | clear();
|
---|
| 399 | }
|
---|
[6178] | 400 |
|
---|
[6084] | 401 | @Override
|
---|
[6890] | 402 | public final void clear() {
|
---|
[7005] | 403 | root = new QBLevel<>(this);
|
---|
[8346] | 404 | searchCache = null;
|
---|
[3584] | 405 | size = 0;
|
---|
[2165] | 406 | }
|
---|
[6178] | 407 |
|
---|
[6084] | 408 | @Override
|
---|
[3157] | 409 | public boolean add(T n) {
|
---|
[3584] | 410 | root.add(n);
|
---|
| 411 | size++;
|
---|
| 412 | return true;
|
---|
[2165] | 413 | }
|
---|
[3584] | 414 |
|
---|
[6084] | 415 | @Override
|
---|
[6171] | 416 | public boolean retainAll(Collection<?> objects) {
|
---|
[2263] | 417 | for (T o : this) {
|
---|
[2388] | 418 | if (objects.contains(o)) {
|
---|
[2197] | 419 | continue;
|
---|
[2388] | 420 | }
|
---|
[2263] | 421 | if (!this.remove(o))
|
---|
[2197] | 422 | return false;
|
---|
| 423 | }
|
---|
| 424 | return true;
|
---|
[2165] | 425 | }
|
---|
[6178] | 426 |
|
---|
[6084] | 427 | @Override
|
---|
[6171] | 428 | public boolean removeAll(Collection<?> objects) {
|
---|
[2427] | 429 | boolean changed = false;
|
---|
[2263] | 430 | for (Object o : objects) {
|
---|
[2427] | 431 | changed = changed | remove(o);
|
---|
[2197] | 432 | }
|
---|
[2427] | 433 | return changed;
|
---|
[2165] | 434 | }
|
---|
[6178] | 435 |
|
---|
[6084] | 436 | @Override
|
---|
[6171] | 437 | public boolean addAll(Collection<? extends T> objects) {
|
---|
[2427] | 438 | boolean changed = false;
|
---|
[3154] | 439 | for (T o : objects) {
|
---|
| 440 | changed = changed | this.add(o);
|
---|
[2197] | 441 | }
|
---|
[2427] | 442 | return changed;
|
---|
[2165] | 443 | }
|
---|
[6178] | 444 |
|
---|
[6084] | 445 | @Override
|
---|
[6171] | 446 | public boolean containsAll(Collection<?> objects) {
|
---|
[2263] | 447 | for (Object o : objects) {
|
---|
| 448 | if (!this.contains(o))
|
---|
[2197] | 449 | return false;
|
---|
| 450 | }
|
---|
| 451 | return true;
|
---|
[2165] | 452 | }
|
---|
[6178] | 453 |
|
---|
[6084] | 454 | @Override
|
---|
[3157] | 455 | public boolean remove(Object o) {
|
---|
[6178] | 456 | @SuppressWarnings("unchecked")
|
---|
| 457 | T t = (T) o;
|
---|
[8346] | 458 | searchCache = null; // Search cache might point to one of removed buckets
|
---|
[6178] | 459 | QBLevel<T> bucket = root.findBucket(t.getBBox());
|
---|
[3584] | 460 | if (bucket.remove_content(t)) {
|
---|
| 461 | size--;
|
---|
| 462 | return true;
|
---|
| 463 | } else
|
---|
| 464 | return false;
|
---|
[2165] | 465 | }
|
---|
[6178] | 466 |
|
---|
[6084] | 467 | @Override
|
---|
[3154] | 468 | public boolean contains(Object o) {
|
---|
[6178] | 469 | @SuppressWarnings("unchecked")
|
---|
| 470 | T t = (T) o;
|
---|
| 471 | QBLevel<T> bucket = root.findBucket(t.getBBox());
|
---|
[3453] | 472 | return bucket != null && bucket.content != null && bucket.content.contains(t);
|
---|
[2165] | 473 | }
|
---|
[3154] | 474 |
|
---|
[8338] | 475 | public List<T> toList() {
|
---|
| 476 | List<T> a = new ArrayList<>();
|
---|
[2388] | 477 | for (T n : this) {
|
---|
[2165] | 478 | a.add(n);
|
---|
[2388] | 479 | }
|
---|
[2165] | 480 | return a;
|
---|
| 481 | }
|
---|
[6178] | 482 |
|
---|
[6084] | 483 | @Override
|
---|
[6171] | 484 | public Object[] toArray() {
|
---|
[8338] | 485 | return this.toList().toArray();
|
---|
[2165] | 486 | }
|
---|
[6178] | 487 |
|
---|
[6084] | 488 | @Override
|
---|
[6171] | 489 | public <A> A[] toArray(A[] template) {
|
---|
[8338] | 490 | return this.toList().toArray(template);
|
---|
[2165] | 491 | }
|
---|
[6178] | 492 |
|
---|
| 493 | class QuadBucketIterator implements Iterator<T> {
|
---|
[8346] | 494 | private QBLevel<T> currentNode;
|
---|
| 495 | private int contentIndex;
|
---|
| 496 | private int iteratedOver;
|
---|
[6178] | 497 |
|
---|
[6890] | 498 | final QBLevel<T> next_content_node(QBLevel<T> q) {
|
---|
[2165] | 499 | if (q == null)
|
---|
| 500 | return null;
|
---|
[6178] | 501 | QBLevel<T> orig = q;
|
---|
| 502 | QBLevel<T> next;
|
---|
[3584] | 503 | next = q.nextContentNode();
|
---|
[2388] | 504 | if (orig == next) {
|
---|
[2165] | 505 | abort("got same leaf back leaf: " + q.isLeaf());
|
---|
[2388] | 506 | }
|
---|
[2165] | 507 | return next;
|
---|
| 508 | }
|
---|
[6178] | 509 |
|
---|
[6171] | 510 | public QuadBucketIterator(QuadBuckets<T> qb) {
|
---|
[3603] | 511 | if (!qb.root.hasChildren() || qb.root.hasContent()) {
|
---|
[8346] | 512 | currentNode = qb.root;
|
---|
[2388] | 513 | } else {
|
---|
[8346] | 514 | currentNode = next_content_node(qb.root);
|
---|
[2388] | 515 | }
|
---|
[8346] | 516 | iteratedOver = 0;
|
---|
[2165] | 517 | }
|
---|
[6178] | 518 |
|
---|
[6084] | 519 | @Override
|
---|
[6171] | 520 | public boolean hasNext() {
|
---|
[4869] | 521 | if (this.peek() == null)
|
---|
[2165] | 522 | return false;
|
---|
| 523 | return true;
|
---|
| 524 | }
|
---|
[6178] | 525 |
|
---|
[6171] | 526 | T peek() {
|
---|
[8346] | 527 | if (currentNode == null)
|
---|
[2165] | 528 | return null;
|
---|
[8346] | 529 | while ((currentNode.content == null) || (contentIndex >= currentNode.content.size())) {
|
---|
| 530 | contentIndex = 0;
|
---|
| 531 | currentNode = next_content_node(currentNode);
|
---|
| 532 | if (currentNode == null) {
|
---|
[2165] | 533 | break;
|
---|
[2388] | 534 | }
|
---|
[2165] | 535 | }
|
---|
[8346] | 536 | if (currentNode == null || currentNode.content == null)
|
---|
[2165] | 537 | return null;
|
---|
[8346] | 538 | return currentNode.content.get(contentIndex);
|
---|
[2165] | 539 | }
|
---|
[6178] | 540 |
|
---|
[6084] | 541 | @Override
|
---|
[6171] | 542 | public T next() {
|
---|
[2263] | 543 | T ret = peek();
|
---|
[8346] | 544 | contentIndex++;
|
---|
| 545 | iteratedOver++;
|
---|
[2165] | 546 | return ret;
|
---|
| 547 | }
|
---|
[6178] | 548 |
|
---|
[6084] | 549 | @Override
|
---|
[6171] | 550 | public void remove() {
|
---|
[2197] | 551 | // two uses
|
---|
| 552 | // 1. Back up to the thing we just returned
|
---|
| 553 | // 2. move the index back since we removed
|
---|
| 554 | // an element
|
---|
[8346] | 555 | contentIndex--;
|
---|
[3000] | 556 | T object = peek();
|
---|
[8346] | 557 | currentNode.remove_content(object);
|
---|
[2165] | 558 | }
|
---|
| 559 | }
|
---|
[6178] | 560 |
|
---|
[6084] | 561 | @Override
|
---|
[6171] | 562 | public Iterator<T> iterator() {
|
---|
[2165] | 563 | return new QuadBucketIterator(this);
|
---|
| 564 | }
|
---|
[6171] | 565 |
|
---|
[6084] | 566 | @Override
|
---|
[3157] | 567 | public int size() {
|
---|
[3584] | 568 | return size;
|
---|
[2165] | 569 | }
|
---|
[3157] | 570 |
|
---|
[6084] | 571 | @Override
|
---|
[6171] | 572 | public boolean isEmpty() {
|
---|
[8318] | 573 | return size == 0;
|
---|
[2165] | 574 | }
|
---|
[6178] | 575 |
|
---|
[3151] | 576 | public List<T> search(BBox search_bbox) {
|
---|
[7005] | 577 | List<T> ret = new ArrayList<>();
|
---|
[6171] | 578 | // Doing this cuts down search cost on a real-life data set by about 25%
|
---|
[8346] | 579 | if (searchCache == null) {
|
---|
| 580 | searchCache = root;
|
---|
[8318] | 581 | }
|
---|
| 582 | // Walk back up the tree when the last search spot can not cover the current search
|
---|
[8346] | 583 | while (searchCache != null && !searchCache.bbox().bounds(search_bbox)) {
|
---|
| 584 | searchCache = searchCache.parent;
|
---|
[8318] | 585 | }
|
---|
[4178] | 586 |
|
---|
[8346] | 587 | if (searchCache == null) {
|
---|
| 588 | searchCache = root;
|
---|
[8318] | 589 | Main.info("bbox: " + search_bbox + " is out of the world");
|
---|
[2165] | 590 | }
|
---|
[2441] | 591 |
|
---|
[8346] | 592 | // Save parent because searchCache might change during search call
|
---|
| 593 | QBLevel<T> tmp = searchCache.parent;
|
---|
[2441] | 594 |
|
---|
[8346] | 595 | searchCache.search(search_bbox, ret);
|
---|
[2441] | 596 |
|
---|
[2263] | 597 | // A way that spans this bucket may be stored in one
|
---|
| 598 | // of the nodes which is a parent of the search cache
|
---|
| 599 | while (tmp != null) {
|
---|
[3151] | 600 | tmp.search_contents(search_bbox, ret);
|
---|
[2263] | 601 | tmp = tmp.parent;
|
---|
| 602 | }
|
---|
| 603 | return ret;
|
---|
[2165] | 604 | }
|
---|
| 605 | }
|
---|