Permalink
Browse files

Fixed bug when tree was empty: thanks to Fred Long for bug report and…

… tips
  • Loading branch information...
rweeks committed Oct 22, 2011
1 parent 23ed39a commit 2f710a384ed5eda4c35ebafc15534203e4eceb71
Showing with 90 additions and 2 deletions.
  1. +12 −0 src/com/newbrightidea/util/RTree.java
  2. +78 −2 test/com/newbrightidea/util/TestRTree.java
@@ -95,6 +95,14 @@ public int getNumDims()
return numDims;
}
+ /**
+ * @return the number of items in this tree.
+ */
+ public int size()
+ {
+ return size;
+ }
+
/**
* Searches the RTree for objects overlapping with the given rectangle.
*
@@ -176,6 +184,10 @@ public boolean delete(float[] coords, float[] dimensions, T entry)
condenseTree(l);
size--;
}
+ if ( size == 0 )
+ {
+ root = buildRoot(true);
+ }
return (removed != null);
}
@@ -2,6 +2,7 @@
import java.io.FileOutputStream;
import java.io.OutputStream;
+import java.util.Iterator;
import java.util.List;
import org.junit.Test;
@@ -87,7 +88,7 @@ public void testEmptyResults()
new float[] { 0.5f, 0.5f});
assert(results.isEmpty());
}
-
+
@Test
public void testSplitNodesSmall()
{
@@ -104,7 +105,82 @@ public void testSplitNodesSmall()
assert (results.size() == 1);
assert (results.get(0).equals(entries[2]));
}
-
+
+ @Test
+ public void testRemoveAll()
+ {
+ // setup is like testSplitNodesSmall
+ RTree<Object> rt = new RTree(2,1,2);
+ float[][] coords = new float[][] { {0.0f, 0.0f}, {1.0f, 1.0f}, {2.0f, 2.0f}, {3.0f, 3.0f} };
+ float[] dims = new float[]{0.5f, 0.5f};
+ Object[] entries = new Object[] { new Object(), new Object(), new Object(), new Object() };
+ for (int i = 0; i < entries.length; i++ )
+ {
+ rt.insert(coords[i], dims, entries[i]);
+ }
+ List<Object> results = rt.search(new float[] {2.0f, 2.0f},
+ new float[] {0.5f, 0.5f});
+ assert (results.size() == 1);
+ assert (results.get(0).equals(entries[2]));
+
+ float[] sCoords = new float[] { -0.5f * Float.MAX_VALUE, -0.5f * Float.MAX_VALUE };
+ float[] sDims = new float[] { Float.MAX_VALUE, Float.MAX_VALUE };
+ results = rt.search(sCoords, sDims);
+ assert(results.size() == rt.size());
+ for ( Object result: results )
+ {
+ boolean deleted = rt.delete(sCoords, sDims, result);
+ assert(deleted);
+ }
+ assert(rt.size() == 0);
+ float [] newCoords = new float[] { 0.0f, 0.0f };
+ float [] newDims = new float[] { 0.0f, 0.0f };
+ Object entry = new Object();
+ rt.insert(newCoords, newDims, entry);
+ assert( rt.search(newCoords, newDims).get(0) == entry );
+ }
+
+ @Test
+ public void testRemoveAlmostAll()
+ {
+ // setup is like testSplitNodesSmall
+ RTree<Object> rt = new RTree(2,1,2);
+ float[][] coords = new float[][] { {0.0f, 0.0f}, {1.0f, 1.0f}, {2.0f, 2.0f}, {3.0f, 3.0f} };
+ float[] dims = new float[]{0.5f, 0.5f};
+ Object[] entries = new Object[] { new Object(), new Object(), new Object(), new Object() };
+ for (int i = 0; i < entries.length; i++ )
+ {
+ rt.insert(coords[i], dims, entries[i]);
+ }
+ List<Object> results = rt.search(new float[] {2.0f, 2.0f},
+ new float[] {0.5f, 0.5f});
+ assert (results.size() == 1);
+ assert (results.get(0).equals(entries[2]));
+
+ float[] sCoords = new float[] { -0.5f * Float.MAX_VALUE, -0.5f * Float.MAX_VALUE };
+ float[] sDims = new float[] { Float.MAX_VALUE, Float.MAX_VALUE };
+ results = rt.search(sCoords, sDims);
+ assert(results.size() == rt.size());
+ Iterator resultIter = results.iterator();
+ while ( resultIter.hasNext() )
+ {
+ Object toRemove = resultIter.next();
+ if ( !resultIter.hasNext() )
+ {
+ break;
+ }
+ boolean deleted = rt.delete(sCoords, sDims, toRemove);
+ assert(deleted);
+ }
+
+ assert(rt.size() == 1);
+ float [] newCoords = new float[] { 0.0f, 0.0f };
+ float [] newDims = new float[] { 0.0f, 0.0f };
+ Object entry = new Object();
+ rt.insert(newCoords, newDims, entry);
+ assert( rt.search(newCoords, newDims).get(0) == entry );
+ }
+
@Test
public void testSplitNodesBig()
{

0 comments on commit 2f710a3

Please sign in to comment.