Replace consumer based QuadTree iteration with Iterators

This drastically improves debugging, because lambda consumers cause the stack trace to become messy very quickly.
This commit is contained in:
James Seibel
2023-04-02 19:39:47 -05:00
parent 45b410806b
commit d2a0057919
5 changed files with 567 additions and 196 deletions
@@ -5,7 +5,7 @@ import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.LodUtil;
import org.apache.logging.log4j.Logger;
import java.util.function.BiConsumer;
import java.util.Iterator;
import java.util.function.Consumer;
public class QuadNode<T>
@@ -45,8 +45,6 @@ public class QuadNode<T>
public QuadNode(DhSectionPos sectionPos, byte minimumDetailLevel)
{
this.sectionPos = sectionPos;
@@ -54,6 +52,7 @@ public class QuadNode<T>
}
/** @return the number of non-null child nodes */
public int getChildCount()
{
@@ -245,63 +244,22 @@ public class QuadNode<T>
// TODO comment section for iterators
// TODO make naming consistent with QuadTree
// TODO replace consumers with returned iterators, using consumers like this makes debugging painful because the stack traces become messy very quickly
//===========//
// iterators //
//===========//
/**
* Applies the given consumer to all 4 of this nodes' children. <br>
* Note: this will pass in null children.
*/
public void forEachDirectChildNode(BiConsumer<QuadNode<T>, DhSectionPos> callback)
{
if (this.sectionPos.sectionDetailLevel != this.minimumDetailLevel)
{
for (int i = 0; i < 4; i++)
{
DhSectionPos childPos = this.sectionPos.getChildByIndex(i);
QuadNode<T> childNode = this.getChildByIndex(i);
callback.accept(childNode, childPos);
}
}
}
public Iterator<QuadNode<T>> getNodeIterator() { return new QuadNodeIterator<>(this, false, this.minimumDetailLevel); }
public Iterator<QuadNode<T>> getLeafNodeIterator() { return new QuadNodeIterator<>(this, true, this.minimumDetailLevel); }
/** TODO comment */
public void forAllLeafValues(Consumer<? super T> consumer) { this.forAllLeafValues((value, sectionPos) -> { consumer.accept(value); }); }
/**
* Applies the given consumer to all leaf nodes below this node. <br>
* Note: this will pass in null values.
*/
public void forAllLeafValues(BiConsumer<? super T, DhSectionPos> consumer) { this.forAllChildValues(consumer, true, true); }
public Iterator<QuadNode<T>> getDirectChildNodeIterator() { return new QuadNodeIterator<>(this, false, this.sectionPos.sectionDetailLevel); }
public Iterator<DhSectionPos> getDirectChildPosIterator() { return new QuadNodeDirectChildPosIterator<>(this, true); }
public void forAllChildValues(BiConsumer<? super T, DhSectionPos> consumer) { this.forAllChildValues(consumer, false, true); }
private void forAllChildValues(BiConsumer<? super T, DhSectionPos> consumer, boolean onlyReturnLeafNodes, boolean topCaller) // TODO rename/refactor topCaller parameter
{
if (this.getChildCount() == 0 || this.sectionPos.sectionDetailLevel == this.minimumDetailLevel)
{
// base case, bottom leaf node found
consumer.accept(this.value, this.sectionPos);
}
else
{
if (!onlyReturnLeafNodes && !topCaller)
{
consumer.accept(this.value, this.sectionPos);
}
for (int i = 0; i < 4; i++)
{
QuadNode<T> childNode = this.getChildByIndex(i);
if (childNode != null)
{
// TODO should this pass in a null value if the child node is null?
childNode.forAllChildValues(consumer, onlyReturnLeafNodes, false);
}
}
}
}
//==========//
// deletion //
//==========//
public void deleteAllChildren() { this.deleteAllChildren(null); }
/** @param removedItemConsumer is only fired for non-null nodes, however the value passed in may be null */
public void deleteAllChildren(Consumer<? super T> removedItemConsumer)
@@ -342,6 +300,11 @@ public class QuadNode<T>
}
//==============//
// base methods //
//==============//
@Override
public String toString() { return "pos: "+this.sectionPos+", children #: "+this.getChildCount()+", value: "+this.value; }
@@ -0,0 +1,56 @@
package com.seibel.lod.core.util.objects.quadTree;
import com.seibel.lod.core.pos.DhSectionPos;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.function.Consumer;
public class QuadNodeDirectChildPosIterator<T> implements Iterator<DhSectionPos>
{
private final Queue<DhSectionPos> iteratorPosQueue = new LinkedList<>();
public QuadNodeDirectChildPosIterator(QuadNode<T> parentNode, boolean returnNullChildPos)
{
// go over each child pos
for (int i = 0; i < 4; i++)
{
// add pos to queue if either not null or we want to return null values as well
if (returnNullChildPos || parentNode.getChildByIndex(i) != null)
{
this.iteratorPosQueue.add(parentNode.sectionPos.getChildByIndex(i));
}
}
}
@Override
public boolean hasNext() { return this.iteratorPosQueue.size() != 0; }
@Override
public DhSectionPos next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
DhSectionPos iteratorPos = this.iteratorPosQueue.poll();
return iteratorPos;
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super DhSectionPos> action) { Iterator.super.forEachRemaining(action); }
}
@@ -0,0 +1,136 @@
package com.seibel.lod.core.util.objects.quadTree;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.function.Consumer;
class QuadNodeIterator<T> implements Iterator<QuadNode<T>>
{
/** lowest numerical value, inclusive */
private final byte highestDetailLevel;
private final Queue<QuadNode<T>> validNodesForDetailLevel = new LinkedList<>();
private final Queue<QuadNode<T>> iteratorNodeQueue = new LinkedList<>();
private byte iteratorDetailLevel = 0;
private final boolean onlyReturnLeafValues;
public QuadNodeIterator(QuadNode<T> rootNode, boolean onlyReturnLeafValues, byte highestDetailLevel)
{
this.onlyReturnLeafValues = onlyReturnLeafValues;
// TODO the naming conversion for these are flipped in a lot of places
this.highestDetailLevel = highestDetailLevel;
this.iteratorDetailLevel = rootNode.sectionPos.sectionDetailLevel;
if (!this.onlyReturnLeafValues)
{
// set the start for the iterator
this.validNodesForDetailLevel.add(rootNode);
this.iteratorNodeQueue.add(rootNode);
}
else
{
// fully populate the iterator
// this isn't the best way to do this, especially for large trees,
// but it is simple and functions well enough for now
Queue<QuadNode<T>> parentNodeQueue = new LinkedList<>();
parentNodeQueue.add(rootNode);
// walk through the whole tree and add each leaf node to the iterator queue
while (parentNodeQueue.peek() != null)
{
QuadNode<T> parentNode = parentNodeQueue.poll();
for (int i = 0; i < 4; i++)
{
QuadNode<T> childNode = parentNode.getChildByIndex(i);
if (childNode != null)
{
if (childNode.getChildCount() == 0)
{
this.iteratorNodeQueue.add(childNode);
}
else
{
parentNodeQueue.add(childNode);
}
}
}
}
}
}// constructor
@Override
public boolean hasNext()
{
return this.iteratorNodeQueue.size() != 0;
}
@Override
public QuadNode<T> next()
{
if (this.iteratorDetailLevel < this.highestDetailLevel)
{
throw new NoSuchElementException("Highest detail level reached [" + this.highestDetailLevel + "].");
}
if (this.iteratorNodeQueue.size() == 0)
{
throw new NoSuchElementException();
}
// get the current iterator node
QuadNode<T> currentNode = this.iteratorNodeQueue.poll();
// the iterator queue should be fully populated for leaf values,
// for all values, it needs to be populated for each detail level
if (this.iteratorNodeQueue.size() == 0 && !onlyReturnLeafValues)
{
// populate the next detail level list
this.iteratorDetailLevel--;
// only continue if we can go down farther
if (this.iteratorDetailLevel >= this.highestDetailLevel)
{
Queue<QuadNode<T>> parentNodes = new LinkedList<>(this.validNodesForDetailLevel);
this.validNodesForDetailLevel.clear();
for (QuadNode<T> parentNode : parentNodes)
{
for (int i = 0; i < 4; i++)
{
QuadNode<T> childNode = parentNode.getChildByIndex(i);
if (childNode != null)
{
this.iteratorNodeQueue.add(childNode);
this.validNodesForDetailLevel.add(childNode);
}
}
}
}
}
return currentNode;
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super QuadNode<T>> action) { Iterator.super.forEachRemaining(action); }
}
@@ -12,8 +12,10 @@ import com.seibel.lod.core.util.MathUtil;
import com.seibel.lod.core.util.gridList.MovableGridRingList;
import org.apache.logging.log4j.Logger;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.function.Consumer;
/**
@@ -127,7 +129,7 @@ public class QuadTree<T>
}
// check if the testPos is within the X,Z boundry of the tree
// check if the testPos is within the X,Z boundary of the tree
DhBlockPos2D blockCornerOfTree = this.centerBlockPos.add(new DhBlockPos2D(-this.widthInBlocks/2,-this.widthInBlocks/2));
DhLodPos cornerOfTreePos = new DhLodPos((byte)0, blockCornerOfTree.x, blockCornerOfTree.z);
@@ -177,54 +179,15 @@ public class QuadTree<T>
//===========//
// iterators //
//===========//
/** no nulls TODO comment/rename */
public void forEachRootNode(Consumer<QuadNode<T>> consumer)
{
this.topRingList.forEachOrdered((rootNode) ->
{
if (rootNode != null)
{
consumer.accept(rootNode);
}
});
}
/** root nodes can be null */
public void forEachRootNodePos(BiConsumer<QuadNode<T>, DhSectionPos> consumer)
{
this.topRingList.forEachPosOrdered((rootNode, pos2D) ->
{
DhSectionPos rootPos = new DhSectionPos(this.treeMaxDetailLevel, pos2D.x, pos2D.y);
if (isSectionPosInBounds(rootPos))
{
consumer.accept(rootNode, rootPos);
}
});
}
public void forEachLeafValue(Consumer<? super T> consumer) { this.forEachLeafValue((value, sectionPos) -> { consumer.accept(value); }); }
public void forEachLeafValue(BiConsumer<? super T, DhSectionPos> consumer)
{
this.forEachRootNode((rootNode) ->
{
rootNode.forAllLeafValues(consumer);
});
}
public void forEachValue(Consumer<? super T> consumer) { this.forEachValue((value, sectionPos) -> { consumer.accept(value); }); }
public void forEachValue(BiConsumer<? super T, DhSectionPos> consumer)
{
this.forEachRootNode((rootNode) ->
{
rootNode.forAllChildValues(consumer);
});
}
public Iterator<QuadNode<T>> rootNodeIterator() { return new QuadTreeRootNodeIterator(); }
public Iterator<DhSectionPos> rootNodePosIterator() { return new QuadTreeRootPosIterator(true); }
public Iterator<QuadNode<T>> nodeIterator() { return new QuadTreeNodeIterator(false); }
public Iterator<QuadNode<T>> leafNodeIterator() { return new QuadTreeNodeIterator(true); }
@@ -267,13 +230,21 @@ public class QuadTree<T>
public int leafNodeCount()
{
AtomicInteger count = new AtomicInteger(0);
this.topRingList.forEachOrdered((node) ->
int count = 0;
for (QuadNode<T> node : this.topRingList)
{
node.forAllLeafValues((value) -> { count.addAndGet(1); });
});
return count.get();
if (node != null)
{
Iterator<QuadNode<T>> leafNodeIterator = node.getLeafNodeIterator();
while (leafNodeIterator.hasNext())
{
leafNodeIterator.next();
count++;
}
}
}
return count;
}
// TODO comment, currently a tree will always have 9 root nodes, because the tree will grow all the way up to the top, if this is ever changed then these values must also change
@@ -297,4 +268,131 @@ public class QuadTree<T>
@Override
public String toString() { return "center block: "+this.centerBlockPos+", block width: "+this.widthInBlocks+", detail level range: ["+this.treeMinDetailLevel+"-"+this.treeMaxDetailLevel+"], leaf #: "+this.leafNodeCount(); }
//==================//
// iterator classes //
//==================//
private class QuadTreeRootPosIterator implements Iterator<DhSectionPos>
{
private final Queue<DhSectionPos> iteratorPosQueue = new LinkedList<>();
public QuadTreeRootPosIterator(boolean includeNullNodes)
{
QuadTree.this.topRingList.forEachPosOrdered((node, pos2D) ->
{
if (node != null || includeNullNodes)
{
DhSectionPos rootPos = new DhSectionPos(QuadTree.this.treeMaxDetailLevel, pos2D.x, pos2D.y);
if (QuadTree.this.isSectionPosInBounds(rootPos))
{
iteratorPosQueue.add(rootPos);
}
}
});
}// constructor
@Override
public boolean hasNext() { return this.iteratorPosQueue.size() != 0; }
@Override
public DhSectionPos next()
{
if (this.iteratorPosQueue.size() == 0)
{
throw new NoSuchElementException();
}
return this.iteratorPosQueue.poll();
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super DhSectionPos> action) { Iterator.super.forEachRemaining(action); }
}
private class QuadTreeRootNodeIterator implements Iterator<QuadNode<T>>
{
private final QuadTreeRootPosIterator rootPosIterator;
public QuadTreeRootNodeIterator()
{
this.rootPosIterator = new QuadTreeRootPosIterator(false);
}
@Override
public boolean hasNext() { return this.rootPosIterator.hasNext(); }
@Override
public QuadNode<T> next()
{
DhSectionPos pos = this.rootPosIterator.next();
return QuadTree.this.topRingList.get(pos.sectionX, pos.sectionZ);
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super QuadNode<T>> action) { Iterator.super.forEachRemaining(action); }
}
private class QuadTreeNodeIterator implements Iterator<QuadNode<T>>
{
private final QuadTreeRootNodeIterator rootNodeIterator;
private Iterator<QuadNode<T>> currentNodeIterator;
private final boolean onlyReturnLeaves;
public QuadTreeNodeIterator(boolean onlyReturnLeaves)
{
this.rootNodeIterator = new QuadTreeRootNodeIterator();
this.onlyReturnLeaves = onlyReturnLeaves;
}
@Override
public boolean hasNext() { return this.rootNodeIterator.hasNext() || this.currentNodeIterator.hasNext(); }
@Override
public QuadNode<T> next()
{
if (this.currentNodeIterator == null)
{
QuadNode<T> rootNode = this.rootNodeIterator.next();
this.currentNodeIterator = this.onlyReturnLeaves ? rootNode.getLeafNodeIterator() : rootNode.getNodeIterator();
}
return this.currentNodeIterator.next();
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super QuadNode<T>> action) { Iterator.super.forEachRemaining(action); }
}
}
+207 -89
View File
@@ -33,7 +33,8 @@ import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
public class QuadTreeTest
@@ -272,23 +273,112 @@ public class QuadTreeTest
testSet(tree, new DhSectionPos((byte)9, 1, 1), 6);
final AtomicInteger rootNodeCount = new AtomicInteger(0);
final AtomicInteger leafCount = new AtomicInteger(0);
final AtomicInteger leafValueSum = new AtomicInteger(0);
tree.forEachRootNode((rootNode) ->
{
rootNodeCount.addAndGet(1);
rootNode.forAllLeafValues((leafValue) ->
{
leafCount.addAndGet(1);
leafValueSum.addAndGet(leafValue);
});
});
Assert.assertEquals("incorrect root count", 1, rootNodeCount.get());
Assert.assertEquals("incorrect leaf count", 5, leafCount.get());
Assert.assertEquals("incorrect leaf value sum", 20, leafValueSum.get());
// root nodes
int rootNodeCount = 0;
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
QuadNode<Integer> rootNode = rootNodeIterator.next();
rootNodeCount++;
}
Assert.assertEquals("incorrect root count", 1, rootNodeCount);
// leaf nodes
int leafCount = 0;
int leafValueSum = 0;
Iterator<QuadNode<Integer>> leafNodeIterator = tree.leafNodeIterator();
while (leafNodeIterator.hasNext())
{
QuadNode<Integer> leafNode = leafNodeIterator.next();
leafCount++;
leafValueSum += leafNode.value;
}
Assert.assertEquals("incorrect leaf count", 5, leafCount);
Assert.assertEquals("incorrect leaf value sum", 20, leafValueSum);
}
@Test
public void NewQuadTreeIterationTest()
{
AbstractTestTreeParams treeParams = new LargeTestTree();
QuadNode<Integer> rootNode = new QuadNode<>(new DhSectionPos((byte)10, 0, 0), LodUtil.BLOCK_DETAIL_LEVEL);
rootNode.setValue(new DhSectionPos((byte)10, 0, 0), 0);
rootNode.setValue(new DhSectionPos((byte)9, 0, 0), 1);
rootNode.setValue(new DhSectionPos((byte)9, 1, 0), 1);
rootNode.setValue(new DhSectionPos((byte)9, 0, 1), 1);
rootNode.setValue(new DhSectionPos((byte)9, 1, 1), null);
rootNode.setValue(new DhSectionPos((byte)8, 0, 0), 2);
rootNode.setValue(new DhSectionPos((byte)8, 1, 0), 2);
rootNode.setValue(new DhSectionPos((byte)8, 0, 1), 2);
rootNode.setValue(new DhSectionPos((byte)8, 1, 1), null);
// all node iterator //
Iterator<QuadNode<Integer>> iterator = rootNode.getNodeIterator();
HashSet<QuadNode<Integer>> iteratedNodes = new HashSet<>();
int populatedValueCount = 0;
int totalNodeCount = 0;
while (iterator.hasNext())
{
QuadNode<Integer> node = iterator.next();
if (node.value != null)
{
populatedValueCount++;
}
if (!iteratedNodes.add(node))
{
Assert.fail("Iterator passed over the same node multiple times. Section Pos: "+node.sectionPos);
}
totalNodeCount++;
}
Assert.assertEquals("incorrect populated node count", 7, populatedValueCount);
Assert.assertEquals("incorrect total node count", 9, totalNodeCount);
// leaf node iterator //
Iterator<QuadNode<Integer>> leafIterator = rootNode.getLeafNodeIterator();
HashSet<QuadNode<Integer>> iteratedLeafNodes = new HashSet<>();
int populatedLeafCount = 0;
int totalLeafCount = 0;
while (leafIterator.hasNext())
{
QuadNode<Integer> node = leafIterator.next();
if (node.value != null)
{
populatedLeafCount++;
}
if (!iteratedLeafNodes.add(node))
{
Assert.fail("Iterator passed over the same node multiple times. Section Pos: "+node.sectionPos);
}
totalLeafCount++;
}
Assert.assertEquals("incorrect populated leaf count", 5, populatedLeafCount);
Assert.assertEquals("incorrect total leaf count", 7, totalLeafCount);
}
@@ -300,20 +390,28 @@ public class QuadTreeTest
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 0), 0);
// confirm the root node were added
final AtomicInteger rootNodeCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) -> { rootNodeCount.addAndGet(1); });
Assert.assertEquals("incorrect root count", 1, rootNodeCount.get());
int rootNodeCount = 0;
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
rootNodeIterator.next();
rootNodeCount++;
}
Assert.assertEquals("incorrect root count", 1, rootNodeCount);
// attempt to get and remove, each node in the tree
final AtomicInteger rootNodePosCount = new AtomicInteger(0);
tree.forEachRootNodePos((renderBufferNode, sectionPos) ->
int rootNodePosCount = 0;
Iterator<DhSectionPos> rootNodePosIterator = tree.rootNodePosIterator();
while (rootNodePosIterator.hasNext())
{
testGet(tree, sectionPos, 0);
testSet(tree, sectionPos, null);
DhSectionPos rootNodePos = rootNodePosIterator.next();
rootNodePosCount.addAndGet(1);
});
Assert.assertEquals("incorrect root count", 1, rootNodeCount.get());
testGet(tree, rootNodePos, 0);
testSet(tree, rootNodePos, null);
rootNodePosCount++;
}
Assert.assertEquals("incorrect root count", 1, rootNodePosCount);
}
@@ -340,18 +438,22 @@ public class QuadTreeTest
tree.setCenterBlockPos(treeMovePos);
Assert.assertEquals("tree move failed, incorrect position after move", treeMovePos, tree.getCenterBlockPos());
tree.forEachRootNodePos((rootNode, sectionPos) ->
Iterator<DhSectionPos> rootNodePosIterator = tree.rootNodePosIterator();
while (rootNodePosIterator.hasNext())
{
DhSectionPos sectionPos = rootNodePosIterator.next();
testSet(tree, sectionPos, 0);
});
}
// 4 root nodes should be added
final AtomicInteger rootNodeCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) ->
{
rootNodeCount.addAndGet(1);
});
Assert.assertEquals("incorrect root count", expectedRootNodeCount, rootNodeCount.get());
int rootNodeCount = 0;
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
QuadNode<Integer> rootNode = rootNodeIterator.next();
rootNodeCount++;
}
Assert.assertEquals("incorrect root count", expectedRootNodeCount, rootNodeCount);
}
@Test
@@ -369,12 +471,14 @@ public class QuadTreeTest
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, -1), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 1), -1, IndexOutOfBoundsException.class);
AtomicInteger rootCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) ->
int rootNodeCount = 0;
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
rootCount.getAndAdd(1);
});
Assert.assertEquals("incorrect leaf value sum", 1, rootCount.get());
QuadNode<Integer> rootNode = rootNodeIterator.next();
rootNodeCount++;
}
Assert.assertEquals("incorrect leaf value sum", 1, rootNodeCount);
}
@@ -403,12 +507,14 @@ public class QuadTreeTest
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, -1), -1, IndexOutOfBoundsException.class);
AtomicInteger rootCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) ->
int rootNodeCount = 0;
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
rootCount.getAndAdd(1);
});
Assert.assertEquals("incorrect leaf value sum", 4, rootCount.get());
QuadNode<Integer> rootNode = rootNodeIterator.next();
rootNodeCount++;
}
Assert.assertEquals("incorrect leaf value sum", 4, rootNodeCount);
}
@@ -434,55 +540,65 @@ public class QuadTreeTest
}
@Test
public void QuadNodeDetailLimitTest()
{
AbstractTestTreeParams treeParams = new MediumTestTree();
QuadTree<Integer> tree = new QuadTree<>(treeParams.getWidthInBlocks(), new DhBlockPos2D(0, 0), (byte)6);
Assert.assertEquals("Test detail level's need to be adjusted. This isn't necessarily a failed test.", 10, tree.treeMaxDetailLevel);
// create the root node
testSet(tree, new DhSectionPos((byte)10, 0, 0), 1);
// recurse down the tree
AtomicInteger minimumDetailLevelReachedRef = new AtomicInteger(tree.treeMaxDetailLevel);
tree.forEachRootNode((rootNode) ->
{
rootNode.forEachDirectChildNode((quadNode, sectionPos) ->
{
// all sections will be null
rootNode.setValue(sectionPos, 0);
});
rootNode.forEachDirectChildNode((quadNode, sectionPos) ->
{
recursivelyCreateNodeChildren(quadNode, tree.treeMinDetailLevel, minimumDetailLevelReachedRef);
});
});
// confirm that the tree can and did iterate all the way down to the minimum detail level
Assert.assertEquals("Minimum detail level never reached", minimumDetailLevelReachedRef.get(), tree.treeMinDetailLevel);
}
// @Test
// public void QuadNodeDetailLimitTest()
// {
// AbstractTestTreeParams treeParams = new MediumTestTree();
// QuadTree<Integer> tree = new QuadTree<>(treeParams.getWidthInBlocks(), new DhBlockPos2D(0, 0), (byte)6);
// Assert.assertEquals("Test detail level's need to be adjusted. This isn't necessarily a failed test.", 10, tree.treeMaxDetailLevel);
//
// // create the root node
// testSet(tree, new DhSectionPos((byte)10, 0, 0), 1);
//
// // recurse down the tree
// AtomicInteger minimumDetailLevelReachedRef = new AtomicInteger(tree.treeMaxDetailLevel);
// tree.forEachRootNode((rootNode) ->
// {
// rootNode.forEachDirectChildNode((quadNode, sectionPos) ->
// {
// // all sections will be null
// rootNode.setValue(sectionPos, 0);
// });
//
// rootNode.forEachDirectChildNode((quadNode, sectionPos) ->
// {
// recursivelyCreateNodeChildren(quadNode, tree.treeMinDetailLevel, minimumDetailLevelReachedRef);
// });
// });
//
// // confirm that the tree can and did iterate all the way down to the minimum detail level
// Assert.assertEquals("Minimum detail level never reached", minimumDetailLevelReachedRef.get(), tree.treeMinDetailLevel);
// }
private void recursivelyCreateNodeChildren(QuadNode<Integer> node, byte minDetailLevel, AtomicInteger minimumDetailLevelReachedRef)
{
AtomicBoolean childNodesCreatedRef = new AtomicBoolean(false);
AtomicBoolean childNodesIteratedRef = new AtomicBoolean(false);
boolean childNodesCreated = false;
boolean childNodesIterated = false;
Iterator<QuadNode<Integer>> directChildIterator;
// fill in the null children
node.forEachDirectChildNode((childNode, childSectionPos) ->
directChildIterator = node.getDirectChildNodeIterator();
while (directChildIterator.hasNext())
{
node.setValue(childSectionPos, 0);
childNodesCreatedRef.set(true);
});
QuadNode<Integer> childNode = directChildIterator.next();
node.setValue(childNode.sectionPos, 0);
childNodesCreated = true;
}
// attempt to recurse down these new children
node.forEachDirectChildNode((childNode, childSectionPos) ->
directChildIterator = node.getDirectChildNodeIterator();
while (directChildIterator.hasNext())
{
Assert.assertTrue("Child node recurred too low. Min detail level: "+minDetailLevel+", node detail level: "+childSectionPos.sectionDetailLevel, childSectionPos.sectionDetailLevel >= minDetailLevel);
QuadNode<Integer> childNode = directChildIterator.next();
Assert.assertTrue("Child node recurred too low. Min detail level: "+minDetailLevel+", node detail level: "+childNode.sectionPos.sectionDetailLevel, childNode.sectionPos.sectionDetailLevel >= minDetailLevel);
recursivelyCreateNodeChildren(childNode, minDetailLevel, minimumDetailLevelReachedRef);
childNodesIteratedRef.set(true);
});
childNodesIterated = true;
}
// keep track of how far down the tree we have gone
@@ -492,12 +608,13 @@ public class QuadTreeTest
}
// assertions
if (childNodesCreatedRef.get())
if (childNodesCreated)
{
Assert.assertTrue("node children created below minimum detail level", node.sectionPos.sectionDetailLevel >= minDetailLevel);
}
if (childNodesIteratedRef.get())
if (childNodesIterated)
{
Assert.assertTrue("node children iterated below minimum detail level", node.sectionPos.sectionDetailLevel-1 >= minDetailLevel);
}
@@ -507,10 +624,11 @@ public class QuadTreeTest
public void quadNodeChildPositionIndexTest()
{
QuadNode<Integer> rootNode = new QuadNode<>(new DhSectionPos((byte)10, 0, 0), (byte)0);
rootNode.forEachDirectChildNode((child, childPos) ->
Iterator<DhSectionPos> directChildIterator = rootNode.getDirectChildPosIterator();
while (directChildIterator.hasNext())
{
rootNode.setValue(childPos, 1);
});
rootNode.setValue(directChildIterator.next(), 1);
}
Assert.assertEquals("node not filled", rootNode.getChildValueCount(), 4);