Created
January 4, 2018 06:34
-
-
Save mjs3339/9f8b2f2a2cbd9fe8a79216af18e4e8d0 to your computer and use it in GitHub Desktop.
C# Limited Size Stack Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class LimitedStack<T> : IEnumerable<T> | |
{ | |
private T[] _array; | |
public LimitedStack(int maxsize) | |
{ | |
if (maxsize == 0) | |
throw new Exception("The MaxSize argument must be greater than 0"); | |
MaxSize = maxsize; | |
_array = new T[MaxSize]; | |
Count = 0; | |
} | |
public int Count { get; private set; } | |
private int MaxSize { get; } | |
public T this[int index] | |
{ | |
get | |
{ | |
if (index > MaxSize - 1) | |
throw new Exception($"Index {index} is out of range {MaxSize - 1}"); | |
return _array[index]; | |
} | |
} | |
IEnumerator<T> IEnumerable<T>.GetEnumerator() | |
{ | |
return new Enumerator(this); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return new Enumerator(this); | |
} | |
public void Clear() | |
{ | |
Array.Clear(_array, 0, MaxSize); | |
Count = 0; | |
} | |
public bool Contains(T item) | |
{ | |
var size = Count; | |
var equalityComparer = EqualityComparer<T>.Default; | |
while (size-- > 0) | |
if (item == null) | |
{ | |
if (_array[size] == null) | |
return true; | |
} | |
else if (_array[size] != null && equalityComparer.Equals(_array[size], item)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public void LeftShift() | |
{ | |
var TArray = new T[MaxSize]; | |
Array.Copy(_array, 1, TArray, 0, MaxSize - 1); | |
_array = TArray; | |
} | |
public T Peek() | |
{ | |
return _array[Count - 1]; | |
} | |
public T Pop() | |
{ | |
Count--; | |
var TVal = _array[Count]; | |
_array[Count] = default(T); | |
return TVal; | |
} | |
public void Push(T item) | |
{ | |
if (Count < MaxSize) | |
{ | |
_array[Count] = item; | |
Count++; | |
} | |
else | |
{ | |
LeftShift(); | |
_array[Count - 1] = item; | |
} | |
} | |
[Serializable] | |
public struct Enumerator : IEnumerator<T> | |
{ | |
private readonly LimitedStack<T> _stack; | |
private int _index; | |
public T Current { get; private set; } | |
object IEnumerator.Current => Current; | |
internal Enumerator(LimitedStack<T> stack) | |
{ | |
_stack = stack; | |
_index = -2; | |
Current = default(T); | |
} | |
public void Dispose() | |
{ | |
_index = -1; | |
} | |
public bool MoveNext() | |
{ | |
if (_index == -2) | |
{ | |
_index = _stack.Count - 1; | |
if (_index >= 0) | |
Current = _stack._array[_index]; | |
return _index >= 0; | |
} | |
if (_index == -1) | |
return false; | |
_index--; | |
Current = !(_index >= 0) ? default(T) : _stack._array[_index]; | |
return _index >= 0; | |
} | |
void IEnumerator.Reset() | |
{ | |
_index = -2; | |
Current = default(T); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment