Advertisement
sashomaga

Generic List implementation

Feb 21st, 2013
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class GenericList<T>
  7. {
  8.     private T[] elements;
  9.     private int count = 0;
  10.  
  11.     public GenericList(int capacity)
  12.     {
  13.         this.elements = new T[capacity];
  14.     }
  15.  
  16.     public void Add(T element)
  17.     {
  18.         if (count >= elements.Length)
  19.         {
  20.             StretchCapacity();
  21.         }
  22.         elements[count] = element;
  23.         count++;
  24.     }
  25.  
  26.     private void StretchCapacity()
  27.     {
  28.         T[] tempArray = new T[elements.Length * 2];
  29.         Array.Copy(elements, 0, tempArray, 0, elements.Length);
  30.         elements = tempArray;
  31.     }
  32.  
  33.     public void Delete(T point)
  34.     {
  35.         int index = Array.IndexOf(elements,point);
  36.         //T[] tempArray = (T[])elements.Clone();
  37.         //elements = new T[tempArray.Length];
  38.         //for (int i = 0, j = 0; i < tempArray.Length; i++, j++)
  39.         //{
  40.         //    if (i != index)
  41.         //    {
  42.         //        elements[j] = tempArray[i];
  43.         //    }
  44.         //    else
  45.         //    {
  46.         //        j--;
  47.         //    }          
  48.         //}
  49.         T[] tempArray = (T[])elements.Clone();
  50.         elements = new T[tempArray.Length];
  51.         Array.Copy(tempArray, index + 1, elements, index, elements.Length - index - 1);
  52.         count--;      
  53.     }
  54.  
  55.     public int FindByValue(T element)
  56.     {
  57.         int myIndex = Array.IndexOf(elements, element);
  58.         return myIndex; //return index ot the element
  59.     }
  60.  
  61.     public void InsertElement(int position, T element)
  62.     {
  63.         if (count == elements.Length)
  64.         {
  65.             StretchCapacity();
  66.         }
  67.         //T[] tempArray = (T[])elements.Clone();
  68.         //elements = new T[tempArray.Length];
  69.         //for (int i = 0, j = 0; i < tempArray.Length; i++, j++)
  70.         //{
  71.         //    if (i != position)
  72.         //    {
  73.         //        elements[i] = tempArray[j];
  74.         //    }
  75.         //    else
  76.         //    {
  77.         //        elements[i] = element;
  78.         //        j--;
  79.         //    }
  80.         //}
  81.         Array.Copy(elements, position, elements, position + 1, elements.Length - position-1);
  82.         elements[position] = element;
  83.         count++;
  84.     }
  85.  
  86.     public void Clear()
  87.     {
  88.         int length;
  89.         length = elements.Length;
  90.         elements = new T[length];
  91.     }
  92.  
  93.     public override string ToString()
  94.     {
  95.         string concatenated = string.Join(",", elements.Select(x => x.ToString()).ToArray());
  96.         return concatenated;
  97.     }
  98.  
  99.     public T this[int index]
  100.     {
  101.         get
  102.         {
  103.             if (index >= count)
  104.             {
  105.                 throw new IndexOutOfRangeException(String.Format(
  106.                     "Invalid index: {0}.", index));
  107.             }
  108.             T result = elements[index];            
  109.             return result;
  110.         }
  111.     }  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement