Skip to content

Commit

Permalink
undo generics for now
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Nov 7, 2023
1 parent 7f99fb3 commit 75ba927
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*
* @version 1.7
*/
public class ArrayListVisitor<T>
implements ItemVisitor<T>
public class ArrayListVisitor
implements ItemVisitor
{

private final ArrayList<T> items = new ArrayList<>();
private ArrayList items = new ArrayList();

/**
* Creates a new instance.
Expand All @@ -36,7 +36,7 @@ public ArrayListVisitor() {
*
* @param item the item to visit
*/
public void visitItem(T item)
public void visitItem(Object item)
{
items.add(item);
}
Expand All @@ -46,6 +46,6 @@ public void visitItem(T item)
*
* @return the array of items
*/
public ArrayList<T> getItems() { return items; }
public ArrayList getItems() { return items; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* @version 1.7
*/

public interface ItemVisitor<T>
public interface ItemVisitor
{
/**
* Visits an item in the index.
*
* @param item the index item to be visited
*/
void visitItem(T item);
void visitItem(Object item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
*
* @version 1.7
*/
public interface SpatialIndex<T>
public interface SpatialIndex
{
/**
* Adds a spatial item with an extent specified by the given {@link Envelope} to the index
*/
void insert(Envelope itemEnv, T item);
void insert(Envelope itemEnv, Object item);

/**
* Queries the index for all items whose extents intersect the given search {@link Envelope}
Expand All @@ -41,7 +41,7 @@ public interface SpatialIndex<T>
* @param searchEnv the envelope to query for
* @return a list of the items found by the query
*/
List<T> query(Envelope searchEnv);
List query(Envelope searchEnv);

/**
* Queries the index for all items whose extents intersect the given search {@link Envelope},
Expand All @@ -52,7 +52,7 @@ public interface SpatialIndex<T>
* @param searchEnv the envelope to query for
* @param visitor a visitor object to apply to the items found
*/
void query(Envelope searchEnv, ItemVisitor<? super T> visitor);
void query(Envelope searchEnv, ItemVisitor visitor);

/**
* Removes a single item from the tree.
Expand All @@ -61,6 +61,6 @@ public interface SpatialIndex<T>
* @param item the item to remove
* @return <code>true</code> if the item was found
*/
boolean remove(Envelope itemEnv, T item);
boolean remove(Envelope itemEnv, Object item);

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@
* @author Martin Davis
*
*/
public class HPRtree<T>
implements SpatialIndex<T>
public class HPRtree
implements SpatialIndex
{
private static final int ENV_SIZE = 4;

private static final int HILBERT_LEVEL = 12;

private static final int DEFAULT_NODE_CAPACITY = 16;

private List<Item<T>> itemsToLoad = new ArrayList<>();
private List<Item> itemsToLoad = new ArrayList<>();

private final int nodeCapacity;

Expand Down Expand Up @@ -110,29 +110,29 @@ public int size() {
}

@Override
public void insert(Envelope itemEnv, T item) {
public void insert(Envelope itemEnv, Object item) {
if (isBuilt) {
throw new IllegalStateException("Cannot insert items after tree is built.");
}
numItems++;
itemsToLoad.add( new Item<>(itemEnv, item) );
itemsToLoad.add( new Item(itemEnv, item) );
totalExtent.expandToInclude(itemEnv);
}

@Override
public List<T> query(Envelope searchEnv) {
public List query(Envelope searchEnv) {
build();

if (! totalExtent.intersects(searchEnv))
return new ArrayList<>();
return new ArrayList();

ArrayListVisitor<T> visitor = new ArrayListVisitor<>();
ArrayListVisitor visitor = new ArrayListVisitor();
query(searchEnv, visitor);
return visitor.getItems();
}

@Override
public void query(Envelope searchEnv, ItemVisitor<? super T> visitor) {
public void query(Envelope searchEnv, ItemVisitor visitor) {
build();
if (! totalExtent.intersects(searchEnv))
return;
Expand All @@ -144,7 +144,7 @@ public void query(Envelope searchEnv, ItemVisitor<? super T> visitor) {
}
}

private void queryTopLayer(Envelope searchEnv, ItemVisitor<? super T> visitor) {
private void queryTopLayer(Envelope searchEnv, ItemVisitor visitor) {
int layerIndex = layerStartIndex.length - 2;
int layerSize = layerSize(layerIndex);
// query each node in layer
Expand All @@ -153,7 +153,7 @@ private void queryTopLayer(Envelope searchEnv, ItemVisitor<? super T> visitor) {
}
}

private void queryNode(int layerIndex, int nodeOffset, Envelope searchEnv, ItemVisitor<? super T> visitor) {
private void queryNode(int layerIndex, int nodeOffset, Envelope searchEnv, ItemVisitor visitor) {
int layerStart = layerStartIndex[layerIndex];
int nodeIndex = layerStart + nodeOffset;
if (! intersects(nodeBounds, nodeIndex, searchEnv)) return;
Expand All @@ -175,7 +175,7 @@ private static boolean intersects(double[] bounds, int nodeIndex, Envelope env)
return ! isBeyond;
}

private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchEnv, ItemVisitor<? super T> visitor) {
private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchEnv, ItemVisitor visitor) {
int layerStart = layerStartIndex[layerIndex];
int layerEnd = layerStartIndex[layerIndex + 1];
for (int i = 0; i < nodeCapacity; i++) {
Expand All @@ -187,15 +187,13 @@ private void queryNodeChildren(int layerIndex, int blockOffset, Envelope searchE
}
}

private void queryItems(int blockStart, Envelope searchEnv, ItemVisitor<? super T> visitor) {
private void queryItems(int blockStart, Envelope searchEnv, ItemVisitor visitor) {
for (int i = 0; i < nodeCapacity; i++) {
int itemIndex = blockStart + i;
// don't query past end of items
if (itemIndex >= numItems) break;
if (intersects(itemBounds, itemIndex * ENV_SIZE, searchEnv)) {
@SuppressWarnings("unchecked")
T item = (T) itemValues[itemIndex];
visitor.visitItem(item);
visitor.visitItem(itemValues[itemIndex]);
}
}
}
Expand All @@ -207,7 +205,7 @@ private int layerSize(int layerIndex) {
}

@Override
public boolean remove(Envelope itemEnv, T item) {
public boolean remove(Envelope itemEnv, Object item) {
// TODO Auto-generated method stub
return false;
}
Expand Down Expand Up @@ -252,7 +250,7 @@ private void prepareItems() {
int valueIndex = 0;
itemBounds = new double[itemsToLoad.size() * 4];
itemValues = new Object[itemsToLoad.size()];
for (Item<T> item : itemsToLoad) {
for (Item item : itemsToLoad) {
Envelope envelope = item.getEnvelope();
itemBounds[boundsIndex++] = envelope.getMinX();
itemBounds[boundsIndex++] = envelope.getMinY();
Expand Down Expand Up @@ -365,7 +363,7 @@ private void sortItems() {
HilbertEncoder encoder = new HilbertEncoder(HILBERT_LEVEL, totalExtent);
int[] hilbertValues = new int[itemsToLoad.size()];
int pos = 0;
for (Item<T> item : itemsToLoad) {
for (Item item : itemsToLoad) {
hilbertValues[pos++] = encoder.encode(item.getEnvelope());
}
quickSortItemsIntoNodes(hilbertValues, 0, itemsToLoad.size() - 1);
Expand Down Expand Up @@ -395,7 +393,7 @@ private int hoarePartition(int[] values, int lo, int hi) {
}

private void swapItems(int[] values, int i, int j) {
Item<T> tmpItemp = itemsToLoad.get(i);
Item tmpItemp = itemsToLoad.get(i);
itemsToLoad.set(i, itemsToLoad.get(j));
itemsToLoad.set(j, tmpItemp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

import org.locationtech.jts.geom.Envelope;

public class Item<T> {
public class Item {

private final Envelope env;
private final T item;
private Envelope env;
private Object item;

public Item(Envelope env, T item) {
public Item(Envelope env, Object item) {
this.env = env;
this.item = item;
}
Expand All @@ -27,7 +27,7 @@ public Envelope getEnvelope() {
return env;
}

public T getItem() {
public Object getItem() {
return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
public class MCIndexNoder
extends SinglePassNoder
{
private final List<MonotoneChain> monoChains = new ArrayList<>();
private final SpatialIndex<MonotoneChain> index = new HPRtree<>();
private List monoChains = new ArrayList();
private SpatialIndex index= new HPRtree();
private int idCounter = 0;
private Collection nodedSegStrings;
// statistics
Expand Down Expand Up @@ -69,9 +69,9 @@ public MCIndexNoder(SegmentIntersector si, double overlapTolerance)
this.overlapTolerance = overlapTolerance;
}

public List<MonotoneChain> getMonotoneChains() { return monoChains; }
public List getMonotoneChains() { return monoChains; }

public SpatialIndex<MonotoneChain> getIndex() { return index; }
public SpatialIndex getIndex() { return index; }

public Collection getNodedSubstrings()
{
Expand All @@ -91,9 +91,13 @@ public void computeNodes(Collection inputSegStrings)
private void intersectChains()
{
MonotoneChainOverlapAction overlapAction = new SegmentOverlapAction(segInt);
for (MonotoneChain queryChain : monoChains) {

for (Iterator i = monoChains.iterator(); i.hasNext(); ) {
MonotoneChain queryChain = (MonotoneChain) i.next();
Envelope queryEnv = queryChain.getEnvelope(overlapTolerance);
for (MonotoneChain testChain : index.query(queryEnv)) {
List overlapChains = index.query(queryEnv);
for (Iterator j = overlapChains.iterator(); j.hasNext(); ) {
MonotoneChain testChain = (MonotoneChain) j.next();
/**
* following test makes sure we only compare each pair of chains once
* and that we don't compare a chain to itself
Expand All @@ -103,7 +107,8 @@ private void intersectChains()
nOverlaps++;
}
// short-circuit if possible
if (segInt.isDone()) return;
if (segInt.isDone())
return;
}
}
}
Expand Down

0 comments on commit 75ba927

Please sign in to comment.