MrPe

ListBounded

Sep 6th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. /// <summary>
  2.     /// A List restricted to contain a limited number of items.
  3.     /// When the list is full an item is added the oldest
  4.     /// </summary>
  5.     /// <typeparam name="T"></typeparam>
  6.     public class ListBounded<T>
  7.     {
  8.         #region prop
  9.         //private T[] _array;
  10.         // 'internal' for testing purposes only
  11.         internal T[] _array;
  12.  
  13.         private int _max;
  14.  
  15.         private int _head;
  16.  
  17.         // Is true if more items than _max were added to the list.
  18.         private bool _firstRoundDone = false;
  19.  
  20.         /// <summary>
  21.         /// Number of available items in the list
  22.         /// </summary>
  23.         public int Count
  24.         {
  25.             get
  26.             {
  27.                 return _firstRoundDone ? _max : _head + 1;
  28.             }
  29.         }
  30.  
  31.         /// <summary>
  32.         /// Oldest item added to the list
  33.         /// </summary>
  34.         public T First
  35.         {
  36.             get
  37.             {
  38.                 if (_head < 0)
  39.                     throw new IndexOutOfRangeException("The list is empty");
  40.  
  41.                 if (_firstRoundDone)
  42.                     return _array[(_head + 1) % _max];
  43.                 else
  44.                     return _array[0];
  45.             }
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Last item added to the list
  50.         /// </summary>
  51.         public T Last
  52.         {
  53.             get
  54.             {
  55.                 if (_head < 0)
  56.                     throw new IndexOutOfRangeException("The list is empty");
  57.  
  58.                 return _array[_head];
  59.             }
  60.         }
  61.         #endregion
  62.  
  63.         #region init
  64.         public ListBounded(int maxItem)
  65.         {
  66.             _max = maxItem;
  67.             Clear();
  68.         }
  69.         #endregion
  70.  
  71.         #region core
  72.         public void Clear()
  73.         {
  74.             _array = new T[_max];
  75.             _head = -1;
  76.         }
  77.  
  78.         public void Add(T item)
  79.         {
  80.             if (_head == _max - 1) _firstRoundDone = true;
  81.  
  82.             _head = (_head + 1) % _max;
  83.             _array[_head] = item;
  84.         }
  85.  
  86.         public List<T> ToList()
  87.         {
  88.             var list = new List<T>();
  89.  
  90.             lock (this)
  91.             {
  92.                 int count = this.Count;
  93.                 for (int i = 0; i < count; i++)
  94.                 {
  95.                     list.Add(_array[(_head + i) % count]);
  96.                 }
  97.             }
  98.  
  99.             return list;
  100.         }
  101.         #endregion
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment