Advertisement
simonradev

SimpleCustomList

Jan 7th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DelegatesAndEvents
  6. {
  7.     public class CustomList<T>
  8.         where T : IComparable<T>
  9.     {
  10.         private const int DefaultSize = 16;
  11.         private const string Separator = ", ";
  12.  
  13.         private T[] elements;
  14.         private int index;
  15.  
  16.         public CustomList()
  17.             : this(DefaultSize)
  18.         {
  19.         }
  20.  
  21.         public CustomList(int initialSize)
  22.         {
  23.             this.elements = new T[initialSize];
  24.         }
  25.  
  26.         public int Count
  27.         {
  28.             get
  29.             {
  30.                 return this.index;
  31.             }
  32.         }
  33.  
  34.         public int Capacity
  35.         {
  36.             get
  37.             {
  38.                 return this.elements.Length;
  39.             }
  40.         }
  41.  
  42.         private bool ResizeIsNeeded()
  43.         {
  44.             return this.elements.Length == this.index;
  45.         }
  46.  
  47.         private void DoubleUpTheSizeOfTheArray()
  48.         {
  49.             int sizeOfNewArray = this.index * 2;
  50.             T[] newArray = new T[sizeOfNewArray];
  51.  
  52.             for (int currentElement = 0; currentElement < this.elements.Length; currentElement++)
  53.             {
  54.                 newArray[currentElement] = this.elements[currentElement];
  55.             }
  56.  
  57.             this.elements = newArray;
  58.         }
  59.  
  60.         private ElementSearchResult FindMatchingElement(T elementToRemove)
  61.         {
  62.             bool matchIsFound = false;
  63.             int lastIndex;
  64.             for (lastIndex = 0; lastIndex < this.index; lastIndex++)
  65.             {
  66.                 T currentElement = this.elements[lastIndex];
  67.                 if (currentElement.CompareTo(elementToRemove) == 0)
  68.                 {
  69.                     matchIsFound = true;
  70.                     break;
  71.                 }
  72.             }
  73.  
  74.             return new ElementSearchResult(lastIndex, matchIsFound);
  75.         }
  76.  
  77.         private void RemoveElementAtIndex(int indexToRemove)
  78.         {
  79.             for (int moveIndex = indexToRemove; moveIndex < this.index - 1; moveIndex++)
  80.             {
  81.                 this.elements[moveIndex] = this.elements[moveIndex + 1];
  82.             }
  83.         }
  84.  
  85.         public void Add(T elementToAdd)
  86.         {
  87.             if (this.ResizeIsNeeded())
  88.             {
  89.                 this.DoubleUpTheSizeOfTheArray();
  90.             }
  91.  
  92.             this.elements[this.index] = elementToAdd;
  93.  
  94.             this.index++;
  95.         }
  96.  
  97.         public bool Remove(T elementToRemove)
  98.         {
  99.             ElementSearchResult searchResult = this.FindMatchingElement(elementToRemove);
  100.  
  101.             if (searchResult.MatchIsFound)
  102.             {
  103.                 this.RemoveElementAtIndex(searchResult.LastIndex);
  104.  
  105.                 this.index--;
  106.             }
  107.            
  108.             return searchResult.MatchIsFound;
  109.         }
  110.  
  111.         public override string ToString()
  112.         {
  113.             IEnumerable<T> elementsToShow = this.elements.Take(this.index);
  114.  
  115.             return string.Join(Separator, elementsToShow);
  116.         }
  117.  
  118.         private class ElementSearchResult
  119.         {
  120.             public ElementSearchResult(int lastIndex, bool matchIsFound)
  121.             {
  122.                 this.LastIndex = lastIndex;
  123.                 this.MatchIsFound = matchIsFound;
  124.             }
  125.  
  126.             public int LastIndex { get; set; }
  127.  
  128.             public bool MatchIsFound { get; set; }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement