public T peek() { // if (isEmpty()) // throw new RuntimeException("cannot peek on empty stack"); // else return this.head.get(); }
public ImmutableStack<T> pop() { // if (isEmpty()) // throw new RuntimeException("cannot pop on empty stack"); // else return this.tail.get(); }
@Override public boolean equals(Object obj) { Optional<RectangleDouble> other = ObjectsHelper.asClass(obj, RectangleDouble.class); if (other.isPresent()) { return Objects.equal(x1, other.get().x1) && Objects.equal(x2, other.get().x2) && Objects.equal(y1, other.get().y1) && Objects.equal(y2, other.get().y2); } else return false; }
@Override public boolean equals(Object obj) { Optional<LineDouble> other = ObjectsHelper.asClass(obj, LineDouble.class); if (other.isPresent()) { return Objects.equal(x1, other.get().x1) && Objects.equal(x2, other.get().x2) && Objects.equal(y1, other.get().y1) && Objects.equal(y2, other.get().y2); } else return false; }
@Override public boolean equals(Object obj) { Optional<RectangleFloat> other = ObjectsHelper.asClass(obj, RectangleFloat.class); if (other.isPresent()) { return Objects.equal(x1, other.get().x1) && Objects.equal(x2, other.get().x2) && Objects.equal(y1, other.get().y1) && Objects.equal(y2, other.get().y2); } else return false; }
@Override public boolean equals(Object obj) { Optional<LineFloat> other = ObjectsHelper.asClass(obj, LineFloat.class); if (other.isPresent()) { return Objects.equal(x1, other.get().x1) && Objects.equal(x2, other.get().x2) && Objects.equal(y1, other.get().y1) && Objects.equal(y2, other.get().y2); } else return false; }
private static <T, S extends Geometry> int calculateDepth(Optional<? extends Node<T, S>> root) { if (!root.isPresent()) return 0; else return calculateDepth(root.get(), 0); }
public BufferedImage createImage() { final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Graphics2D g = (Graphics2D) image.getGraphics(); g.setBackground(Color.white); g.clearRect(0, 0, width, height); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f)); if (tree.root().isPresent()) { final List<RectangleDepth> nodeDepths = getNodeDepthsSortedByDepth(tree.root().get()); drawNode(g, nodeDepths); } return image; }
private static <R, S extends Geometry> int calculateMaxDepth( Optional<? extends Node<R, S>> root) { if (!root.isPresent()) return 0; else return calculateDepth(root.get(), 0); }
/** * Create an RTree by bulk loading, using the STR method. STR: a simple and * efficient algorithm for R-tree packing * http://ieeexplore.ieee.org/abstract/document/582015/ * <p> * Note: this method mutates the input entries, the internal order of the List * may be changed. * </p> * * @param entries * entries to be added to the r-tree * @return a loaded RTree */ @SuppressWarnings("unchecked") public <T, S extends Geometry> RTree<T, S> create(List<Entry<T, S>> entries) { setDefaultCapacity(); Context<T, S> context = new Context<T, S>(minChildren.get(), maxChildren.get(), selector, splitter, (Factory<T, S>) factory); return packingSTR(entries, true, entries.size(), context); }
/** * Returns a human readable form of the RTree. Here's an example: * * <pre> * mbr=Rectangle [x1=10.0, y1=4.0, x2=62.0, y2=85.0] * mbr=Rectangle [x1=28.0, y1=4.0, x2=34.0, y2=85.0] * entry=Entry [value=2, geometry=Point [x=29.0, y=4.0]] * entry=Entry [value=1, geometry=Point [x=28.0, y=19.0]] * entry=Entry [value=4, geometry=Point [x=34.0, y=85.0]] * mbr=Rectangle [x1=10.0, y1=45.0, x2=62.0, y2=63.0] * entry=Entry [value=5, geometry=Point [x=62.0, y=45.0]] * entry=Entry [value=3, geometry=Point [x=10.0, y=63.0]] * </pre> * * @return a string representation of the RTree */ public String asString() { if (!root.isPresent()) return ""; else return asString(root.get(), ""); }
/** * Builds the {@link RTree}. * * @param <T> * value type * @param <S> * geometry type * @return RTree */ @SuppressWarnings("unchecked") public <T, S extends Geometry> RTree<T, S> create() { setDefaultCapacity(); return new RTree<T, S>(Optional.<Node<T, S>>absent(), 0, new Context<T, S>(minChildren.get(), maxChildren.get(), selector, splitter, (Factory<T, S>) factory)); }
private void setDefaultCapacity() { if (!maxChildren.isPresent()) if (star) maxChildren = of(MAX_CHILDREN_DEFAULT_STAR); else maxChildren = of(MAX_CHILDREN_DEFAULT_GUTTMAN); if (!minChildren.isPresent()) minChildren = of((int) Math.round(maxChildren.get() * DEFAULT_FILLING_FACTOR)); }
/** * If the RTree has no entries returns {@link Optional#absent} otherwise returns * the minimum bounding rectangle of all entries in the RTree. * * @return minimum bounding rectangle of all entries in RTree */ public Optional<Rectangle> mbr() { if (!root.isPresent()) return absent(); else return of(root.get().geometry().mbr()); }
@VisibleForTesting static <T extends HasGeometry> T getBestCandidateForGroup(List<T> list, List<T> group, Rectangle groupMbr) { // TODO reduce allocations by not using Optional Optional<T> minEntry = absent(); Optional<Double> minArea = absent(); for (final T entry : list) { final double area = groupMbr.add(entry.geometry().mbr()).area(); if (!minArea.isPresent() || area < minArea.get()) { minArea = of(area); minEntry = of(entry); } } return minEntry.get(); }
@Override public Optional<Rectangle> call(Optional<Rectangle> r, Entry<T, S> entry) { if (r.isPresent()) return of(r.get().add(entry.geometry().mbr())); else return of(entry.geometry().mbr()); } }).toBlocking().single().or(rectangle(0, 0, 0, 0));
private void write(Kryo kryo, Output output, RTree<T, S> tree) { writeContext(tree.context(), output); output.writeBoolean(tree.root().isPresent()); output.writeInt(tree.size()); if (tree.root().isPresent()) { writeNode(tree.root().get(), output); } }