kema

ReversedList

Jan 21st, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ReversedList<T> : IEnumerable<T>
  6. {
  7.     private T[] elements;
  8.  
  9.     public int Count { get; private set; }
  10.  
  11.     public int Capacity { get; private set; }
  12.  
  13.     public ReversedList()
  14.     {
  15.         this.Capacity = 2;
  16.  
  17.         this.elements = new T[this.Capacity];
  18.     }
  19.  
  20.  
  21.     public T this[int index]
  22.     {
  23.         get
  24.         {
  25.             CheckArrLenght(index);
  26.             return this.elements[index];
  27.         }
  28.  
  29.         set
  30.         {
  31.             CheckArrLenght(index);
  32.             this.elements[index] = value;
  33.         }
  34.     }
  35.  
  36.     public void Add(T item)
  37.     {
  38.         if (this.Count == 0)
  39.         {
  40.             this.elements[this.Count] = item;
  41.             this.Count++;
  42.             return;
  43.         }
  44.  
  45.         if (this.Count >= this.elements.Length)
  46.         {
  47.             this.Grow();
  48.         }
  49.  
  50.         var newElements = new T[this.Capacity];
  51.         this.CopyAllElementsTo(newElements, item);
  52.         this.elements = newElements;
  53.  
  54.     }
  55.  
  56.     public T RemoveAt(int index)
  57.     {
  58.         CheckArrLenght(index);
  59.  
  60.         T deletedElement = this.elements[index];
  61.  
  62.         for (int i = index; i < this.Count; i++)
  63.         {
  64.             this.elements[i] = this.elements[i + 1];
  65.         }
  66.  
  67.         this.Count--;
  68.  
  69.         if (this.Count <= this.elements.Length / 4)
  70.         {
  71.             this.Shrink();
  72.         }
  73.         return deletedElement;
  74.     }
  75.  
  76.     public IEnumerator<T> GetEnumerator()
  77.     {
  78.         var currentElement = 0;
  79.  
  80.         while (currentElement < this.elements.Length -1)
  81.         {
  82.             yield return this.elements[currentElement];
  83.             currentElement++;
  84.  
  85.         }
  86.     }
  87.  
  88.     IEnumerator IEnumerable.GetEnumerator()
  89.     {
  90.         return this.GetEnumerator();
  91.     }
  92.  
  93.     private void Shrink()
  94.     {
  95.         T[] newArr = new T[this.elements.Length / 2];
  96.         Array.Copy(this.elements, newArr, this.Count);
  97.         this.elements = newArr;
  98.     }
  99.  
  100.     private void CopyAllElementsTo(T[] newElements, T item)
  101.     {
  102.         newElements[0] = item;
  103.         Count++;
  104.  
  105.         for (int i = 1; i < this.Count; i++)
  106.         {
  107.             newElements[i] = this.elements[i - 1];
  108.         }
  109.     }      
  110.  
  111.     private void CheckArrLenght(int index)
  112.     {
  113.         if (index < 0 || index >= this.Count)
  114.         {
  115.             throw new ArgumentOutOfRangeException();
  116.         }
  117.     }
  118.  
  119.     private void Grow()
  120.     {
  121.         this.Capacity *= 2;
  122.         var newElements = new T[this.Capacity];
  123.         Array.Copy(this.elements, newElements, this.Count);
  124.         this.elements = newElements;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment