Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. public class DynamicArray<T>// : IList<T>
  2.     {
  3.         public T[] RawArray;
  4.         public int Count
  5.         {
  6.             get;
  7.             private set;
  8.         }
  9.         public int Capacity
  10.         {
  11.             get;
  12.             private set;
  13.         }
  14.  
  15.         public bool IsReadOnly => throw new NotImplementedException();
  16.        
  17.         public T this[int index]
  18.         {
  19.             get
  20.             {
  21.                 return RawArray[index];
  22.             }
  23.             set
  24.             {
  25.                 RawArray[index] = value;
  26.             }  
  27.         }
  28.         /*
  29.         public ref T this[int index]
  30.         {
  31.             get
  32.             {
  33.                 return ref RawArray[index];
  34.             }
  35.         }        
  36.         */
  37.         public DynamicArray(int capacity = 4)
  38.         {
  39.             Capacity = capacity;
  40.             RawArray = new T[Capacity];
  41.         }
  42.  
  43.         public void Add(T element)
  44.         {
  45.             if(Count == Capacity)
  46.             {
  47.                 Capacity *= 2;
  48.                 Array.Resize(ref RawArray, Capacity);
  49.             }
  50.             RawArray[Count] = element;
  51.             Count++;
  52.         }
  53.  
  54.         public bool Remove(T item)
  55.         {
  56.             int index = Array.IndexOf(RawArray, item, 0, Count);
  57.             if(index >= 0)
  58.             {
  59.                 Count--;
  60.                 Array.Copy(RawArray, index+1, RawArray, index, Count-index);
  61.                 return true;
  62.             }
  63.             return false;
  64.         }
  65.  
  66.         public int IndexOf(T item)
  67.         {
  68.             throw new NotImplementedException();
  69.         }
  70.  
  71.         public void Insert(int index, T item)
  72.         {
  73.             throw new NotImplementedException();
  74.         }
  75.  
  76.         public void RemoveAt(int index)
  77.         {
  78.             throw new NotImplementedException();
  79.         }
  80.  
  81.         public void Clear()
  82.         {
  83.             throw new NotImplementedException();
  84.         }
  85.  
  86.         public bool Contains(T item)
  87.         {
  88.             throw new NotImplementedException();
  89.         }
  90.  
  91.         public void CopyTo(T[] array, int arrayIndex)
  92.         {
  93.             throw new NotImplementedException();
  94.         }
  95.  
  96.         public IEnumerator<T> GetEnumerator()
  97.         {
  98.             throw new NotImplementedException();
  99.         }
  100.  
  101.        /* IEnumerator IEnumerable.GetEnumerator()
  102.         {
  103.             throw new NotImplementedException();
  104.         }*/
  105.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement