Advertisement
Guest User

ListCore

a guest
Aug 4th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.77 KB | None | 0 0
  1. using System; using System.Collections; using System.Collections.Generic;
  2. using System.ComponentModel; namespace Fire {
  3. //##################################################################################################
  4. public abstract class ListCore<T> {
  5. //------------------------------------------------------------------------------------------
  6. public static readonly T[] EmptyArray = TYPE<T>.EmptyArray;
  7. public const int DefaultCapacity = 4;
  8. //------------------------------------------------------------------------------------------
  9. protected internal T[] items;
  10. protected internal int size;
  11. //##################################################################################################
  12. #region Constructors
  13. public ListCore() {
  14.     items = EmptyArray; }
  15. public ListCore(int capacity) {
  16.     if(capacity < 0) Throw.Range("capacity");
  17.     items = new T[capacity]; }
  18. public ListCore(ICollection<T> src) {
  19.     if(src == null) Throw.Null("src"); init(src); }
  20. public ListCore(IEnumerable<T> src) {
  21.     if(src == null) Throw.Null("src"); init(src); }
  22. public ListCore(IEnumerable src) {
  23.     if(src == null) Throw.Null("src"); init(src); }
  24. public ListCore(T[] src, bool swallow) {
  25.     if(src == null) Throw.Null("src");
  26.     if(swallow) { items = src; size = src.Length; }
  27.     else init((ICollection<T>)src); }
  28. public ListCore(T[] src, int from) {
  29.     if(src == null) Throw.Null("src");
  30.     if(from == src.Length) items = EmptyArray; else {
  31.         if(from < 0 || from > src.Length) Throw.Range("from");
  32.         items = new T[size = src.Length-from];
  33.         Array.Copy(src, from, items, 0, items.Length); }}
  34. public ListCore(T[] src, int from, int count) {
  35.     if(count == 0) items = EmptyArray; else {
  36.         if(src == null) Throw.Null("src");
  37.         if(from < 0 || from > src.Length) Throw.Range("from");
  38.         if(count < 0 || count > src.Length-from) Throw.Range("count");
  39.         items = new T[size = count];
  40.         Array.Copy(src, from, items, 0, items.Length); }}
  41. //--------------------------------------------------------------------------------------------------
  42. private void init(ICollection<T> src) {
  43.     items = new T[size = src.Count];
  44.     src.CopyTo(items, 0); }
  45. private void init(IEnumerable<T> src) {
  46.     ICollection<T> col = src as ICollection<T>;
  47.     if(col != null) { init(col); return; }
  48.     items = new T[size = DefaultCapacity];
  49.     foreach(T item in src) AddCore(item); }
  50. private void init(IEnumerable src) {
  51.     ICollection col = src as ICollection;
  52.     if(col != null) { init(col); return; }
  53.     items = new T[size = DefaultCapacity];
  54.     foreach(T item in src) AddCore(item); }
  55. //--------------------------------------------------------------------------------------------------
  56. #endregion
  57. //------------------------------------------------------------------------------------------
  58. #region Size
  59. protected void EnsureCapacityCore(int min) {
  60.     if(items.Length >= min) return;
  61.     int n = items.Length << 1;
  62.     if(n < DefaultCapacity) n = DefaultCapacity;
  63.     if(n < min) n = min;
  64.     T[] a = new T[n];
  65.     Array.Copy(items, 0, a, 0, size);
  66.     items = a; }
  67. protected void GrowCore() {
  68.     int n = items.Length << 1;
  69.     if(n < DefaultCapacity) n = DefaultCapacity;
  70.     T[] a = new T[n];
  71.     Array.Copy(items, 0, a, 0, size);
  72.     items = a; }
  73. protected int CapacityCore {
  74.     get { return items.Length; }
  75.     set {
  76.         if(value == items.Length) return;
  77.         if(value < size) Throw.Range("value");
  78.         if(value == 0) items = EmptyArray;
  79.         else {
  80.             T[] a = new T[value];
  81.             Array.Copy(items, 0, a, 0, size);
  82.             items = a; }}}
  83. protected int CountCore {
  84.     get { return size; }
  85.     set {
  86.         if(value == size) return;
  87.         if(value > size) EnsureCapacityCore(value);
  88.         else if(value < 0) Throw.Range("value");
  89.         else Array.Clear(items, value, size-value);
  90.         size = value; }}
  91. #endregion
  92. //------------------------------------------------------------------------------------------
  93. #region Get, Set, Enumerate
  94. protected T GetCore(int index) {
  95.     if(index < 0 || index >= size) Throw.Range("index");
  96.     return items[index]; }
  97. protected void SetCore(int index, T value) {
  98.     if(index < 0 || index >= size) Throw.Range("index");
  99.     items[index] = value; }
  100. protected IEnumerator<T> GetEnumeratorCore() {
  101.     T[] a = items; int n = size;
  102.     for(int i = 0; i < n; i++)
  103.         yield return a[i]; }
  104. #endregion
  105. //------------------------------------------------------------------------------------------
  106. #region Add, Insert
  107. protected void AddCore(T item) {
  108.     if(size == items.Length) GrowCore();
  109.     items[size++] = item; }
  110. protected void InsertCore(int index, T item) {
  111.     if(index < 0 || index > size) Throw.Range("index");
  112.     if(size == items.Length) GrowCore();
  113.     if(index < size) Array.Copy(items, index, items, index+1, size-index);
  114.     items[index] = item; size++; }
  115. protected T TryAddCore(T item) {
  116.     int idx = IndexOfCore(item);
  117.     if(idx < 0) { AddCore(item); return item; }
  118.     return items[idx]; }
  119. //------------------------------------------------------------------------------------------
  120. protected void AddRangeCore(T[] src) {
  121.     if(src == null) Throw.Null("src");
  122.     int n = src.Length; EnsureCapacityCore(size+n);
  123.     Array.Copy(src, 0, items, size, n); size += n; }
  124. protected void InsertRangeCore(int index, T[] src) {
  125.     if(src == null) Throw.Null("src");
  126.     if(index < 0 || index > size) Throw.Range("index");
  127.     int n = src.Length; EnsureCapacityCore(size+n);
  128.     if(index < size) Array.Copy(items, index, items, index+n, size-index);
  129.     Array.Copy(src, 0, items, index, n); size += n; }
  130. //------------------------------------------------------------------------------------------
  131. protected void AddRangeCore(ICollection<T> src) {
  132.     if(src == null) Throw.Null("src");
  133.     EnsureCapacityCore(size+src.Count);
  134.     foreach(T item in src) items[size++] = item; }
  135. protected void InsertRangeCore(int index, ICollection<T> src) {
  136.     if(src == null) Throw.Null("src");
  137.     if(index < 0 || index > size) Throw.Range("index");
  138.     int n = src.Count; EnsureCapacityCore(size+n);
  139.     if(index < size) Array.Copy(items, index, items, index+n, size-index);
  140.     foreach(T item in src) items[index++] = item; size += n; }
  141. //------------------------------------------------------------------------------------------
  142. protected void AddRangeCore2(ICollection src) {
  143.     if(src == null) Throw.Null("src");
  144.     EnsureCapacityCore(size+src.Count);
  145.     foreach(T item in src) items[size++] = item; }
  146. protected void InsertRangeCore2(int index, ICollection src) {
  147.     if(src == null) Throw.Null("src");
  148.     if(index < 0 || index > size) Throw.Range("index");
  149.     int n = src.Count; EnsureCapacityCore(size+n);
  150.     if(index < size) Array.Copy(items, index, items, index+n, size-index);
  151.     foreach(T item in src) items[index++] = item; size += n; }
  152. //------------------------------------------------------------------------------------------
  153. protected void AddRangeCore(T[] src, int from) {
  154.     if(src == null) Throw.Null("src");
  155.     int n = src.Length-from; if(n == 0) return;
  156.     if(n < 0 || from < 0) Throw.Range("from");
  157.     EnsureCapacityCore(size+n);
  158.     Array.Copy(src, from, items, size, n); size += n; }
  159. protected void InsertRangeCore(int index, T[] src, int from) {
  160.     if(src == null) Throw.Null("src");
  161.     if(index < 0 || index > size) Throw.Range("index");
  162.     int n = src.Length-from; if(n == 0) return;
  163.     if(n < 0 || from < 0) Throw.Range("from");
  164.     EnsureCapacityCore(size+n);
  165.     if(index < size) Array.Copy(items, index, items, index+n, size-index);
  166.     Array.Copy(src, from, items, index, n); size += n; }
  167. //------------------------------------------------------------------------------------------
  168. protected void AddRangeCore(T[] src, int from, int count) {
  169.     if(count == 0) return;
  170.     if(src == null) Throw.Null("src");
  171.     if(from < 0 || from > src.Length) Throw.Range("from");
  172.     if(count < 0 || count > src.Length-from) Throw.Range("count");
  173.     EnsureCapacityCore(size+count);
  174.     Array.Copy(src, from, items, size, count); size += count; }
  175. protected void InsertRangeCore(int index, T[] src, int from, int count) {
  176.     if(count == 0) return;
  177.     if(src == null) Throw.Null("src");
  178.     if(index < 0 || index > size) Throw.Range("index");
  179.     if(from < 0 || from > src.Length) Throw.Range("from");
  180.     if(count < 0 || count > src.Length-from) Throw.Range("count");
  181.     EnsureCapacityCore(size+count);
  182.     if(index < size) Array.Copy(items, index, items, index+count, size-index);
  183.     Array.Copy(src, from, items, index, count); size += count; }
  184. #endregion
  185. //------------------------------------------------------------------------------------------
  186. #region Remove, Clear
  187. protected void ClearCore() {
  188.     if(size == 0) return; Array.Clear(items, 0, size); size = 0; }
  189. protected bool RemoveCore(T item) {
  190.     int index = Array.IndexOf(items, item, 0, size);
  191.     if(index < 0) return false;
  192.     if(index < --size) Array.Copy(items, index+1, items, index, size-index);
  193.     items[size] = default(T); return true; }
  194. protected void RemoveAtCore(int index) {
  195.     if(index < 0 || index > size) Throw.Range("index");
  196.     if(index < --size) Array.Copy(items, index+1, items, index, size-index);
  197.     items[size] = default(T); }
  198. protected T RemoveAndGetCore(int index) {
  199.     if(index < 0 || index > size) Throw.Range("index");
  200.     T item = items[index];
  201.     if(index < --size) Array.Copy(items, index+1, items, index, size-index);
  202.     items[size] = default(T);
  203.     return item; }
  204. protected void RemoveRangeCore(int from, int count) {
  205.     if(count == 0) return;
  206.     if(from < 0 || from > size) Throw.Range("from");
  207.     if(count < 0 || count > size-from) Throw.Range("count");
  208.     if(from < (size -= count)) Array.Copy(items, from+count, items, from, size-from);
  209.     Array.Clear(items, size, count); }
  210. #endregion
  211. //------------------------------------------------------------------------------------------
  212. #region Move, Swap
  213. protected T MoveCore(int from, int to) {
  214.     if(from < 0 || from >= size) Throw.Range("from" /* , from, 0, size-1 */);
  215.     if(to < 0 || to >= size) Throw.Range("to");
  216.     return items.Move(from, to); }
  217. protected int SetIndexCore(T item, int to) {
  218.     int from = IndexOfCore(item);
  219.     if(from < 0) Throw.Arg("item", "Not in list");
  220.     items.Move(from, to); return from; }
  221. protected int MoveRelativeCore(T item, int move) {
  222.     int from = IndexOfCore(item);
  223.     if(from < 0) Throw.Arg("item", "Not in list");
  224.     int to = from+move;
  225.     if(to < 0 || to >= size) Throw.Range("move");
  226.     items.Move(from, to); return to; }
  227. protected void SwapCore(int a, int b) {
  228.     if(a < 0 || a >= size) Throw.Range("a");
  229.     if(b < 0 || b >= size) Throw.Range("b");
  230.     T tmp = items[a]; items[a] = items[b]; items[b] = tmp; }
  231. #endregion
  232. //------------------------------------------------------------------------------------------
  233. #region Searching
  234. protected bool ContainsCore(T item) {
  235.     return items.Contains(item, 0, size); }
  236. protected int IndexOfCore(T item) {
  237.     return items.IndexOf(item, 0, size); }
  238. protected int IndexOfCore(T item, int startIndex) {
  239.     if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
  240.     return items.IndexOf(item, startIndex, size-startIndex); }
  241. protected int IndexOfCore(T item, int startIndex, int count) {
  242.     if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
  243.     if(count < 0 || count > size-startIndex) Throw.Range("count");
  244.     return items.IndexOf(item, startIndex, count); }
  245. protected int LastIndexOfCore(T item) {
  246.     return items.LastIndexOf(item, size-1, size); }
  247. protected int LastIndexOfCore(T item, int startIndex) {
  248.     if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
  249.     return items.LastIndexOf(item, startIndex, startIndex+1); }
  250. protected int LastIndexOfCore(T item, int startIndex, int count) {
  251.     if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
  252.     if(count < 0 || count > size-startIndex) Throw.Range("count");
  253.     return items.LastIndexOf(item, startIndex, count); }
  254. protected int SearchCore(T item) {
  255.     return Array.BinarySearch(items, 0, size, item); }
  256. protected int SearchCore(T item, int index, int count) {
  257.     if(index < 0 || index > size) Throw.Range("index");
  258.     if(count < 0 || count > index+1) Throw.Range("count");
  259.     return Array.BinarySearch(items, index, count, item); }
  260. protected int SearchCore(T item, IComparer<T> cmp) {
  261.     return Array.BinarySearch(items, 0, size, item, cmp); }
  262. protected int SearchCore(T item, int index, int count, IComparer<T> cmp) {
  263.     if(index < 0 || index > size) Throw.Range("index");
  264.     if(count < 0 || count > index+1) Throw.Range("count");
  265.     return Array.BinarySearch(items, index, count, item, cmp); }
  266. protected int SearchCore(T item, IComparer cmp) {
  267.     return Array.BinarySearch(items, 0, size, item, cmp); }
  268. protected int SearchCore(T item, int index, int count, IComparer cmp) {
  269.     if(index < 0 || index > size) Throw.Range("index");
  270.     if(count < 0 || count > index+1) Throw.Range("count");
  271.     return Array.BinarySearch(items, index, count, item, cmp); }
  272. #endregion
  273. //------------------------------------------------------------------------------------------
  274. #region Other
  275. protected void CopyToCore(T[] array) { items.CopyTo(array, 0, size); }
  276. protected void CopyToCore(T[] array, int index) { items.CopyTo(array, index, size); }
  277. protected void CopyToCore(Array array) { items.CopyTo(array, 0, size); }
  278. protected void CopyToCore(Array array, int index) { items.CopyTo(array, index, size); }
  279. protected T[] SubArrayCore(int from, int count) {
  280.     if(from < 0) Throw.Range("from");
  281.     if(count < 0 || size-from < count) Throw.Range("count");
  282.     return items.SubArray(from, count); }
  283.  
  284. protected void SortCore() { items.Sort(0, size); }
  285. protected void SortCore(IComparer<T> cmp) { items.Sort(0, size, cmp); }
  286. protected void SortCore(int from, int count) {
  287.     if(from < 0) Throw.Range("from");
  288.     if(count < 0 || size-from < count) Throw.Range("count");
  289.     items.Sort(from, count); }
  290. protected void SortCore(int from, int count, IComparer<T> cmp) {
  291.     if(from < 0) Throw.Range("from");
  292.     if(count < 0 || size-from < count) Throw.Range("count");
  293.     items.Sort(from, count, cmp); }
  294.  
  295. protected void ReverseCore() {
  296.     for(int i = 0, j = size-1, n = size>>1; i < n; i++, j--) {
  297.         T tmp = items[i]; items[i] = items[j]; items[j] = tmp;  } }
  298.  
  299. protected bool EqualsCore(ListCore<T> other) {
  300.     if(other == null) return false;
  301.     if(ReferenceEquals(other, this)) return true;
  302.     if(other.size != size) return false;
  303.     T[] a = items, b = other.items;
  304.     for(int i = 0, n = size; i < n; i++)
  305.         if(!Equals(a[i], b[i])) return false;
  306.     return true; }
  307. #endregion
  308. //------------------------------------------------------------------------------------------
  309. #region Verify
  310. protected static bool IsGood(object item) {
  311.     return (item == null && !typeof(T).IsValueType) || item is T; }
  312. protected static void Verify(object item) {
  313.     if(!IsGood(item)) throw new ArgumentException("Wrong type", "item"); }
  314. #endregion
  315. }
  316. //##################################################################################################
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement