Add QuadTree toString() and getChildCountAtPos()

This commit is contained in:
James Seibel
2023-03-25 09:23:24 -05:00
parent a985e1c542
commit 0b05ced5b9
3 changed files with 56 additions and 5 deletions
@@ -55,7 +55,7 @@ public class QuadNode<T>
/** @return the number of non-null child nodes */
public int childCount()
public int getChildCount()
{
int count = 0;
for (int i = 0; i < 4; i++)
@@ -68,6 +68,21 @@ public class QuadNode<T>
return count;
}
/** returns the number of children that have non-null values */
public int getChildValueCount()
{
int count = 0;
for (int i = 0; i < 4; i++)
{
QuadNode<T> child = this.getChildByIndex(i);
if (child != null && child.value != null)
{
count++;
}
}
return count;
}
/**
@@ -241,7 +256,8 @@ public class QuadNode<T>
for (int i = 0; i < 4; i++)
{
DhSectionPos childPos = this.sectionPos.getChildByIndex(i);
callback.accept(this.getChildByIndex(i), childPos);
QuadNode<T> childNode = this.getChildByIndex(i);
callback.accept(childNode, childPos);
}
}
}
@@ -252,7 +268,7 @@ public class QuadNode<T>
*/
public void forAllLeafValues(Consumer<? super T> callback)
{
if (this.childCount() == 0 || this.sectionPos.sectionDetailLevel == this.minimumDetailLevel)
if (this.getChildCount() == 0 || this.sectionPos.sectionDetailLevel == this.minimumDetailLevel)
{
// base case, bottom leaf node found
callback.accept(this.value);
@@ -312,6 +328,6 @@ public class QuadNode<T>
@Override
public String toString() { return "pos: "+this.sectionPos+", value: "+this.value; }
public String toString() { return "pos: "+this.sectionPos+", children #: "+this.getChildCount()+", value: "+this.value; }
}
@@ -153,6 +153,26 @@ public class QuadTree<T>
}
public int getNonNullChildCountAtPos(DhSectionPos pos) { return this.getChildCountAtPos(pos, false); }
public int getChildCountAtPos(DhSectionPos pos, boolean includeNullValues)
{
int childCount = 0;
for (int i = 0; i < 4; i++)
{
DhSectionPos childPos = pos.getChildByIndex(i);
if (this.isSectionPosInBounds(childPos))
{
T value = this.get(childPos);
if (includeNullValues || value != null)
{
childCount++;
}
}
}
return childCount;
}
/** no nulls TODO comment/rename */
public void forEachRootNode(Consumer<QuadNode<T>> consumer)
@@ -254,4 +274,7 @@ public class QuadTree<T>
// return sb.toString();
// }
@Override
public String toString() { return "center block: "+this.centerBlockPos+", block width: "+this.widthInBlocks+", detail level range: ["+this.treeMinDetailLevel+"-"+this.treeMaxDetailLevel+"], leaf #: "+this.leafNodeCount(); }
}
+13 -1
View File
@@ -529,7 +529,7 @@ public class QuadTreeTest
QuadNode<Integer> rootNode = new QuadNode<>(new DhSectionPos((byte)10, 0, 0), (byte)0);
rootNode.forEachDirectChild((child, childPos) ->
{
rootNode.setValue(childPos, 1, null);
rootNode.setValue(childPos, 1);
});
Assert.assertEquals("node not filled", rootNode.getChildValueCount(), 4);
@@ -543,6 +543,18 @@ public class QuadTreeTest
}
// this is here for quickly testing the toString method, it should never fail
@Test
public void toStringTest()
{
QuadTree<Integer> tree = new QuadTree<>(ROOT_NODE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0), (byte)6);
String treeString = tree.toString();
Assert.assertNotNull(treeString);
Assert.assertNotEquals("", treeString);
}