Fix QuadNode.getDirectChildNodeIterator()

This commit is contained in:
James Seibel
2023-04-02 21:31:17 -05:00
parent d2a0057919
commit 90295b7dcf
6 changed files with 161 additions and 64 deletions
@@ -248,11 +248,11 @@ public class QuadNode<T>
// iterators //
//===========//
public Iterator<QuadNode<T>> getNodeIterator() { return new QuadNodeIterator<>(this, false, this.minimumDetailLevel); }
public Iterator<QuadNode<T>> getLeafNodeIterator() { return new QuadNodeIterator<>(this, true, this.minimumDetailLevel); }
public Iterator<QuadNode<T>> getNodeIterator() { return new QuadNodeIterator<>(this, false); }
public Iterator<QuadNode<T>> getLeafNodeIterator() { return new QuadNodeIterator<>(this, true); }
public Iterator<QuadNode<T>> getDirectChildNodeIterator() { return new QuadNodeIterator<>(this, false, this.sectionPos.sectionDetailLevel); }
public Iterator<DhSectionPos> getDirectChildPosIterator() { return new QuadNodeDirectChildPosIterator<>(this, true); }
public Iterator<QuadNode<T>> getDirectChildNodeIterator() { return new QuadNodeDirectChildNodeIterator<>(this); }
public Iterator<DhSectionPos> getDirectChildPosIterator() { return new QuadNodeDirectChildPosIterator<>(this); }
@@ -0,0 +1,57 @@
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;
public class QuadNodeChildIndexIterator<T> implements Iterator<Integer>
{
private final Queue<Integer> iteratorQueue = new LinkedList<>();
public QuadNodeChildIndexIterator(QuadNode<T> parentNode, boolean returnNullChildPos)
{
// only get the children if this section isn't at the bottom of the tree
if (parentNode.sectionPos.sectionDetailLevel > parentNode.minimumDetailLevel)
{
// go over each child pos
for (int i = 0; i < 4; i++)
{
// add index to queue if either not null or we want to return null values as well
if (returnNullChildPos || parentNode.getChildByIndex(i) != null)
{
this.iteratorQueue.add(i);
}
}
}
}
@Override
public boolean hasNext() { return this.iteratorQueue.size() != 0; }
@Override
public Integer next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
return this.iteratorQueue.poll();
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super Integer> action) { Iterator.super.forEachRemaining(action); }
}
@@ -0,0 +1,45 @@
package com.seibel.lod.core.util.objects.quadTree;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
public class QuadNodeDirectChildNodeIterator<T> implements Iterator<QuadNode<T>>
{
private final QuadNodeChildIndexIterator<T> childIndexIterator;
private final QuadNode<T> parentNode;
public QuadNodeDirectChildNodeIterator(QuadNode<T> parentNode)
{
this.parentNode = parentNode;
this.childIndexIterator = new QuadNodeChildIndexIterator<>(this.parentNode, false);
}
@Override
public boolean hasNext() { return this.childIndexIterator.hasNext(); }
@Override
public QuadNode<T> next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
int childIndex = this.childIndexIterator.next();
return this.parentNode.getChildByIndex(childIndex);
}
/** Unimplemented */
@Override
public void remove() { throw new UnsupportedOperationException("remove"); }
@Override
public void forEachRemaining(Consumer<? super QuadNode<T>> action) { Iterator.super.forEachRemaining(action); }
}
@@ -3,34 +3,25 @@ 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<>();
private final QuadNodeChildIndexIterator<T> childIndexIterator;
private final QuadNode<T> parentNode;
public QuadNodeDirectChildPosIterator(QuadNode<T> parentNode, boolean returnNullChildPos)
public QuadNodeDirectChildPosIterator(QuadNode<T> parentNode)
{
// 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));
}
}
this.parentNode = parentNode;
this.childIndexIterator = new QuadNodeChildIndexIterator<>(this.parentNode, true);
}
@Override
public boolean hasNext() { return this.iteratorPosQueue.size() != 0; }
public boolean hasNext() { return this.childIndexIterator.hasNext(); }
@Override
public DhSectionPos next()
@@ -41,8 +32,8 @@ public class QuadNodeDirectChildPosIterator<T> implements Iterator<DhSectionPos>
}
DhSectionPos iteratorPos = this.iteratorPosQueue.poll();
return iteratorPos;
int childIndex = this.childIndexIterator.next();
return this.parentNode.sectionPos.getChildByIndex(childIndex);
}
@@ -20,11 +20,11 @@ class QuadNodeIterator<T> implements Iterator<QuadNode<T>>
public QuadNodeIterator(QuadNode<T> rootNode, boolean onlyReturnLeafValues, byte highestDetailLevel)
public QuadNodeIterator(QuadNode<T> rootNode, boolean onlyReturnLeafValues)
{
this.onlyReturnLeafValues = onlyReturnLeafValues;
// TODO the naming conversion for these are flipped in a lot of places
this.highestDetailLevel = highestDetailLevel;
this.highestDetailLevel = rootNode.minimumDetailLevel;
this.iteratorDetailLevel = rootNode.sectionPos.sectionDetailLevel;
@@ -72,10 +72,7 @@ class QuadNodeIterator<T> implements Iterator<QuadNode<T>>
@Override
public boolean hasNext()
{
return this.iteratorNodeQueue.size() != 0;
}
public boolean hasNext() { return this.iteratorNodeQueue.size() != 0; }
@Override
public QuadNode<T> next()
+44 -37
View File
@@ -540,56 +540,60 @@ 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);
Iterator<QuadNode<Integer>> rootNodeIterator = tree.rootNodeIterator();
while (rootNodeIterator.hasNext())
{
QuadNode<Integer> rootNode = rootNodeIterator.next();
Iterator<DhSectionPos> rootNodeDirectChildPosIterator = rootNode.getDirectChildPosIterator();
while (rootNodeDirectChildPosIterator.hasNext())
{
DhSectionPos sectionPos = rootNodeDirectChildPosIterator.next();
// all sections will be null
rootNode.setValue(sectionPos, 0);
}
Iterator<QuadNode<Integer>> rootNodeDirectChildIterator = rootNode.getDirectChildNodeIterator();
while (rootNodeDirectChildIterator.hasNext())
{
QuadNode<Integer> quadNode = rootNodeDirectChildIterator.next();
recursivelyCreateNodeChildren(quadNode, tree.treeMinDetailLevel, minimumDetailLevelReachedRef);
}
}
// confirm that the tree can and did iterate all the way down to the minimum detail level
Assert.assertEquals("Incorrect minimum detail level reached.", tree.treeMinDetailLevel, minimumDetailLevelReachedRef.get());
}
private void recursivelyCreateNodeChildren(QuadNode<Integer> node, byte minDetailLevel, AtomicInteger minimumDetailLevelReachedRef)
{
boolean childNodesCreated = false;
boolean childNodesIterated = false;
Iterator<QuadNode<Integer>> directChildIterator;
// fill in the null children
directChildIterator = node.getDirectChildNodeIterator();
while (directChildIterator.hasNext())
Iterator<DhSectionPos> directChildPosIterator = node.getDirectChildPosIterator();
while (directChildPosIterator.hasNext())
{
QuadNode<Integer> childNode = directChildIterator.next();
node.setValue(childNode.sectionPos, 0);
node.setValue(directChildPosIterator.next(), 0);
childNodesCreated = true;
}
// attempt to recurse down these new children
directChildIterator = node.getDirectChildNodeIterator();
Iterator<QuadNode<Integer>> directChildIterator = node.getDirectChildNodeIterator();
while (directChildIterator.hasNext())
{
QuadNode<Integer> childNode = directChildIterator.next();
@@ -627,7 +631,10 @@ public class QuadTreeTest
Iterator<DhSectionPos> directChildIterator = rootNode.getDirectChildPosIterator();
while (directChildIterator.hasNext())
{
rootNode.setValue(directChildIterator.next(), 1);
DhSectionPos sectionPos = directChildIterator.next();
Assert.assertNotEquals("Root node pos shouldn't be included in direct child pos iteration", sectionPos, rootNode.sectionPos);
rootNode.setValue(sectionPos, 1);
}
Assert.assertEquals("node not filled", rootNode.getChildValueCount(), 4);