Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FIFO<T>
- {
- private T[] _array;
- private int _offset, _storage;
- public int Length { get { return this._array.Length; }}
- public FIFO(int size)
- {
- this._array = new T[size];
- this._offset = 0;
- this._storage = 0;
- }
- public T GetAt(int position, out bool checkValue)
- {
- checkValue = false;
- if(this._storage <= position)
- return default(T);
- checkValue = true;
- return this._array[(this._offset + position)%this._array.Length];
- }
- // Push a new value into the FIFO
- // @return true if an old value got dropped in the process
- public bool Push(T value)
- {
- bool ret = false;
- this._array[(this._offset + this._storage++) % this._array.Length] = value;
- if(this._storage > this._array.Length)
- {
- this._storage = this._array.Length;
- this._offset = (this._offset + 1 ) % this._array.Length;
- ret = true;
- }
- return ret;
- }
- // Pops the oldest value from the FIFO
- // @param checkValue true if successful and false if the FIFO is empty
- // @return default(T) if the FIFI is empty
- public T Pop(out bool checkValue)
- {
- checkValue = false;
- if(this._storage <= 0)
- return default(T);
- checkValue = true;
- T val = this._array[this._offset++];
- this._offset = this._offset % this._array.Length;
- this._storage--;
- return val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement