Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System; using System.Collections; using System.Collections.Generic;
- using System.ComponentModel; namespace Fire {
- //##################################################################################################
- public abstract class ListCore<T> {
- //------------------------------------------------------------------------------------------
- public static readonly T[] EmptyArray = TYPE<T>.EmptyArray;
- public const int DefaultCapacity = 4;
- //------------------------------------------------------------------------------------------
- protected internal T[] items;
- protected internal int size;
- //##################################################################################################
- #region Constructors
- public ListCore() {
- items = EmptyArray; }
- public ListCore(int capacity) {
- if(capacity < 0) Throw.Range("capacity");
- items = new T[capacity]; }
- public ListCore(ICollection<T> src) {
- if(src == null) Throw.Null("src"); init(src); }
- public ListCore(IEnumerable<T> src) {
- if(src == null) Throw.Null("src"); init(src); }
- public ListCore(IEnumerable src) {
- if(src == null) Throw.Null("src"); init(src); }
- public ListCore(T[] src, bool swallow) {
- if(src == null) Throw.Null("src");
- if(swallow) { items = src; size = src.Length; }
- else init((ICollection<T>)src); }
- public ListCore(T[] src, int from) {
- if(src == null) Throw.Null("src");
- if(from == src.Length) items = EmptyArray; else {
- if(from < 0 || from > src.Length) Throw.Range("from");
- items = new T[size = src.Length-from];
- Array.Copy(src, from, items, 0, items.Length); }}
- public ListCore(T[] src, int from, int count) {
- if(count == 0) items = EmptyArray; else {
- if(src == null) Throw.Null("src");
- if(from < 0 || from > src.Length) Throw.Range("from");
- if(count < 0 || count > src.Length-from) Throw.Range("count");
- items = new T[size = count];
- Array.Copy(src, from, items, 0, items.Length); }}
- //--------------------------------------------------------------------------------------------------
- private void init(ICollection<T> src) {
- items = new T[size = src.Count];
- src.CopyTo(items, 0); }
- private void init(IEnumerable<T> src) {
- ICollection<T> col = src as ICollection<T>;
- if(col != null) { init(col); return; }
- items = new T[size = DefaultCapacity];
- foreach(T item in src) AddCore(item); }
- private void init(IEnumerable src) {
- ICollection col = src as ICollection;
- if(col != null) { init(col); return; }
- items = new T[size = DefaultCapacity];
- foreach(T item in src) AddCore(item); }
- //--------------------------------------------------------------------------------------------------
- #endregion
- //------------------------------------------------------------------------------------------
- #region Size
- protected void EnsureCapacityCore(int min) {
- if(items.Length >= min) return;
- int n = items.Length << 1;
- if(n < DefaultCapacity) n = DefaultCapacity;
- if(n < min) n = min;
- T[] a = new T[n];
- Array.Copy(items, 0, a, 0, size);
- items = a; }
- protected void GrowCore() {
- int n = items.Length << 1;
- if(n < DefaultCapacity) n = DefaultCapacity;
- T[] a = new T[n];
- Array.Copy(items, 0, a, 0, size);
- items = a; }
- protected int CapacityCore {
- get { return items.Length; }
- set {
- if(value == items.Length) return;
- if(value < size) Throw.Range("value");
- if(value == 0) items = EmptyArray;
- else {
- T[] a = new T[value];
- Array.Copy(items, 0, a, 0, size);
- items = a; }}}
- protected int CountCore {
- get { return size; }
- set {
- if(value == size) return;
- if(value > size) EnsureCapacityCore(value);
- else if(value < 0) Throw.Range("value");
- else Array.Clear(items, value, size-value);
- size = value; }}
- #endregion
- //------------------------------------------------------------------------------------------
- #region Get, Set, Enumerate
- protected T GetCore(int index) {
- if(index < 0 || index >= size) Throw.Range("index");
- return items[index]; }
- protected void SetCore(int index, T value) {
- if(index < 0 || index >= size) Throw.Range("index");
- items[index] = value; }
- protected IEnumerator<T> GetEnumeratorCore() {
- T[] a = items; int n = size;
- for(int i = 0; i < n; i++)
- yield return a[i]; }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Add, Insert
- protected void AddCore(T item) {
- if(size == items.Length) GrowCore();
- items[size++] = item; }
- protected void InsertCore(int index, T item) {
- if(index < 0 || index > size) Throw.Range("index");
- if(size == items.Length) GrowCore();
- if(index < size) Array.Copy(items, index, items, index+1, size-index);
- items[index] = item; size++; }
- protected T TryAddCore(T item) {
- int idx = IndexOfCore(item);
- if(idx < 0) { AddCore(item); return item; }
- return items[idx]; }
- //------------------------------------------------------------------------------------------
- protected void AddRangeCore(T[] src) {
- if(src == null) Throw.Null("src");
- int n = src.Length; EnsureCapacityCore(size+n);
- Array.Copy(src, 0, items, size, n); size += n; }
- protected void InsertRangeCore(int index, T[] src) {
- if(src == null) Throw.Null("src");
- if(index < 0 || index > size) Throw.Range("index");
- int n = src.Length; EnsureCapacityCore(size+n);
- if(index < size) Array.Copy(items, index, items, index+n, size-index);
- Array.Copy(src, 0, items, index, n); size += n; }
- //------------------------------------------------------------------------------------------
- protected void AddRangeCore(ICollection<T> src) {
- if(src == null) Throw.Null("src");
- EnsureCapacityCore(size+src.Count);
- foreach(T item in src) items[size++] = item; }
- protected void InsertRangeCore(int index, ICollection<T> src) {
- if(src == null) Throw.Null("src");
- if(index < 0 || index > size) Throw.Range("index");
- int n = src.Count; EnsureCapacityCore(size+n);
- if(index < size) Array.Copy(items, index, items, index+n, size-index);
- foreach(T item in src) items[index++] = item; size += n; }
- //------------------------------------------------------------------------------------------
- protected void AddRangeCore2(ICollection src) {
- if(src == null) Throw.Null("src");
- EnsureCapacityCore(size+src.Count);
- foreach(T item in src) items[size++] = item; }
- protected void InsertRangeCore2(int index, ICollection src) {
- if(src == null) Throw.Null("src");
- if(index < 0 || index > size) Throw.Range("index");
- int n = src.Count; EnsureCapacityCore(size+n);
- if(index < size) Array.Copy(items, index, items, index+n, size-index);
- foreach(T item in src) items[index++] = item; size += n; }
- //------------------------------------------------------------------------------------------
- protected void AddRangeCore(T[] src, int from) {
- if(src == null) Throw.Null("src");
- int n = src.Length-from; if(n == 0) return;
- if(n < 0 || from < 0) Throw.Range("from");
- EnsureCapacityCore(size+n);
- Array.Copy(src, from, items, size, n); size += n; }
- protected void InsertRangeCore(int index, T[] src, int from) {
- if(src == null) Throw.Null("src");
- if(index < 0 || index > size) Throw.Range("index");
- int n = src.Length-from; if(n == 0) return;
- if(n < 0 || from < 0) Throw.Range("from");
- EnsureCapacityCore(size+n);
- if(index < size) Array.Copy(items, index, items, index+n, size-index);
- Array.Copy(src, from, items, index, n); size += n; }
- //------------------------------------------------------------------------------------------
- protected void AddRangeCore(T[] src, int from, int count) {
- if(count == 0) return;
- if(src == null) Throw.Null("src");
- if(from < 0 || from > src.Length) Throw.Range("from");
- if(count < 0 || count > src.Length-from) Throw.Range("count");
- EnsureCapacityCore(size+count);
- Array.Copy(src, from, items, size, count); size += count; }
- protected void InsertRangeCore(int index, T[] src, int from, int count) {
- if(count == 0) return;
- if(src == null) Throw.Null("src");
- if(index < 0 || index > size) Throw.Range("index");
- if(from < 0 || from > src.Length) Throw.Range("from");
- if(count < 0 || count > src.Length-from) Throw.Range("count");
- EnsureCapacityCore(size+count);
- if(index < size) Array.Copy(items, index, items, index+count, size-index);
- Array.Copy(src, from, items, index, count); size += count; }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Remove, Clear
- protected void ClearCore() {
- if(size == 0) return; Array.Clear(items, 0, size); size = 0; }
- protected bool RemoveCore(T item) {
- int index = Array.IndexOf(items, item, 0, size);
- if(index < 0) return false;
- if(index < --size) Array.Copy(items, index+1, items, index, size-index);
- items[size] = default(T); return true; }
- protected void RemoveAtCore(int index) {
- if(index < 0 || index > size) Throw.Range("index");
- if(index < --size) Array.Copy(items, index+1, items, index, size-index);
- items[size] = default(T); }
- protected T RemoveAndGetCore(int index) {
- if(index < 0 || index > size) Throw.Range("index");
- T item = items[index];
- if(index < --size) Array.Copy(items, index+1, items, index, size-index);
- items[size] = default(T);
- return item; }
- protected void RemoveRangeCore(int from, int count) {
- if(count == 0) return;
- if(from < 0 || from > size) Throw.Range("from");
- if(count < 0 || count > size-from) Throw.Range("count");
- if(from < (size -= count)) Array.Copy(items, from+count, items, from, size-from);
- Array.Clear(items, size, count); }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Move, Swap
- protected T MoveCore(int from, int to) {
- if(from < 0 || from >= size) Throw.Range("from" /* , from, 0, size-1 */);
- if(to < 0 || to >= size) Throw.Range("to");
- return items.Move(from, to); }
- protected int SetIndexCore(T item, int to) {
- int from = IndexOfCore(item);
- if(from < 0) Throw.Arg("item", "Not in list");
- items.Move(from, to); return from; }
- protected int MoveRelativeCore(T item, int move) {
- int from = IndexOfCore(item);
- if(from < 0) Throw.Arg("item", "Not in list");
- int to = from+move;
- if(to < 0 || to >= size) Throw.Range("move");
- items.Move(from, to); return to; }
- protected void SwapCore(int a, int b) {
- if(a < 0 || a >= size) Throw.Range("a");
- if(b < 0 || b >= size) Throw.Range("b");
- T tmp = items[a]; items[a] = items[b]; items[b] = tmp; }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Searching
- protected bool ContainsCore(T item) {
- return items.Contains(item, 0, size); }
- protected int IndexOfCore(T item) {
- return items.IndexOf(item, 0, size); }
- protected int IndexOfCore(T item, int startIndex) {
- if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
- return items.IndexOf(item, startIndex, size-startIndex); }
- protected int IndexOfCore(T item, int startIndex, int count) {
- if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
- if(count < 0 || count > size-startIndex) Throw.Range("count");
- return items.IndexOf(item, startIndex, count); }
- protected int LastIndexOfCore(T item) {
- return items.LastIndexOf(item, size-1, size); }
- protected int LastIndexOfCore(T item, int startIndex) {
- if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
- return items.LastIndexOf(item, startIndex, startIndex+1); }
- protected int LastIndexOfCore(T item, int startIndex, int count) {
- if(startIndex < 0 || startIndex > size) Throw.Range("startIndex");
- if(count < 0 || count > size-startIndex) Throw.Range("count");
- return items.LastIndexOf(item, startIndex, count); }
- protected int SearchCore(T item) {
- return Array.BinarySearch(items, 0, size, item); }
- protected int SearchCore(T item, int index, int count) {
- if(index < 0 || index > size) Throw.Range("index");
- if(count < 0 || count > index+1) Throw.Range("count");
- return Array.BinarySearch(items, index, count, item); }
- protected int SearchCore(T item, IComparer<T> cmp) {
- return Array.BinarySearch(items, 0, size, item, cmp); }
- protected int SearchCore(T item, int index, int count, IComparer<T> cmp) {
- if(index < 0 || index > size) Throw.Range("index");
- if(count < 0 || count > index+1) Throw.Range("count");
- return Array.BinarySearch(items, index, count, item, cmp); }
- protected int SearchCore(T item, IComparer cmp) {
- return Array.BinarySearch(items, 0, size, item, cmp); }
- protected int SearchCore(T item, int index, int count, IComparer cmp) {
- if(index < 0 || index > size) Throw.Range("index");
- if(count < 0 || count > index+1) Throw.Range("count");
- return Array.BinarySearch(items, index, count, item, cmp); }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Other
- protected void CopyToCore(T[] array) { items.CopyTo(array, 0, size); }
- protected void CopyToCore(T[] array, int index) { items.CopyTo(array, index, size); }
- protected void CopyToCore(Array array) { items.CopyTo(array, 0, size); }
- protected void CopyToCore(Array array, int index) { items.CopyTo(array, index, size); }
- protected T[] SubArrayCore(int from, int count) {
- if(from < 0) Throw.Range("from");
- if(count < 0 || size-from < count) Throw.Range("count");
- return items.SubArray(from, count); }
- protected void SortCore() { items.Sort(0, size); }
- protected void SortCore(IComparer<T> cmp) { items.Sort(0, size, cmp); }
- protected void SortCore(int from, int count) {
- if(from < 0) Throw.Range("from");
- if(count < 0 || size-from < count) Throw.Range("count");
- items.Sort(from, count); }
- protected void SortCore(int from, int count, IComparer<T> cmp) {
- if(from < 0) Throw.Range("from");
- if(count < 0 || size-from < count) Throw.Range("count");
- items.Sort(from, count, cmp); }
- protected void ReverseCore() {
- for(int i = 0, j = size-1, n = size>>1; i < n; i++, j--) {
- T tmp = items[i]; items[i] = items[j]; items[j] = tmp; } }
- protected bool EqualsCore(ListCore<T> other) {
- if(other == null) return false;
- if(ReferenceEquals(other, this)) return true;
- if(other.size != size) return false;
- T[] a = items, b = other.items;
- for(int i = 0, n = size; i < n; i++)
- if(!Equals(a[i], b[i])) return false;
- return true; }
- #endregion
- //------------------------------------------------------------------------------------------
- #region Verify
- protected static bool IsGood(object item) {
- return (item == null && !typeof(T).IsValueType) || item is T; }
- protected static void Verify(object item) {
- if(!IsGood(item)) throw new ArgumentException("Wrong type", "item"); }
- #endregion
- }
- //##################################################################################################
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement