From 21cc2cefbf5eed2944c5742a5ab74ea0afd3ed7f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 19 Aug 2022 11:47:11 -0400 Subject: [PATCH] refactor: Un-nest SelectorWrapper class, rename SelectorList --- .../Collections/Extensions/ListExtensions.cs | 39 +----- .../spark_core/Collections/SelectorList.cs | 117 ++++++++++++++++++ 2 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 src/client/spark_core/Collections/SelectorList.cs diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index f8809bf9c..fde19f349 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -374,10 +374,10 @@ namespace bts.collections { new SelectorWrapper(list, convert, convert_back); public static IList WrapSelector(this IList list, Func convert, Func convert_back) => - new SelectorWrapper(list, convert, convert_back); + new SelectorList(list, convert, convert_back); public static IReadOnlyList WrapSelectorRO(this IList list, Func convert) => - new SelectorWrapper(list, convert, null); + new SelectorList(list, convert, null); private static Random rng = new Random(); @@ -698,41 +698,6 @@ namespace bts.collections { #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 is 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; diff --git a/src/client/spark_core/Collections/SelectorList.cs b/src/client/spark_core/Collections/SelectorList.cs new file mode 100644 index 000000000..275aaba6a --- /dev/null +++ b/src/client/spark_core/Collections/SelectorList.cs @@ -0,0 +1,117 @@ +#nullable enable +namespace Spark.Core.Collections { + using System; + using System.Collections; + using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; + + /// + /// An that transforms elements of type to + /// + /// + public class SelectorList : IList, IReadOnlyList { + readonly Func _convert; + readonly Func? _convert_back; + readonly IList _source; + + /// + /// Initializes a new instance of + /// + /// The source list + /// + /// The delegate used to convert input elements (of the source list's element type + /// ) to type + /// + /// + /// The delegate used to convert output elements (of type ) + /// back to type . If null, the list will be read-only + /// (). + /// + public SelectorList( + IList source, Func convert, Func? convert_back = null) { + _source = source; + _convert = convert; + _convert_back = convert_back; + } + + public TOut this[int index] { + get => _convert(_source[index]); + set { + _require_mutable(); + _source[index] = _convert_back(value); + } + } + + public int Count => + _source.Count; + + [MemberNotNullWhen(false, nameof(_convert_back))] + public bool IsReadOnly => + /// Although isn't technically required for every mutation fn, + /// would be misleading to return true when all but + /// throw an ex. + _source.IsReadOnly || _convert_back is null; + + public void Add(TOut item) { + _require_mutable(); + _source.Add(_convert_back(item)); + } + + public void Clear() => + _source.Clear(); + + public bool Contains(TOut item) { + _require_convert_back(); + return _source.Contains(_convert_back(item)); + } + + public void CopyTo(TOut[] array, int array_index) { + for (int i = 0; i < _source.Count; i++) { + array[array_index + 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) { + _require_mutable(); + return _source.IndexOf(_convert_back(item)); + } + + public void Insert(int index, TOut item) { + _require_mutable(); + _source.Insert(index, _convert_back(item)); + } + + public bool Remove(TOut item) { + _require_mutable(); + return _source.Remove(_convert_back(item)); + } + + public void RemoveAt(int index) { + _require_mutable(); + _source.RemoveAt(index); + } + + IEnumerator IEnumerable.GetEnumerator() => + GetEnumerator(); + + [MemberNotNull(nameof(_convert_back))] + void _require_convert_back() { + if (_convert_back is null) { + throw new InvalidOperationException("The 'convert-back' delegate is null"); + } + } + + [MemberNotNull(nameof(_convert_back))] + void _require_mutable() { + if (IsReadOnly) { + throw new InvalidOperationException("The list is read-only"); + } + } + } +}