Able
Askowl Base Library Enabler
CounterStack.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 
5 namespace Askowl {
6  /// <a href="http://bit.ly/2NU98YO">A stack of counters for garbage-free iterations</a> <inheritdoc />
7  // ReSharper disable once ClassNeverInstantiated.Global
8  public class CounterFifo : Fifo<int> {
9  /// <a href="http://bit.ly/2NU98YO">Retrieve a new (cached) instance</a>
10  public new static CounterFifo Instance => Cache<CounterFifo>.Instance;
11 
12  /// <a href="http://bit.ly/2NU98YO">Add a new counter and set it to the starting value</a>
13  public int Start(int startingValue = 0) => Math.Abs(Push(startingValue));
14 
15  /// <a href="http://bit.ly/2NU98YO">Increment he top counter</a>
16  public new int Next() => Math.Abs(++Top);
17 
18  /// <a href="http://bit.ly/2NU98YO">See if the top counter has reached the target value</a>
19  public bool Reached(int bounds) {
20  bool reached = Top >= bounds;
21  if (reached) Pop();
22  return reached;
23  }
24 
25  /// <inheritdoc />
26  public override void Dispose() => Cache<CounterFifo>.Dispose(this);
27  }
28 }
Definition: Clock.cs:3
Instance caching
Definition: Cache.cs:9
bool Reached(int bounds)
See if the top counter has reached the target value
Definition: CounterStack.cs:19
A stack of counters for garbage-free iterations
Definition: CounterStack.cs:8
virtual T Push(T entry)
Push a new entry onto the top of the stack
Definition: Fifo.cs:33
override void Dispose()
Send back to recycling
Intentionally simple stack implementation
Definition: Fifo.cs:8
new int Next()
Increment he top counter
int Start(int startingValue=0)
Add a new counter and set it to the starting value
T Top
Get/set the value at the top of the stack.
Definition: Fifo.cs:59
virtual T Pop()
Pop an entry from the top of the stack
static new CounterFifo Instance
Retrieve a new (cached) instance
Definition: CounterStack.cs:10