Advertisement
tomlev

IList<T> wrapper for ArraySegment<T>

Oct 12th, 2012
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1.     public static class ArraySegmentExtensions
  2.     {
  3.         public static IList<T> AsList<T>(this ArraySegment<T> arraySegment)
  4.         {
  5.             return new ArraySegmentList<T>(arraySegment);
  6.         }
  7.        
  8.         private class ArraySegmentList<T> : IList<T>
  9.         {
  10.             private readonly ArraySegment<T> _segment;
  11.  
  12.             public ArraySegmentList(ArraySegment<T> segment)
  13.             {
  14.                 _segment = segment;
  15.             }
  16.  
  17.             #region Implementation of IEnumerable
  18.  
  19.             public IEnumerator<T> GetEnumerator()
  20.             {
  21.                 for (int i = 0; i < Count; i++)
  22.                 {
  23.                     yield return this[i];
  24.                 }
  25.             }
  26.  
  27.             IEnumerator IEnumerable.GetEnumerator()
  28.             {
  29.                 return GetEnumerator();
  30.             }
  31.  
  32.             #endregion
  33.  
  34.             #region Implementation of ICollection<T>
  35.  
  36.             public void Add(T item)
  37.             {
  38.                 throw FixedLengthException();
  39.             }
  40.  
  41.             public void Clear()
  42.             {
  43.                 throw FixedLengthException();
  44.             }
  45.  
  46.             public bool Contains(T item)
  47.             {
  48.                 return this.AsEnumerable().Contains(item);
  49.             }
  50.  
  51.             public void CopyTo(T[] array, int arrayIndex)
  52.             {
  53.                 Array.Copy(_segment.Array, _segment.Offset, array, arrayIndex, _segment.Count);
  54.             }
  55.  
  56.             public bool Remove(T item)
  57.             {
  58.                 throw FixedLengthException();
  59.             }
  60.  
  61.             public int Count { get { return _segment.Count; } }
  62.             public bool IsReadOnly { get { return false; } }
  63.  
  64.             #endregion
  65.  
  66.             #region Implementation of IList<T>
  67.  
  68.             public int IndexOf(T item)
  69.             {
  70.                 int arrayIndex = Array.IndexOf(_segment.Array, item, _segment.Offset, _segment.Count);
  71.                 if (arrayIndex != -1)
  72.                     return arrayIndex - _segment.Offset;
  73.                 return -1;
  74.             }
  75.  
  76.             public void Insert(int index, T item)
  77.             {
  78.                 throw FixedLengthException();
  79.             }
  80.  
  81.             public void RemoveAt(int index)
  82.             {
  83.                 throw FixedLengthException();
  84.             }
  85.  
  86.             public T this[int index]
  87.             {
  88.                 get { return _segment.Array[index + _segment.Offset]; }
  89.                 set { _segment.Array[index + _segment.Offset] = value; }
  90.             }
  91.  
  92.             #endregion
  93.  
  94.             private Exception FixedLengthException()
  95.             {
  96.                 return new InvalidOperationException("The collection has a fixed size");
  97.             }
  98.         }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement