historyBounds.total

This commit is contained in:
mischa 2023-07-24 20:35:55 +08:00
parent fea626943a
commit b46a5e9531
2 changed files with 27 additions and 22 deletions

View File

@ -15,6 +15,8 @@ public class HistoryBounds
public readonly int limit;
public Bounds total;
public HistoryBounds(int limit)
{
// initialize queue with maximum capacity to avoid runtime resizing
@ -22,9 +24,9 @@ public HistoryBounds(int limit)
history = new Queue<Bounds>(limit);
}
// insert new bounds into history. returns new total bounds.
// insert new bounds into history. calculates new total bounds.
// Queue.Dequeue() always has the oldest bounds.
public Bounds Insert(Bounds bounds)
public void Insert(Bounds bounds)
{
// remove oldest if limit reached
if (history.Count >= limit)
@ -37,16 +39,15 @@ public Bounds Insert(Bounds bounds)
// starting at latest bounds, not at 'new Bounds' because that would
// encapsulate (0,0) too.
// TODO make this not be O(N)
Bounds total = bounds;
total = bounds;
foreach (Bounds b in history)
total.Encapsulate(b);
return total;
}
public void Reset()
{
history.Clear();
total = new Bounds();
}
}
}

View File

@ -44,7 +44,11 @@ public void Benchmark(int iterations, int insertions, int limit)
float min = Random.Range(-1, 1);
float max = Random.Range(min, 1);
Bounds bounds = MinMax(min, max);
Bounds total = history.Insert(bounds);
history.Insert(bounds);
// always call .total to include any getter calculations in
// the benchmark here.
Bounds total = history.total;
}
}
}
@ -58,29 +62,29 @@ public void Insert_Basic()
// insert initial [-1, 1].
// should calculate new bounds == initial.
Bounds total = history.Insert(MinMax(-1, 1));
history.Insert(MinMax(-1, 1));
Assert.That(history.Count, Is.EqualTo(1));
Assert.That(total, Is.EqualTo(MinMax(-1, 1)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 1)));
// insert [0, 2]
// should calculate new bounds == [-1, 2].
total = history.Insert(MinMax(0, 2));
history.Insert(MinMax(0, 2));
Assert.That(history.Count, Is.EqualTo(2));
Assert.That(total, Is.EqualTo(MinMax(-1, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 2)));
// insert one that's smaller than current bounds [-.5, 0]
// history needs to contain it even if smaller, because once the oldest
// largest one gets removed, this one matters too.
total = history.Insert(MinMax(-0.5f, 0));
history.Insert(MinMax(-0.5f, 0));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-1, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 2)));
// insert more than 'limit': [0, 0]
// the oldest one [-1, 1] should be discarded.
// new bounds should be [-0.5, 2]
total = history.Insert(MinMax(0, 0));
history.Insert(MinMax(0, 0));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-0.5f, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-0.5f, 2)));
}
// player runs in a circles and visits the same areas again.
@ -93,27 +97,27 @@ public void Insert_Revisit()
// insert initial [-1, 1].
// should calculate new bounds == initial.
Bounds total = history.Insert(MinMax(-1, 1));
history.Insert(MinMax(-1, 1));
Assert.That(history.Count, Is.EqualTo(1));
Assert.That(total, Is.EqualTo(MinMax(-1, 1)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 1)));
// insert [0, 2]
// should calculate new bounds == [-1, 2].
total = history.Insert(MinMax(0, 2));
history.Insert(MinMax(0, 2));
Assert.That(history.Count, Is.EqualTo(2));
Assert.That(total, Is.EqualTo(MinMax(-1, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 2)));
// visit [-1, 1] again
total = history.Insert(MinMax(-1, 1));
history.Insert(MinMax(-1, 1));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-1, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 2)));
// insert beyond limit.
// oldest one [-1, 1] should be removed.
// total should still include it because we revisited [1, 1].
total = history.Insert(MinMax(0, 0));
history.Insert(MinMax(0, 0));
Assert.That(history.Count, Is.EqualTo(3));
Assert.That(total, Is.EqualTo(MinMax(-1, 2)));
Assert.That(history.total, Is.EqualTo(MinMax(-1, 2)));
}
[Test]