CircularBuffer.cs 520 B

123456789101112131415161718192021222324252627282930
  1. using UnityEngine;
  2. namespace IngameDebugConsole
  3. {
  4. public class CircularBuffer<T>
  5. {
  6. private T[] arr;
  7. private int index;
  8. public T this[int index] { get { return arr[( this.index + index ) % arr.Length]; } }
  9. public int Count { get; private set; }
  10. public CircularBuffer( int capacity )
  11. {
  12. arr = new T[capacity];
  13. }
  14. public void Add( T value )
  15. {
  16. if( Count < arr.Length )
  17. arr[Count++] = value;
  18. else
  19. {
  20. arr[index] = value;
  21. if( ++index >= arr.Length )
  22. index = 0;
  23. }
  24. }
  25. }
  26. }