Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------------------------------------
- // <copyright file="GenericList.cs" company="">
- // TODO: Update copyright text.
- // </copyright>
- // -----------------------------------------------------------------------
- namespace GenericListClass
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /// <summary>
- /// GenericList<T> Class.
- /// </summary>
- public class GenericList<T>
- {
- private const int DefaultCapacity = 16;
- private T[] elements;
- private int count;
- public GenericList(int capacity = GenericList<T>.DefaultCapacity)
- {
- this.elements = new T[capacity];
- this.count = 0;
- }
- public GenericList(IEnumerable<T> list)
- {
- this.elements = list.ToArray();
- this.count = list.Count();
- }
- public int Count
- {
- get
- {
- return this.count;
- }
- }
- public int Capacity
- {
- get
- {
- return this.elements.Count();
- }
- }
- private void Resize()
- {
- T[] temp = this.elements.ToArray();
- this.elements = new T[this.elements.Count() << 1];
- for (int i = 0, len = temp.Count(); i < len; i++)
- {
- this.elements[i] = temp[i];
- }
- }
- private void ShiftElementsToLeft(int start, int end)
- {
- for (int i = start; i <= end; i++)
- {
- this.elements[i - 1] = this.elements[i];
- }
- }
- private void ShiftElementsToRight(int start, int end)
- {
- for (int i = end; i >= start; i--)
- {
- this.elements[i + 1] = this.elements[i];
- }
- }
- private int FindElementInGenericList(T value)
- {
- int index = -1;
- for (int i = 0; i < this.Count; i++)
- {
- if (this.elements[i].Equals(value))
- {
- index = i;
- break;
- }
- }
- return index;
- }
- public void Add(T element)
- {
- if (this.Count >= this.Capacity)
- {
- this.Resize();
- }
- this.elements[this.count++] = element;
- }
- public T this[int index]
- {
- get
- {
- if (index < 0 || index >= this.Count)
- {
- throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
- }
- return this.elements[index];
- }
- set
- {
- if (index < 0 || index >= this.Count)
- {
- throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
- }
- this.elements[index] = value;
- }
- }
- public void RemoveAt(int index)
- {
- if (index < 0 || index >= this.Count)
- {
- throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
- }
- this.ShiftElementsToLeft(index + 1, this.Count - 1);
- this.count--;
- }
- public void InsertAt(T value, int index)
- {
- if (index < 0 || index >= this.Count)
- {
- throw new IndexOutOfRangeException(string.Format("Invalid index: {0}", index));
- }
- if (this.Count >= this.Capacity)
- {
- Resize();
- }
- this.ShiftElementsToRight(index, this.Count - 1);
- this.elements[index] = value;
- this.count++;
- }
- public void Clear()
- {
- this.elements = new T[GenericList<T>.DefaultCapacity];
- this.count = 0;
- }
- public int Find(T value)
- {
- int index = this.FindElementInGenericList(value);
- if (index == -1)
- {
- throw new KeyNotFoundException(string.Format("The value {0} is not found.", value));
- }
- return index;
- }
- public bool Countains(T value)
- {
- return this.FindElementInGenericList(value) != -1;
- }
- public override string ToString()
- {
- //var seg = new ArraySegment<T>(this.elements, 0, this.count);
- var seg = this.elements.Take(count);
- return string.Format("GenericList<{0}>:{{{1}}}", typeof(T).FullName, string.Join(", ", seg));
- }
- public T Min()
- {
- if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
- {
- return this.elements.Min();
- }
- else
- {
- throw new InvalidOperationException(string.Format("Elements of type {0} are not comparable, so the GenericList<{0}> cannot has minimal value.", typeof(T).FullName));
- }
- //if (this.count == 0)
- //{
- // throw new InvalidOperationException(string.Format("Cannot give minimal element of an empty GenericList<{0}> instance.", typeof(T).FullName));
- //}
- //T min = this.elements[0];
- //for (int i = 1; i < this.count; i++)
- //{
- // if (min.CompareTo(this.elements[i]) < 0)
- // {
- // min = this.elements[i];
- // }
- //}
- //return min;
- }
- public T Max()
- {
- if (typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
- {
- return this.elements.Max();
- }
- else
- {
- throw new InvalidOperationException(string.Format("Elements of type {0} are not comparable, so the GenericList<{0}> cannot has maximal value.", typeof(T).FullName));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement