Skip to content

Commit

Permalink
NPE with getStats() on empty tree (#36)
Browse files Browse the repository at this point in the history
* Empty tree getStats()
  • Loading branch information
tzaeschke authored Jun 22, 2024
1 parent 04fa75a commit de6a03c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- getStats() for empty trees fails. [#36](https://github.com/tzaeschke/phtree/pull/36)

## 2.8.0 - 2023-07-29

- Proper kNN implementation with MinMaxHeaps [#33](https://github.com/tzaeschke/phtree/pull/33)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ch/ethz/globis/phtree/v13/PhTree13.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public int size() {

@Override
public PhTreeStats getStats() {
if (getRoot() == null) {
return new PhTreeStats(DEPTH_64);
}
return getStats(0, getRoot(), new PhTreeStats(DEPTH_64));
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ch/ethz/globis/phtree/v16/PhTree16.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public int size() {

@Override
public PhTreeStats getStats() {
if (getRoot() == null) {
return new PhTreeStats(DEPTH_64);
}
return getStats(0, getRoot(), new PhTreeStats(DEPTH_64));
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ch/ethz/globis/phtree/v16hd/PhTree16HD.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public int size() {

@Override
public PhTreeStats getStats() {
if (getRoot() == null) {
return new PhTreeStats(DEPTH_64);
}
return getStats(0, getRoot(), new PhTreeStats(DEPTH_64));
}

Expand Down
26 changes: 20 additions & 6 deletions src/test/java/ch/ethz/globis/phtree/test/TestMultiMapF2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ch.ethz.globis.phtree.PhTreeMultiMapF2.*;
import ch.ethz.globis.phtree.util.BitTools;
import ch.ethz.globis.phtree.util.Bits;
import ch.ethz.globis.phtree.util.PhTreeStats;
import org.junit.Test;

import java.util.*;
Expand Down Expand Up @@ -121,8 +122,8 @@ public void testCRUD_JDK8() {
break;
case 1: {
final int id2 = id;
assertEquals(id2, (int) idx.computeIfAbsent(v, id2, (v2) -> id2));
assertNull(idx.computeIfAbsent(v, id2, (v2) -> id2));
assertEquals(id2, (int) idx.computeIfAbsent(v, id2, v2 -> id2));
assertNull(idx.computeIfAbsent(v, id2, v2 -> id2));
break;
}
case 2:
Expand Down Expand Up @@ -226,15 +227,15 @@ public void testKNN() {
assertTrue(3 <= result.size());

result = toList(idx.nearestNeighbour(1, 1, 1));
assertTrue(1 <= result.size());
assertFalse(result.isEmpty());
check(result.get(0).getKey(), 1, 1);

result = toList(idx.nearestNeighbour(1, 1, 3));
assertTrue(1 <= result.size());
assertFalse(result.isEmpty());
check(result.get(0).getKey(), 1, 3);

result = toList(idx.nearestNeighbour(1, 3, 1));
assertTrue(1 <= result.size());
assertFalse(result.isEmpty());
check(result.get(0).getKey(), 3, 1);
}

Expand Down Expand Up @@ -273,7 +274,7 @@ public void testKnnLarge() {
v[j] = R.nextDouble() * MAXV;
}
list.forEach(xx -> xx.dist = dist(v, xx.key));
list.sort((o1, o2) -> Double.compare(o1.dist, o2.dist));
list.sort(Comparator.comparingDouble(o -> o.dist));
List<PhEntryDistF<Integer>> nnList = toList(q.reset(MIN_RESULT, PhDistanceF.THIS, v));
assertFalse("i=" + i + " d=" + d, nnList.isEmpty());
for (int x = 0; x < MIN_RESULT; ++x) {
Expand Down Expand Up @@ -483,4 +484,17 @@ public EntryDist(double[] key, T v, double dist) {
this.dist = dist;
}
}

@Test
public void testEmptyTreeStats() {
testEmptyTreeStats(2);
testEmptyTreeStats(60);
testEmptyTreeStats(600);
}

private void testEmptyTreeStats(int dim) {
PhTreeMultiMapF2<Integer> ind = newTree(dim);
PhTreeStats stats = ind.getStats();
assertEquals(0, stats.nNodes);
}
}

0 comments on commit de6a03c

Please sign in to comment.