From 70e39d8d529c4ee1ec7ad6b562a96724dbb2c95b Mon Sep 17 00:00:00 2001 From: Miroslav312 <35622427+Miroslav312@users.noreply.github.com> Date: Tue, 9 Oct 2018 15:42:23 +0300 Subject: [PATCH] Add files via upload --- DataStructures/SortedList.cs | 135 +++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 DataStructures/SortedList.cs diff --git a/DataStructures/SortedList.cs b/DataStructures/SortedList.cs new file mode 100644 index 0000000..ccf0314 --- /dev/null +++ b/DataStructures/SortedList.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; + +namespace DataStructures.ListSpace +{ + /// + /// Use insertion sort for every element added + /// + /// + [Serializable] + public class SortedList : IEnumerable, ICollection + where T : IComparable + { + private List list; + + public int Capacity + { + get { return list.Capacity; } + } + + bool ICollection.Remove(T item) + { + var success = ((ICollection)list).Remove(item); + return success; + } + + public int Count + { + get { return list.Count; } + } + + public bool IsReadOnly { get; private set; } + public bool IsSynchronized { get { return false; } } + + [ContractInvariantMethod] + private void ObjectInvariant() + { + Contract.Invariant(Count >= 0); + Contract.Invariant(Capacity >= 0); + } + + public SortedList() + { + list = new List(); + } + + public SortedList(int capacity) + { + Contract.Requires(capacity > 0); + + list = new List(capacity); + } + + public SortedList(IEnumerable list) + { + Contract.Requires(list != null); + + this.list = new List(list); + this.list.Sort(); + } + + public void Add(T element) + { + Contract.Requires(element != null); + + var count = 0; + //search for the previous element only when there are other elements in the list + if(list.Count>0){ + for (var i = 0; i < list.Count; i++, count++) + { + if (element.CompareTo(list[i]) <= 0) + { + count = i; + break; + } + } + if(count>0) + Contract.Assert(list[count-1].CompareTo(element) <= 0); + } + list.Insert(count, element); + } + + public void Clear() + { + list = new List(); + } + + public bool Contains(T item) + { + return list.Contains(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + Contract.Requires(array != null, "array is null"); + Contract.Requires(arrayIndex >= 0, "arrayIndex less than 0"); + + for (var i = arrayIndex; i < list.Count; i++) + { + array[i] = list[i]; + } + } + + public void Remove(T element) + { + Contract.Requires(element != null); + + list.Remove(element); + } + + public void CopyTo(Array array, int index) + { + Contract.Requires(array != null, "array is null"); + Contract.Requires(index >= 0, "arrayIndex less than 0"); + Contract.Requires(array.Length < Count, "array not big enough"); + + int i = index; + foreach (T element in list) + { + array.SetValue(element, i++); + } + } + + public IEnumerator GetEnumerator() + { + return list.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + } +}