Advertisement
aslv

GenericList<T>

Sep 27th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. // -----------------------------------------------------------------------
  2. // <copyright file="GenericList.cs" company="">
  3. // TODO: Update copyright text.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6.  
  7. namespace GenericListClass
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Linq;
  12.     using System.Text;
  13.  
  14.     /// <summary>
  15.     /// GenericList&lt;T&gt; Class.
  16.     /// </summary>
  17.     public class GenericList<T>
  18.     {
  19.         private const int DefaultCapacity = 16;
  20.  
  21.         private T[] elements;
  22.         private int count;
  23.  
  24.         public GenericList(int capacity = GenericList<T>.DefaultCapacity)
  25.         {
  26.             this.elements = new T[capacity];
  27.             this.count = 0;
  28.         }
  29.         public GenericList(IEnumerable<T> list)
  30.         {
  31.             this.elements = list.ToArray();
  32.             this.count = list.Count();
  33.         }
  34.  
  35.         public int Count
  36.         {
  37.             get
  38.             {
  39.                 return this.count;
  40.             }
  41.         }
  42.         public int Capacity
  43.         {
  44.             get
  45.             {
  46.                 return this.elements.Count();
  47.             }
  48.         }
  49.  
  50.         private void Resize()
  51.         {
  52.             T[] temp = this.elements.ToArray();
  53.             this.elements = new T[this.elements.Count() << 1];
  54.             for (int i = 0, len = temp.Count(); i < len; i++)
  55.             {
  56.                 this.elements[i] = temp[i];
  57.             }
  58.         }
  59.         private void ShiftElementsToLeft(int start, int end)
  60.         {
  61.             for (int i = start; i <= end; i++)
  62.             {
  63.                 this.elements[i - 1] = this.elements[i];
  64.             }
  65.         }
  66.         private void ShiftElementsToRight(int start, int end)
  67.         {
  68.             for (int i = end; i >= start; i--)
  69.             {
  70.                 this.elements[i + 1] = this.elements[i];
  71.             }
  72.         }
  73.         private int FindElementInGenericList(T value)
  74.         {
  75.             int index = -1;
  76.             for (int i = 0; i < this.Count; i++)
  77.             {
  78.                 if (this.elements[i].Equals(value))
  79.                 {
  80.                     index = i;
  81.                     break;
  82.                 }
  83.             }
  84.             return index;
  85.         }
  86.  
  87.         public void Add(T element)
  88.         {
  89.             if (this.Count >= this.Capacity)
  90.             {
  91.                 this.Resize();
  92.             }
  93.             this.elements[this.count++] = element;
  94.         }
  95.         public T this[int index]
  96.         {
  97.             get
  98.             {
  99.                 if (index < 0 || index >= this.Count)
  100.                 {
  101.                     throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
  102.                 }
  103.                 return this.elements[index];
  104.             }
  105.             set
  106.             {
  107.                 if (index < 0 || index >= this.Count)
  108.                 {
  109.                     throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
  110.                 }
  111.                 this.elements[index] = value;
  112.             }
  113.         }
  114.         public void RemoveAt(int index)
  115.         {
  116.             if (index < 0 || index >= this.Count)
  117.             {
  118.                 throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
  119.             }
  120.             this.ShiftElementsToLeft(index + 1, this.Count - 1);
  121.             this.count--;
  122.         }
  123.         public void InsertAt(T value, int index)
  124.         {
  125.             if (index < 0 || index >= this.Count)
  126.             {
  127.                 throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
  128.             }
  129.             if (this.Count >= this.Capacity)
  130.             {
  131.                 Resize();
  132.             }
  133.             this.ShiftElementsToRight(index, this.Count - 1);
  134.             this.elements[index] = value;
  135.             this.count++;
  136.         }
  137.         public void Clear()
  138.         {
  139.             this.elements = new T[GenericList<T>.DefaultCapacity];
  140.             this.count = 0;
  141.         }
  142.         public int Find(T value)
  143.         {
  144.             int index = this.FindElementInGenericList(value);
  145.             if (index == -1)
  146.             {
  147.                 throw new KeyNotFoundException(string.Format("The value {0} is not found.", value));
  148.             }
  149.             return index;
  150.         }
  151.         public bool Countains(T value)
  152.         {
  153.             return this.FindElementInGenericList(value) != -1;
  154.         }
  155.         public override string ToString()
  156.         {
  157.             //var seg = new ArraySegment<T>(this.elements, 0, this.count);
  158.             var seg = this.elements.Take(count);
  159.             return string.Format("GenericList<{0}>:{{{1}}}", typeof(T).FullName, string.Join(", ", seg));
  160.         }
  161.  
  162.         public T Min()
  163.         {
  164.             if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
  165.             {
  166.                 return this.elements.Min();
  167.             }
  168.             else
  169.             {
  170.                 throw new InvalidOperationException(string.Format("Elements of type {0} are not comparable, so the GenericList<{0}> cannot has minimal value.", typeof(T).FullName));
  171.             }
  172.             //if (this.count == 0)
  173.             //{
  174.             //    throw new InvalidOperationException(string.Format("Cannot give minimal element of an empty GenericList<{0}> instance.", typeof(T).FullName));
  175.             //}
  176.             //T min = this.elements[0];
  177.             //for (int i = 1; i < this.count; i++)
  178.             //{
  179.             //    if (min.CompareTo(this.elements[i]) < 0)
  180.             //    {
  181.             //        min = this.elements[i];
  182.             //    }
  183.             //}
  184.             //return min;
  185.         }
  186.  
  187.         public T Max()
  188.         {
  189.             if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
  190.             {
  191.                 return this.elements.Max();
  192.             }
  193.             else
  194.             {
  195.                 throw new InvalidOperationException(string.Format("Elements of type {0} are not comparable, so the GenericList<{0}> cannot has maximal value.", typeof(T).FullName));
  196.             }
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement