Able
Askowl Base Library Enabler
Fifo.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using System.Linq;
5 
6 namespace Askowl {
7  /// <a href="http://bit.ly/2NTA5Ml">Intentionally simple stack implementation</a>
8  public class Fifo<T> : IDisposable {
9  private T[] stack = new T[8];
10  private int pointer;
11 
12  // ReSharper disable once UnusedMember.Local
13  private void DeactivateItem() => pointer = 0;
14 
15  /// <a href="http://bit.ly/2NTA5Ml">Raw creation for subclasses that do their own caching</a>
16  // ReSharper disable once EmptyConstructor
17  public Fifo() { }
18 
19  /// <a href="http://bit.ly/2NTA5Ml">Fetch Fifo Stack from recycling</a>
20  public static Fifo<T> Instance => Cache<Fifo<T>>.Instance;
21 
22  /// <a href="http://bit.ly/2NTA5Ml">Array-like indexing into the stack</a>
23  public T this[int i] => stack[i];
24 
25  /// <a href="http://bit.ly/2NTA5Ml">Number of items on the stack</a>
26  public int Count { get => pointer; set => pointer = value < pointer ? value : pointer; }
27 
28  /// <a href=""></a> //#TBD#//
29  public bool Empty => Count == 0;
30 
31  /// <a href="http://bit.ly/2NTA5Ml">Push a new entry onto the top of the stack</a>
32  // ReSharper disable once VirtualMemberNeverOverridden.Global
33  public virtual T Push(T entry) {
34  if (pointer >= stack.Length) Array.Resize(ref stack, stack.Length * 2);
35  stack[pointer++] = entry;
36  return entry;
37  }
38 
39  /// <a href="http://bit.ly/2NTA5Ml">Pop an entry from the top of the stack</a>
40  // ReSharper disable once VirtualMemberNeverOverridden.Global
41 // public virtual T Pop() => pointer == 0 ? default : stack[--pointer];
42  public virtual T Pop() => pointer == 0 ? default : stack[--pointer];
43 
44  private static int nextId;
45  private readonly int id = ++nextId;
46 
47  /// <a href="http://bit.ly/2NTA5Ml">Swap the top and second entries on the stack</a>
48  public Fifo<T> Swap() {
49  if (pointer >= 2) {
50  T top = Top;
51  Top = Next;
52  Next = top;
53  }
54 
55  return this;
56  }
57 
58  /// <a href="http://bit.ly/2NTA5Ml">Get/set the value at the top of the stack.</a>
59  public T Top { get => Empty ? default : stack[pointer - 1]; set => stack[pointer - 1] = value; }
60 
61  /// <a href="http://bit.ly/2NTA5Ml">Get/set the value at second to top of the stack.</a>
62  public T Next { get => stack[pointer - 2]; set => stack[pointer - 2] = value; }
63 
64  /// <a href="http://bit.ly/2NTA5Ml">Get/set the value at the bottom of the stack.</a>
65  public T Bottom { get => Empty ? default : stack[0]; set => stack[0] = value; }
66 
67  /// <a href="http://bit.ly/2NTA5Ml">Send back to recycling</a><inheritdoc />
68  public virtual void Dispose() {
69  Clear();
70  Cache<Fifo<T>>.Dispose(this);
71  }
72 
73  /// <a href=""></a> //#TBD#//
74  public void Clear() {
75  for (var i = 0; i < pointer; i++) (stack[i] as IDisposable)?.Dispose();
76  pointer = 0;
77  }
78 
79  /// <a href=""></a> //#TBD#//
80  public T[] ToArray() {
81  Array.Resize(ref stack, pointer);
82  return stack;
83  }
84 
85  /// <a href=""></a> //#TBD#//
86  public override string ToString() => $"{typeof(T)}-{id}:{pointer}";
87  }
88 }
Definition: Clock.cs:3
Instance caching
Definition: Cache.cs:9
static Fifo< T > Instance
Fetch Fifo Stack from recycling
Definition: Fifo.cs:20
Fifo()
Raw creation for subclasses that do their own caching
Definition: Fifo.cs:17
virtual T Push(T entry)
Push a new entry onto the top of the stack
Definition: Fifo.cs:33
override string ToString()
//#TBD#//
bool Empty
//#TBD#//
Definition: Fifo.cs:29
void Clear()
//#TBD#//
Definition: Fifo.cs:74
Intentionally simple stack implementation
Definition: Fifo.cs:8
virtual void Dispose()
Send back to recycling
Definition: Fifo.cs:68
T [] ToArray()
//#TBD#//
Definition: Fifo.cs:80
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
Fifo< T > Swap()
Swap the top and second entries on the stack
Definition: Fifo.cs:48
int Count
Number of items on the stack
Definition: Fifo.cs:26
T Bottom
Get/set the value at the bottom of the stack.
Definition: Fifo.cs:65
T Next
Get/set the value at second to top of the stack.
Definition: Fifo.cs:62