refactor: Un-nest SelectorWrapper class, rename SelectorList
This commit is contained in:
1 parent
904df4260d
commit
21cc2cefbf
2 files changed
+119
-37
No files matched your search
@@ -374,10 +374,10 @@ namespace bts.collections {
|
||||
new SelectorWrapper<T>(list, convert, convert_back);
|
||||
|
||||
public static IList<TResult> WrapSelector<T, TResult>(this IList<T> list, Func<T, TResult> convert, Func<TResult, T> convert_back) =>
|
||||
new SelectorWrapper<T, TResult>(list, convert, convert_back);
|
||||
new SelectorList<T, TResult>(list, convert, convert_back);
|
||||
|
||||
public static IReadOnlyList<TResult> WrapSelectorRO<T, TResult>(this IList<T> list, Func<T, TResult> convert) =>
|
||||
new SelectorWrapper<T, TResult>(list, convert, null);
|
||||
new SelectorList<T, TResult>(list, convert, null);
|
||||
|
||||
private static Random rng = new Random();
|
||||
|
||||
@@ -698,41 +698,6 @@ namespace bts.collections {
|
||||
#endregion
|
||||
}
|
||||
|
||||
class SelectorWrapper<TIn, TOut> : IList<TOut>, IReadOnlyList<TOut> {
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly Func<TOut, TIn> _convert_back;
|
||||
readonly IList<TIn> _source;
|
||||
|
||||
public SelectorWrapper(IList<TIn> source, Func<TIn, TOut> convert, Func<TOut, TIn> 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<TOut> 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<T> : IList<T>, IReadOnlyList<T> {
|
||||
readonly IList<T> _source;
|
||||
readonly int _skip;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core.Collections {
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IList{T}"/> that transforms elements of type <typeparamref name="TIn"/> to
|
||||
/// <typeparamref name="TOut"/>
|
||||
/// </summary>
|
||||
public class SelectorList<TIn, TOut> : IList<TOut>, IReadOnlyList<TOut> {
|
||||
readonly Func<TIn, TOut> _convert;
|
||||
readonly Func<TOut, TIn>? _convert_back;
|
||||
readonly IList<TIn> _source;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="SelectorList{TIn, TOut}"/>
|
||||
/// </summary>
|
||||
/// <param name="source">The source list</param>
|
||||
/// <param name="convert">
|
||||
/// The delegate used to convert input elements (of the source list's element type
|
||||
/// <typeparamref name="TIn"/>) to type <typeparamref name="TOut"/>
|
||||
/// </param>
|
||||
/// <param name="convert_back">
|
||||
/// The delegate used to convert output elements (of type <typeparamref name="TOut"/>)
|
||||
/// back to type <typeparamref name="TIn"/>. If null, the list will be read-only
|
||||
/// (<see cref="IsReadOnly"/>).
|
||||
/// </param>
|
||||
public SelectorList(
|
||||
IList<TIn> source, Func<TIn, TOut> convert, Func<TOut, TIn>? 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 <see cref="_convert_back"/> isn't technically required for every mutation fn,
|
||||
/// <see cref="IsReadOnly"/> would be misleading to return true when all but
|
||||
/// <see cref="RemoveAt(int)"/> 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<TOut> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user