From 36a84d28e2fecb9b9841d17981db25ab76375c2e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 4 Jan 2022 14:57:21 -0500 Subject: [PATCH] refactor: Improve RemoveWhere ext fn impl * Don't allocate list of items to remove * Return the number of items removed --- .../Collections/Extensions/ListExtensions.cs | 1132 +++++++++-------- 1 file changed, 573 insertions(+), 559 deletions(-) diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 2476b8b9f..d41ee27f3 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -1,560 +1,574 @@ -using bts.utils; -using mbts.utils; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; - -namespace bts.collections { - public static class ListExtensions { - public static void InsertSorted(this IList list, T item) => - list.InsertSorted(item, Comparer.Default); - - public static void InsertSorted(this IList list, IEnumerable items) => - InsertSorted(list, items, Comparer.Default); - - public static void InsertSorted(this IList list, IEnumerable items, IComparer comparer) { - foreach (var item in items) - list.InsertSorted(item, comparer); - } - - public static void InsertSorted(this IList list, T item, IComparer comparer) { - int i = CollectionHelper.binary_search(list, item, comparer); - list.Insert(i < 0 ? ~i : i, item); - } - - public static void InsertSorted(this List list, T value, IComparer comparer) { - int i = list.BinarySearch(value, comparer); - list.Insert(i < 0 ? ~i : i, value); - } - - public static void InsertOrReplaceSorted(this IList list, T item, IComparer comparer) { - int i = CollectionHelper.binary_search(list, item, comparer); - if (i < 0) { - list.Insert(~i, item); - } else { - list[i] = item; - } - } - - public static bool RemoveSorted(this IList list, T item, IComparer comparer) { - int i = CollectionHelper.binary_search(list, item, comparer); - if (i < 0) - return false; - list.RemoveAt(i); - return true; - } - - public static void RemoveSorted(this IList list, IEnumerable items) where T : IComparable => - RemoveSorted(list, items, Comparer.Default); - - public static void RemoveSorted(this IList list, IEnumerable items, IComparer comparer) { - foreach (var item in items) { - int i = CollectionHelper.binary_search(list, item, comparer); - if (i >= 0) - list.RemoveAt(i); - } - } - - public static T Pop(this IList list, int index) { - T rv = list.ElementAt(index); - list.RemoveAt(index); - return rv; - } - - public static bool Pop(this IList list, TKey key, Func key_selector, out T result) { - for(int i = 0; i < list.Count; i++) { - if (key_selector(list[i]).Equals(key)) { - result = list[i]; - return true; - } - } - result = default(T); - return false; - } - - public static bool PopDispose(this IList list, TKey key, Func key_selector) where T : IDisposable { - if (list.Pop(key, key_selector, out T result)) { - result.Dispose(); - return true; - } - return false; - } - - public static bool IsNullOrEmpty(this IList source) => - source == null || source.Count == 0; - - public static IEnumerable ExceptAt(this List list, int index) { - for (int i = 0; i < index; i++) - yield return list[i]; - for (int i = index + 1; i < list.Count; i++) - yield return list[i]; - } - - public static void RemoveWhere(this IList list, Func where) { foreach (var r in list.Where(where).ToList()) list.Remove(r); } - - public static void RemoveWhere(this IList list, Func where, Action on_remove) { - int count = list.Count; - for (int i = 0; i < count; i++) { - var t = list[i]; - if (where(t)) { - list.RemoveAt(i--); - --count; - on_remove(t); - } - } - } - - public static TResult[] ToArray(this IList list, Func selector) { - var array = new TResult[list.Count]; - for (int i = 0; i < list.Count; i++) - array[i] = selector(list[i]); - return array; - } - - public static bool SafeTryGetAt(this IList list, int index, out T value) { - if (list != null && index >= 0 && index < list.Count) { - value = list[index]; - return true; - } - value = default; - return false; - } - - public static bool TryGetAt(this IList list, int index, out T value) { - if (index >= 0 && index < list.Count) { - value = list[index]; - return true; - } - value = default; - return false; - } - - public static IList WrapValue(T value) => - new ValueListWrapper(value); - - public static IList WrapType(this IList list) => - new TypeWrapper(list); - - public static IList WrapReverse(this IList list) => - new ReverseWrapper(list); - - public static IReadOnlyList WrapReverseRO(this IList list) => - new ReverseWrapper(list); - - public static IList WrapSubRange(this IList list, int start) => - new SubRangeWrapper(list, start, list.Count - start); - - public static IList WrapSubRange(this IList list, int start, int length) => - new SubRangeWrapper(list, start, length); - - public static IReadOnlyList WrapSubRangeRO(this IList list, int start, int length) => - new SubRangeWrapper(list, start, length); - - public static IList WrapSkip(this IList list, int skip) => - new SkipFrontWrapper(list, skip); - - public static IList WrapSelector(this IList list, Func convert, Func convert_back = null) => - new SelectorWrapper(list, convert, convert_back); - - public static IList WrapSelectorHidden(this IList list, Func convert, Func convert_back = null) => - new SelectorWrapper(list, convert, convert_back); - - public static IList WrapSelector(this IList list, Func convert, Func convert_back) => - new SelectorWrapper(list, convert, convert_back); - - public static IReadOnlyList WrapSelectorRO(this IList list, Func convert) => - new SelectorWrapper(list, convert, null); - - private static Random rng = new Random(); - - public static void Shuffle(this IList list) { - int n = list.Count; - while (n > 1) { - n--; - int k = rng.Next(n + 1); - T value = list[k]; - list[k] = list[n]; - list[n] = value; - } - } - - public static void RemoveAt(this IList list, int index, int count) { - for (int i = 0; i < count; i++) - list.RemoveAt(index); - } - - public static int IndexOf(IReadOnlyList list, T value) { - for (int i = 0; i < list.Count; i++) - if (value.Equals(list[i])) - return i; - return -1; - } - - // unlike new T[] { value }, this struct prohibits changing the value at index 0. - struct ValueListWrapper : IList { - readonly T _value; - - public ValueListWrapper(T value) => - _value = value; - - public T this[int index] { - get => index == 0 ? _value : throw new IndexOutOfRangeException(); - set => throw new NotSupportedException(); - } - public int Count => 1; - public bool IsReadOnly => true; - - void ICollection.Add(T item) => throw new NotSupportedException(); - void ICollection.Clear() => throw new NotSupportedException(); - public bool Contains(T item) => _value.Equals(item); - public void CopyTo(T[] array, int arrayIndex) => array[arrayIndex] = _value; - public IEnumerator GetEnumerator() { - yield return _value; - } - public int IndexOf(T item) => _value.Equals(item) ? 0 : -1; - void IList.Insert(int index, T item) => throw new NotSupportedException(); - bool ICollection.Remove(T item) => throw new NotSupportedException(); - void IList.RemoveAt(int index) => throw new NotSupportedException(); - IEnumerator IEnumerable.GetEnumerator() { - yield return _value; - } - } - - class TypeWrapper : IList, IReadOnlyList { - readonly IList _source; - - public TypeWrapper(IList source) => - _source = source; - - public T this[int index] { - get => _source[index] is T v ? v : default; - set => _source[index] = value; - } - public int Count => _source.Count; - public bool IsReadOnly => _source.IsReadOnly; - - public void Add(T item) => _source.Add(item); - public void Clear() => _source.Clear(); - public bool Contains(T item) => _source.Contains(item); - public void CopyTo(T[] array, int array_index) => _source.CopyTo(array, array_index); - public IEnumerator GetEnumerator() { - for (int i = 0; i < _source.Count; i++) - yield return _source[i] is T v ? v : default; - } - public int IndexOf(T item) => _source.IndexOf(item); - public void Insert(int index, T item) => _source.Insert(index, item); - public bool Remove(T item) { - int index = _source.IndexOf(item); - if (index >= 0) { - _source.RemoveAt(index); - return true; - } else return false; - } - public void RemoveAt(int index) => _source.RemoveAt(index); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } - - class ReverseWrapper : IList, IReadOnlyList { - readonly IList _source; - - public ReverseWrapper(IList source) => - _source = source ?? throw new ArgumentNullException(nameof(source)); - - public T this[int index] { - get => _source[_check_reverse(index)]; - set => _source[_check_reverse(index)] = value; - } - - public int Count => _source.Count; - - public bool IsReadOnly => _source.IsReadOnly; - - public void Add(T item) => _source.Insert(0, item); - public void Clear() => _source.Clear(); - public bool Contains(T item) => _source.Contains(item); - public void CopyTo(T[] array, int arrayIndex) { - for (int i = 0; i < Count; i++) - array[arrayIndex + i] = _source[_reverse(i)]; - } - public IEnumerator GetEnumerator() { - for (int i = Count - 1; i >= 0; i--) - yield return _source[i]; - } - public int IndexOf(T item) { - /// Though it isn't stated in the IList contract, it is standard practice for 'IndexOf' to return -1 if the item isn't found. - /// Therefor, if 'i' is negative, return it directly (without reversing it). - int i = _source.IndexOf(item); - return i < 0 ? i : _reverse(i); - } - public void Insert(int index, T item) => _source.Insert(_check_reverse(index), item); - public bool Remove(T item) => _source.Remove(item); - public void RemoveAt(int index) => _source.RemoveAt(_check_reverse(index)); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - // Validates the index before reversing it, so any inevitable 'IndexOutOfRange' exception isn't incidentally reversed too. - int _check_reverse(int index) => - index < 0 ? throw ExceptionBuilder.IndexOutOfRange.lt_zero() : - index >= Count ? throw ExceptionBuilder.IndexOutOfRange.gte_length() : - _reverse(index); - - int _reverse(int index) => - Count - index - 1; - } - - class SubRangeWrapper : IList, IReadOnlyList { - readonly IList _source; - readonly int _start; - - public SubRangeWrapper(IList source, int start, int count) { - _source = source; - _start = start; - Count = count; - } - - public T this[int index] { - get => _source[_convert_index(index)]; - set => _source[_convert_index(index)] = value; - } - public int Count { get; private set; } - public bool IsReadOnly => _source.IsReadOnly; - - public void Add(T item) { - int index = _start + Count; - if (index < _source.Count) { - _source.Insert(index, item); - } else _source.Add(item); - Count++; - } - public void Clear() { - if (_source is List l) { - /// Favor ; it is optimized. - l.RemoveRange(_start, Count); - Count = 0; - } else { - while (Count > 0) { - _source.RemoveAt(_start); - Count--; - } - } - } - public bool Contains(T item) => IndexOf(item) >= 0; - public void CopyTo(T[] array, int arrayIndex) { - for (int i = 0; i < Count; i++) - array[i + arrayIndex] = _source[i + _start]; - } - public IEnumerator GetEnumerator() { - int stop = _start + Count; - for (int i = _start; i < stop; i++) - yield return _source[i]; - } - public int IndexOf(T item) { - int i = 0; - foreach (T v in this) { - if (v.Equals(item)) - return i; - i++; - } - return -1; - } - public void Insert(int index, T item) { - _source.Insert(_convert_index(index), item); - Count++; - } - public bool Remove(T item) { - int i = IndexOf(item); - if (i >= 0) { - _source.RemoveAt(i); - Count--; - return true; - } - return false; - } - public void RemoveAt(int index) { - _source.RemoveAt(_convert_index(index)); - Count--; - } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - int _convert_index(int index) { - if (index >= Count || index < 0) throw new ArgumentOutOfRangeException(nameof(index)); - return _start + index; - } - } - - class SelectorWrapper : IList, IList, IReadOnlyList { - readonly Func _convert; - readonly Func _convert_back; - readonly IList _source; - - public SelectorWrapper(IList source, Func convert, Func convert_back) { - _source = source; - _convert = convert; - _convert_back = convert_back; - } - - public T this[int index] { - get => _convert(_source[index]); - set => _source[index] = _convert_back(value); - } - public int Count => _source.Count; - public bool IsReadOnly => _source.IsReadOnly && _convert_back != null; - - public void Add(T item) => _source.Add(_convert_back(item)); - public void Clear() => _source.Clear(); - public bool Contains(T item) => _source.Contains(_convert_back(item)); - public void CopyTo(T[] array, int arrayIndex) { - for (int i = 0; i < _source.Count; i++) - array[arrayIndex + i] = _convert(_source[i]); - } - public IEnumerator GetEnumerator() { - for (int i = 0; i < _source.Count; i++) - yield return _convert(_source[i]); - } - public int IndexOf(T item) => _source.IndexOf(_convert_back(item)); - public void Insert(int index, T item) => _source.Insert(index, _convert_back(item)); - public bool Remove(T item) { - int i = _source.IndexOf(_convert_back(item)); - if (i >= 0) { - _source.RemoveAt(i); - return true; - } else return false; - } - public void RemoveAt(int index) => _source.RemoveAt(index); - - #region IList (non-generic) - object IList.this[int index] { - get => this[index]; - set { - if (value is T v) - this[index] = v; - } - } - bool IList.IsFixedSize => _source.IsFixedSize; - bool IList.IsReadOnly => IsReadOnly; - bool ICollection.IsSynchronized => _source.IsSynchronized; - object ICollection.SyncRoot => _source.SyncRoot; - int IList.Add(object value) { - if (value is T v) { - return _source.Add(_convert_back(v)); - } else throw new ArgumentException(nameof(value), $"Unexpected type '{value?.GetType()}.'"); - } - bool IList.Contains(object value) => value is T v && _source.Contains(_convert_back(v)); - void ICollection.CopyTo(Array array, int index) { - for (int i = 0; i < _source.Count; i++) - array.SetValue(_convert(_source[index]), index + i); - } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - int IList.IndexOf(object value) => value is T v ? IndexOf(v) : -1; - void IList.Insert(int index, object value) { - if (value is T v) - Insert(index, v); - } - void IList.Remove(object value) { - if (value is T v) - Remove(v); - } - void IList.RemoveAt(int index) => RemoveAt(index); - #endregion - } - - class SelectorWrapper : IList, IReadOnlyList { - readonly Func _convert; - readonly Func _convert_back; - readonly IList _source; - - public SelectorWrapper(IList source, Func convert, Func convert_back) { - _source = source; - _convert = convert; - _convert_back = convert_back; - } - - public TOut this[int index] { - get => _convert(_source[index]); - set => _source[index] = _convert_back(value); - } - public int Count => _source.Count; - public bool IsReadOnly => _source.IsReadOnly && _convert_back != null; - public void Add(TOut item) => _source.Add(_convert_back(item)); - public void Clear() => _source.Clear(); - public bool Contains(TOut item) => _source.Contains(_convert_back(item)); - public void CopyTo(TOut[] array, int arrayIndex) { - for (int i = 0; i < _source.Count; i++) - array[arrayIndex + i] = _convert(_source[i]); - } - public IEnumerator GetEnumerator() { - for (int i = 0; i < _source.Count; i++) - yield return _convert(_source[i]); - } - public int IndexOf(TOut item) => _source.IndexOf(_convert_back(item)); - public void Insert(int index, TOut item) => _source.Insert(index, _convert_back(item)); - public bool Remove(TOut item) => _source.Remove(_convert_back(item)); - public void RemoveAt(int index) => _source.RemoveAt(index); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } - - class SkipFrontWrapper : IList, IReadOnlyList { - readonly IList _source; - readonly int _skip; - - public SkipFrontWrapper(IList source, int skip) { - _source = source; - _skip = skip; - } - - public T this[int index] { - get => _source[_skip + index]; - set => _source[_skip + index] = value; - } - - public int Count { - get { - int c = _source.Count; - return c > _skip ? (c - _skip) : 0; - } - } - - public bool IsReadOnly => _source.IsReadOnly; - - public void Add(T item) => _source.Add(item); - public void Clear() { - int c = Count; - if (c == 0) - return; - if (_source is List l) { - l.RemoveRange(_skip, c); - } else { - for (int i = 0; i < c; i++) - _source.RemoveAt(_skip); - } - } - public bool Contains(T item) => IndexOf(item) >= 0; - public void CopyTo(T[] array, int arrayIndex) { - arrayIndex -= _skip; - for (int i = _skip; i < _source.Count; i++) - array[arrayIndex + i] = _source[i]; - } - public IEnumerator GetEnumerator() { - for (int i = _skip; i < _source.Count; i++) - yield return _source[i]; - } - public int IndexOf(T item) { - for (int i = _skip; i < _source.Count; i++) { - if (_source[i].Equals(item)) - return i - _skip; - } - return -1; - } - public void Insert(int index, T item) => _source.Insert(index + _skip, item); - public bool Remove(T item) { - int i = IndexOf(item); - if (i >= 0) { - _source.RemoveAt(i + _skip); - return true; - } else return false; - } - public void RemoveAt(int index) => _source.RemoveAt(index + _skip); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } - } +using bts.utils; +using mbts.utils; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace bts.collections { + public static class ListExtensions { + public static void InsertSorted(this IList list, T item) => + list.InsertSorted(item, Comparer.Default); + + public static void InsertSorted(this IList list, IEnumerable items) => + InsertSorted(list, items, Comparer.Default); + + public static void InsertSorted(this IList list, IEnumerable items, IComparer comparer) { + foreach (var item in items) + list.InsertSorted(item, comparer); + } + + public static void InsertSorted(this IList list, T item, IComparer comparer) { + int i = CollectionHelper.binary_search(list, item, comparer); + list.Insert(i < 0 ? ~i : i, item); + } + + public static void InsertSorted(this List list, T value, IComparer comparer) { + int i = list.BinarySearch(value, comparer); + list.Insert(i < 0 ? ~i : i, value); + } + + public static void InsertOrReplaceSorted(this IList list, T item, IComparer comparer) { + int i = CollectionHelper.binary_search(list, item, comparer); + if (i < 0) { + list.Insert(~i, item); + } else { + list[i] = item; + } + } + + public static bool RemoveSorted(this IList list, T item, IComparer comparer) { + int i = CollectionHelper.binary_search(list, item, comparer); + if (i < 0) + return false; + list.RemoveAt(i); + return true; + } + + public static void RemoveSorted(this IList list, IEnumerable items) where T : IComparable => + RemoveSorted(list, items, Comparer.Default); + + public static void RemoveSorted(this IList list, IEnumerable items, IComparer comparer) { + foreach (var item in items) { + int i = CollectionHelper.binary_search(list, item, comparer); + if (i >= 0) + list.RemoveAt(i); + } + } + + public static T Pop(this IList list, int index) { + T rv = list.ElementAt(index); + list.RemoveAt(index); + return rv; + } + + public static bool Pop(this IList list, TKey key, Func key_selector, out T result) { + for(int i = 0; i < list.Count; i++) { + if (key_selector(list[i]).Equals(key)) { + result = list[i]; + return true; + } + } + result = default(T); + return false; + } + + public static bool PopDispose(this IList list, TKey key, Func key_selector) where T : IDisposable { + if (list.Pop(key, key_selector, out T result)) { + result.Dispose(); + return true; + } + return false; + } + + public static bool IsNullOrEmpty(this IList source) => + source == null || source.Count == 0; + + public static IEnumerable ExceptAt(this List list, int index) { + for (int i = 0; i < index; i++) + yield return list[i]; + for (int i = index + 1; i < list.Count; i++) + yield return list[i]; + } + + public static int RemoveWhere(this IList list, Func where) { + int num_removed = 0; + for (int i = 0; i < list.Count;) { + if (where(list[i])) { + list.RemoveAt(i); + num_removed++; + continue; + } + i++; + } + return num_removed; + } + + public static int RemoveWhere(this IList list, Func where, Action on_remove) { + int num_removed = 0; + for (int i = 0; i < list.Count;) { + var t = list[i]; + if (where(t)) { + list.RemoveAt(i--); + on_remove(t); + num_removed++; + continue; + } + i++; + } + return num_removed; + } + + public static TResult[] ToArray(this IList list, Func selector) { + var array = new TResult[list.Count]; + for (int i = 0; i < list.Count; i++) + array[i] = selector(list[i]); + return array; + } + + public static bool SafeTryGetAt(this IList list, int index, out T value) { + if (list != null && index >= 0 && index < list.Count) { + value = list[index]; + return true; + } + value = default; + return false; + } + + public static bool TryGetAt(this IList list, int index, out T value) { + if (index >= 0 && index < list.Count) { + value = list[index]; + return true; + } + value = default; + return false; + } + + public static IList WrapValue(T value) => + new ValueListWrapper(value); + + public static IList WrapType(this IList list) => + new TypeWrapper(list); + + public static IList WrapReverse(this IList list) => + new ReverseWrapper(list); + + public static IReadOnlyList WrapReverseRO(this IList list) => + new ReverseWrapper(list); + + public static IList WrapSubRange(this IList list, int start) => + new SubRangeWrapper(list, start, list.Count - start); + + public static IList WrapSubRange(this IList list, int start, int length) => + new SubRangeWrapper(list, start, length); + + public static IReadOnlyList WrapSubRangeRO(this IList list, int start, int length) => + new SubRangeWrapper(list, start, length); + + public static IList WrapSkip(this IList list, int skip) => + new SkipFrontWrapper(list, skip); + + public static IList WrapSelector(this IList list, Func convert, Func convert_back = null) => + new SelectorWrapper(list, convert, convert_back); + + public static IList WrapSelectorHidden(this IList list, Func convert, Func convert_back = null) => + new SelectorWrapper(list, convert, convert_back); + + public static IList WrapSelector(this IList list, Func convert, Func convert_back) => + new SelectorWrapper(list, convert, convert_back); + + public static IReadOnlyList WrapSelectorRO(this IList list, Func convert) => + new SelectorWrapper(list, convert, null); + + private static Random rng = new Random(); + + public static void Shuffle(this IList list) { + int n = list.Count; + while (n > 1) { + n--; + int k = rng.Next(n + 1); + T value = list[k]; + list[k] = list[n]; + list[n] = value; + } + } + + public static void RemoveAt(this IList list, int index, int count) { + for (int i = 0; i < count; i++) + list.RemoveAt(index); + } + + public static int IndexOf(IReadOnlyList list, T value) { + for (int i = 0; i < list.Count; i++) + if (value.Equals(list[i])) + return i; + return -1; + } + + // unlike new T[] { value }, this struct prohibits changing the value at index 0. + struct ValueListWrapper : IList { + readonly T _value; + + public ValueListWrapper(T value) => + _value = value; + + public T this[int index] { + get => index == 0 ? _value : throw new IndexOutOfRangeException(); + set => throw new NotSupportedException(); + } + public int Count => 1; + public bool IsReadOnly => true; + + void ICollection.Add(T item) => throw new NotSupportedException(); + void ICollection.Clear() => throw new NotSupportedException(); + public bool Contains(T item) => _value.Equals(item); + public void CopyTo(T[] array, int arrayIndex) => array[arrayIndex] = _value; + public IEnumerator GetEnumerator() { + yield return _value; + } + public int IndexOf(T item) => _value.Equals(item) ? 0 : -1; + void IList.Insert(int index, T item) => throw new NotSupportedException(); + bool ICollection.Remove(T item) => throw new NotSupportedException(); + void IList.RemoveAt(int index) => throw new NotSupportedException(); + IEnumerator IEnumerable.GetEnumerator() { + yield return _value; + } + } + + class TypeWrapper : IList, IReadOnlyList { + readonly IList _source; + + public TypeWrapper(IList source) => + _source = source; + + public T this[int index] { + get => _source[index] is T v ? v : default; + set => _source[index] = value; + } + public int Count => _source.Count; + public bool IsReadOnly => _source.IsReadOnly; + + public void Add(T item) => _source.Add(item); + public void Clear() => _source.Clear(); + public bool Contains(T item) => _source.Contains(item); + public void CopyTo(T[] array, int array_index) => _source.CopyTo(array, array_index); + public IEnumerator GetEnumerator() { + for (int i = 0; i < _source.Count; i++) + yield return _source[i] is T v ? v : default; + } + public int IndexOf(T item) => _source.IndexOf(item); + public void Insert(int index, T item) => _source.Insert(index, item); + public bool Remove(T item) { + int index = _source.IndexOf(item); + if (index >= 0) { + _source.RemoveAt(index); + return true; + } else return false; + } + public void RemoveAt(int index) => _source.RemoveAt(index); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + class ReverseWrapper : IList, IReadOnlyList { + readonly IList _source; + + public ReverseWrapper(IList source) => + _source = source ?? throw new ArgumentNullException(nameof(source)); + + public T this[int index] { + get => _source[_check_reverse(index)]; + set => _source[_check_reverse(index)] = value; + } + + public int Count => _source.Count; + + public bool IsReadOnly => _source.IsReadOnly; + + public void Add(T item) => _source.Insert(0, item); + public void Clear() => _source.Clear(); + public bool Contains(T item) => _source.Contains(item); + public void CopyTo(T[] array, int arrayIndex) { + for (int i = 0; i < Count; i++) + array[arrayIndex + i] = _source[_reverse(i)]; + } + public IEnumerator GetEnumerator() { + for (int i = Count - 1; i >= 0; i--) + yield return _source[i]; + } + public int IndexOf(T item) { + /// Though it isn't stated in the IList contract, it is standard practice for 'IndexOf' to return -1 if the item isn't found. + /// Therefor, if 'i' is negative, return it directly (without reversing it). + int i = _source.IndexOf(item); + return i < 0 ? i : _reverse(i); + } + public void Insert(int index, T item) => _source.Insert(_check_reverse(index), item); + public bool Remove(T item) => _source.Remove(item); + public void RemoveAt(int index) => _source.RemoveAt(_check_reverse(index)); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + // Validates the index before reversing it, so any inevitable 'IndexOutOfRange' exception isn't incidentally reversed too. + int _check_reverse(int index) => + index < 0 ? throw ExceptionBuilder.IndexOutOfRange.lt_zero() : + index >= Count ? throw ExceptionBuilder.IndexOutOfRange.gte_length() : + _reverse(index); + + int _reverse(int index) => + Count - index - 1; + } + + class SubRangeWrapper : IList, IReadOnlyList { + readonly IList _source; + readonly int _start; + + public SubRangeWrapper(IList source, int start, int count) { + _source = source; + _start = start; + Count = count; + } + + public T this[int index] { + get => _source[_convert_index(index)]; + set => _source[_convert_index(index)] = value; + } + public int Count { get; private set; } + public bool IsReadOnly => _source.IsReadOnly; + + public void Add(T item) { + int index = _start + Count; + if (index < _source.Count) { + _source.Insert(index, item); + } else _source.Add(item); + Count++; + } + public void Clear() { + if (_source is List l) { + /// Favor ; it is optimized. + l.RemoveRange(_start, Count); + Count = 0; + } else { + while (Count > 0) { + _source.RemoveAt(_start); + Count--; + } + } + } + public bool Contains(T item) => IndexOf(item) >= 0; + public void CopyTo(T[] array, int arrayIndex) { + for (int i = 0; i < Count; i++) + array[i + arrayIndex] = _source[i + _start]; + } + public IEnumerator GetEnumerator() { + int stop = _start + Count; + for (int i = _start; i < stop; i++) + yield return _source[i]; + } + public int IndexOf(T item) { + int i = 0; + foreach (T v in this) { + if (v.Equals(item)) + return i; + i++; + } + return -1; + } + public void Insert(int index, T item) { + _source.Insert(_convert_index(index), item); + Count++; + } + public bool Remove(T item) { + int i = IndexOf(item); + if (i >= 0) { + _source.RemoveAt(i); + Count--; + return true; + } + return false; + } + public void RemoveAt(int index) { + _source.RemoveAt(_convert_index(index)); + Count--; + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + int _convert_index(int index) { + if (index >= Count || index < 0) throw new ArgumentOutOfRangeException(nameof(index)); + return _start + index; + } + } + + class SelectorWrapper : IList, IList, IReadOnlyList { + readonly Func _convert; + readonly Func _convert_back; + readonly IList _source; + + public SelectorWrapper(IList source, Func convert, Func convert_back) { + _source = source; + _convert = convert; + _convert_back = convert_back; + } + + public T this[int index] { + get => _convert(_source[index]); + set => _source[index] = _convert_back(value); + } + public int Count => _source.Count; + public bool IsReadOnly => _source.IsReadOnly && _convert_back != null; + + public void Add(T item) => _source.Add(_convert_back(item)); + public void Clear() => _source.Clear(); + public bool Contains(T item) => _source.Contains(_convert_back(item)); + public void CopyTo(T[] array, int arrayIndex) { + for (int i = 0; i < _source.Count; i++) + array[arrayIndex + i] = _convert(_source[i]); + } + public IEnumerator GetEnumerator() { + for (int i = 0; i < _source.Count; i++) + yield return _convert(_source[i]); + } + public int IndexOf(T item) => _source.IndexOf(_convert_back(item)); + public void Insert(int index, T item) => _source.Insert(index, _convert_back(item)); + public bool Remove(T item) { + int i = _source.IndexOf(_convert_back(item)); + if (i >= 0) { + _source.RemoveAt(i); + return true; + } else return false; + } + public void RemoveAt(int index) => _source.RemoveAt(index); + + #region IList (non-generic) + object IList.this[int index] { + get => this[index]; + set { + if (value is T v) + this[index] = v; + } + } + bool IList.IsFixedSize => _source.IsFixedSize; + bool IList.IsReadOnly => IsReadOnly; + bool ICollection.IsSynchronized => _source.IsSynchronized; + object ICollection.SyncRoot => _source.SyncRoot; + int IList.Add(object value) { + if (value is T v) { + return _source.Add(_convert_back(v)); + } else throw new ArgumentException(nameof(value), $"Unexpected type '{value?.GetType()}.'"); + } + bool IList.Contains(object value) => value is T v && _source.Contains(_convert_back(v)); + void ICollection.CopyTo(Array array, int index) { + for (int i = 0; i < _source.Count; i++) + array.SetValue(_convert(_source[index]), index + i); + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + int IList.IndexOf(object value) => value is T v ? IndexOf(v) : -1; + void IList.Insert(int index, object value) { + if (value is T v) + Insert(index, v); + } + void IList.Remove(object value) { + if (value is T v) + Remove(v); + } + void IList.RemoveAt(int index) => RemoveAt(index); + #endregion + } + + class SelectorWrapper : IList, IReadOnlyList { + readonly Func _convert; + readonly Func _convert_back; + readonly IList _source; + + public SelectorWrapper(IList source, Func convert, Func convert_back) { + _source = source; + _convert = convert; + _convert_back = convert_back; + } + + public TOut this[int index] { + get => _convert(_source[index]); + set => _source[index] = _convert_back(value); + } + public int Count => _source.Count; + public bool IsReadOnly => _source.IsReadOnly && _convert_back != null; + public void Add(TOut item) => _source.Add(_convert_back(item)); + public void Clear() => _source.Clear(); + public bool Contains(TOut item) => _source.Contains(_convert_back(item)); + public void CopyTo(TOut[] array, int arrayIndex) { + for (int i = 0; i < _source.Count; i++) + array[arrayIndex + i] = _convert(_source[i]); + } + public IEnumerator GetEnumerator() { + for (int i = 0; i < _source.Count; i++) + yield return _convert(_source[i]); + } + public int IndexOf(TOut item) => _source.IndexOf(_convert_back(item)); + public void Insert(int index, TOut item) => _source.Insert(index, _convert_back(item)); + public bool Remove(TOut item) => _source.Remove(_convert_back(item)); + public void RemoveAt(int index) => _source.RemoveAt(index); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + + class SkipFrontWrapper : IList, IReadOnlyList { + readonly IList _source; + readonly int _skip; + + public SkipFrontWrapper(IList source, int skip) { + _source = source; + _skip = skip; + } + + public T this[int index] { + get => _source[_skip + index]; + set => _source[_skip + index] = value; + } + + public int Count { + get { + int c = _source.Count; + return c > _skip ? (c - _skip) : 0; + } + } + + public bool IsReadOnly => _source.IsReadOnly; + + public void Add(T item) => _source.Add(item); + public void Clear() { + int c = Count; + if (c == 0) + return; + if (_source is List l) { + l.RemoveRange(_skip, c); + } else { + for (int i = 0; i < c; i++) + _source.RemoveAt(_skip); + } + } + public bool Contains(T item) => IndexOf(item) >= 0; + public void CopyTo(T[] array, int arrayIndex) { + arrayIndex -= _skip; + for (int i = _skip; i < _source.Count; i++) + array[arrayIndex + i] = _source[i]; + } + public IEnumerator GetEnumerator() { + for (int i = _skip; i < _source.Count; i++) + yield return _source[i]; + } + public int IndexOf(T item) { + for (int i = _skip; i < _source.Count; i++) { + if (_source[i].Equals(item)) + return i - _skip; + } + return -1; + } + public void Insert(int index, T item) => _source.Insert(index + _skip, item); + public bool Remove(T item) { + int i = IndexOf(item); + if (i >= 0) { + _source.RemoveAt(i + _skip); + return true; + } else return false; + } + public void RemoveAt(int index) => _source.RemoveAt(index + _skip); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + } } \ No newline at end of file