Change local stack to be member field for reuse, and add documentation for both private stacks

This commit is contained in:
Stefan Müller 2025-08-27 17:26:28 +02:00
parent 510ee1e705
commit 6523d469cc

View File

@ -47,8 +47,16 @@ namespace Quadtree
/// </summary> /// </summary>
private float m_maxObjectRadius; private float m_maxObjectRadius;
/// <summary>
/// Stack of vertex indices with their bounding boxes. It is reused for each query.
/// </summary>
private Stack<QuadtreeQuery> _queryStack; private Stack<QuadtreeQuery> _queryStack;
/// <summary>
/// Stack of vertex indices. It is reused for each query.
/// </summary>
private Stack<int> _vertexStack;
public Quadtree(int maxLeafSize, int maxTreeDepth) public Quadtree(int maxLeafSize, int maxTreeDepth)
{ {
m_maxLeafSize = maxLeafSize; m_maxLeafSize = maxLeafSize;
@ -58,6 +66,7 @@ namespace Quadtree
m_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero); m_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero);
m_maxObjectRadius = 0f; m_maxObjectRadius = 0f;
_queryStack = new Stack<QuadtreeQuery>(); _queryStack = new Stack<QuadtreeQuery>();
_vertexStack = new Stack<int>();
} }
public void Add(IWorldObject obj) public void Add(IWorldObject obj)
@ -347,21 +356,19 @@ namespace Quadtree
private void QueryAppendContainedItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList) private void QueryAppendContainedItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList)
{ {
// TODO: Stack as reused member variable? _vertexStack.Push(vertexIndex);
Stack<int> stack = new Stack<int>();
stack.Push(vertexIndex);
while (stack.Count > 0) while (_vertexStack.Count > 0)
{ {
int current = stack.Pop(); int current = _vertexStack.Pop();
if (m_vertices[current].ChildCount == -1) if (m_vertices[current].ChildCount == -1)
{ {
// Branches into all vertices until leaves are found. // Branches into all vertices until leaves are found.
int childIndex = m_vertices[current].FirstChildIndex; int childIndex = m_vertices[current].FirstChildIndex;
stack.Push(childIndex++); _vertexStack.Push(childIndex++);
stack.Push(childIndex++); _vertexStack.Push(childIndex++);
stack.Push(childIndex++); _vertexStack.Push(childIndex++);
stack.Push(childIndex); _vertexStack.Push(childIndex);
} }
else else
{ {