private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
private boolean removeItem(AbstractNode node, Object item) { Boundable childToRemove = null; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof ItemBoundable) { if ( ((ItemBoundable) childBoundable).getItem() == item) childToRemove = childBoundable; } } if (childToRemove != null) { node.getChildBoundables().remove(childToRemove); return true; } return false; }
protected int depth(AbstractNode node) { int maxChildDepth = 0; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { int childDepth = depth((AbstractNode) childBoundable); if (childDepth > maxChildDepth) maxChildDepth = childDepth; } } return maxChildDepth + 1; }
protected int size(AbstractNode node) { int size = 0; for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { size += size((AbstractNode) childBoundable); } else if (childBoundable instanceof ItemBoundable) { size += 1; } } return size; }
private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
private long computeMemorySizeInBytes(AbstractNode root) { if (root.getLevel() == 0) { return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((ItemBoundable) child)).sum(); } return ABSTRACT_NODE_INSTANCE_SIZE + ENVELOPE_INSTANCE_SIZE + root.getChildBoundables().stream().mapToLong(child -> computeMemorySizeInBytes((AbstractNode) child)).sum(); }
private static void addBounds(Boundable bnd, List bounds, GeometryFactory factory) { // don't include bounds of leaf nodes if (! (bnd instanceof AbstractNode)) return; Envelope env = (Envelope) bnd.getBounds(); bounds.add(factory.toGeometry(env)); if (bnd instanceof AbstractNode) { AbstractNode node = (AbstractNode) bnd; List children = node.getChildBoundables(); for (Iterator i = children.iterator(); i.hasNext(); ) { Boundable child = (Boundable) i.next(); addBounds(child, bounds, factory); } } }
private List itemsTree(AbstractNode node) { List valuesTreeForNode = new ArrayList(); for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (childBoundable instanceof AbstractNode) { List valuesTreeForChild = itemsTree((AbstractNode) childBoundable); // only add if not null (which indicates an item somewhere in this tree if (valuesTreeForChild != null) valuesTreeForNode.add(valuesTreeForChild); } else if (childBoundable instanceof ItemBoundable) { valuesTreeForNode.add(((ItemBoundable)childBoundable).getItem()); } else { Assert.shouldNeverReachHere(); } } if (valuesTreeForNode.size() <= 0) return null; return valuesTreeForNode; }
private boolean remove(Object searchBounds, AbstractNode node, Object item) { // first try removing item from this node boolean found = removeItem(node, item); if (found) return true; AbstractNode childToPrune = null; // next try removing item from lower nodes for (Iterator i = node.getChildBoundables().iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (!getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) { continue; } if (childBoundable instanceof AbstractNode) { found = remove(searchBounds, (AbstractNode) childBoundable, item); // if found, record child for pruning and exit if (found) { childToPrune = (AbstractNode) childBoundable; break; } } } // prune child if possible if (childToPrune != null) { if (childToPrune.getChildBoundables().isEmpty()) { node.getChildBoundables().remove(childToPrune); } } return found; }
/** * @param level -1 to get items */ private void boundablesAtLevel(int level, AbstractNode top, Collection boundables) { Assert.isTrue(level > -2); if (top.getLevel() == level) { boundables.add(top); return; } for (Iterator i = top.getChildBoundables().iterator(); i.hasNext(); ) { Boundable boundable = (Boundable) i.next(); if (boundable instanceof AbstractNode) { boundablesAtLevel(level, (AbstractNode)boundable, boundables); } else { Assert.isTrue(boundable instanceof ItemBoundable); if (level == -1) { boundables.add(boundable); } } } return; }
/** * Sorts the childBoundables then divides them into groups of size M, where * M is the node capacity. */ protected List createParentBoundables(List childBoundables, int newLevel) { Assert.isTrue(!childBoundables.isEmpty()); ArrayList parentBoundables = new ArrayList(); parentBoundables.add(createNode(newLevel)); ArrayList sortedChildBoundables = new ArrayList(childBoundables); Collections.sort(sortedChildBoundables, getComparator()); for (Iterator i = sortedChildBoundables.iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (lastNode(parentBoundables).getChildBoundables().size() == getNodeCapacity()) { parentBoundables.add(createNode(newLevel)); } lastNode(parentBoundables).addChildBoundable(childBoundable); } return parentBoundables; }
private void expand(Boundable bndComposite, Boundable bndOther, boolean isFlipped, PriorityQueue priQ, double minDistance) { List children = ((AbstractNode) bndComposite).getChildBoundables(); for (Iterator i = children.iterator(); i.hasNext(); ) { Boundable child = (Boundable) i.next(); BoundablePair bp; if (isFlipped) { bp = new BoundablePair(bndOther, child, itemDistance); } else { bp = new BoundablePair(child, bndOther, itemDistance); } // only add to queue if this pair might contain the closest points // MD - it's actually faster to construct the object rather than called distance(child, bndOther)! if (bp.getDistance() < minDistance) { priQ.add(bp); } } } }
private void queryInternal(Object searchBounds, AbstractNode node, List matches) { List childBoundables = node.getChildBoundables(); for (int i = 0; i < childBoundables.size(); i++) { Boundable childBoundable = (Boundable) childBoundables.get(i); if (! getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) { continue; } if (childBoundable instanceof AbstractNode) { queryInternal(searchBounds, (AbstractNode) childBoundable, matches); } else if (childBoundable instanceof ItemBoundable) { matches.add(((ItemBoundable)childBoundable).getItem()); } else { Assert.shouldNeverReachHere(); } } }
private void doTestCreateParentsFromVerticalSlice(int childCount, int nodeCapacity, int expectedChildrenPerParentBoundable, int expectedChildrenOfLastParent) { STRtreeDemo.TestTree t = new STRtreeDemo.TestTree(nodeCapacity); List parentBoundables = t.createParentBoundablesFromVerticalSlice(itemWrappers(childCount), 0); for (int i = 0; i < parentBoundables.size() - 1; i++) {//-1 AbstractNode parentBoundable = (AbstractNode) parentBoundables.get(i); assertEquals(expectedChildrenPerParentBoundable, parentBoundable.getChildBoundables().size()); } AbstractNode lastParent = (AbstractNode) parentBoundables.get(parentBoundables.size() - 1); assertEquals(expectedChildrenOfLastParent, lastParent.getChildBoundables().size()); }
private void queryInternal(Object searchBounds, AbstractNode node, ItemVisitor visitor) { List childBoundables = node.getChildBoundables(); for (int i = 0; i < childBoundables.size(); i++) { Boundable childBoundable = (Boundable) childBoundables.get(i); if (! getIntersectsOp().intersects(childBoundable.getBounds(), searchBounds)) { continue; } if (childBoundable instanceof AbstractNode) { queryInternal(searchBounds, (AbstractNode) childBoundable, visitor); } else if (childBoundable instanceof ItemBoundable) { visitor.visitItem(((ItemBoundable)childBoundable).getItem()); } else { Assert.shouldNeverReachHere(); } } }