Files
spark/src/client/spark_core/utils/MappedDictionary.cs
T

109 lines
3.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace bts.collections {
static class MappedDictionary {
public static IReadOnlyDictionary<TKey, TResult> create<TKey, TValue, TResult>(IReadOnlyDictionary<TKey, TValue> source, Func<TValue, TResult> map) =>
new MappedDictionary<TKey, TValue, TResult>(source, map);
public static IReadOnlyDictionary<TKey, TResult> create<TKey, TValue, TResult>(IReadOnlyDictionary<TKey, TValue> source, Func<KeyValuePair<TKey, TValue>, TResult> map) =>
new KeyValueWrapper<TKey, TValue, TResult>(source, map);
class KeyValueWrapper<TKey, TValue, TResult> : IReadOnlyDictionary<TKey, TResult> {
readonly IReadOnlyDictionary<TKey, TValue> _source;
readonly Func<KeyValuePair<TKey, TValue>, TResult> _map;
public KeyValueWrapper(IReadOnlyDictionary<TKey, TValue> source, Func<KeyValuePair<TKey, TValue>, TResult> map) {
_source = source;
_map = map;
}
public TResult this[TKey key] => _map(new KeyValuePair<TKey, TValue>(key, _source[key]));
public IEnumerable<TKey> Keys => _source.Keys;
public IEnumerable<TResult> Values {
get {
foreach (var kv in _source)
yield return _map(kv);
}
}
public int Count => _source.Count;
public bool ContainsKey(TKey key) => _source.ContainsKey(key);
public IEnumerator<KeyValuePair<TKey, TResult>> GetEnumerator() {
foreach (var kv in _source)
yield return new KeyValuePair<TKey, TResult>(kv.Key, _map(kv));
}
public bool TryGetValue(TKey key, out TResult value) {
if (_source.TryGetValue(key, out var v)) {
value = _map(new KeyValuePair<TKey, TValue>(key, v));
return true;
}
value = default;
return false;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
internal class MappedDictionary<TKey, TSrc, TRes> : IReadOnlyDictionary<TKey, TRes> {
readonly IReadOnlyDictionary<TKey, TSrc> _dict;
readonly Func<TSrc, TRes> _map;
public MappedDictionary(IReadOnlyDictionary<TKey, TSrc> src, Func<TSrc, TRes> map) {
_dict = src;
_map = map;
}
public TRes this[TKey key] => _map(_dict[key]);
public IEnumerable<TKey> Keys => _dict.Keys;
public IEnumerable<TRes> Values => _dict.Values.Select(_map);
public int Count => _dict.Count;
public bool ContainsKey(TKey key) => _dict.ContainsKey(key);
public IEnumerator<KeyValuePair<TKey, TRes>> GetEnumerator() {
return new MappedEnumerator<KeyValuePair<TKey, TSrc>, KeyValuePair<TKey, TRes>>(
_dict.GetEnumerator(),
kvp => new KeyValuePair<TKey, TRes>(kvp.Key, _map(kvp.Value)));
}
public bool TryGetValue(TKey key, out TRes value) {
if (_dict.TryGetValue(key, out TSrc src)) {
value = _map(src);
return true;
}
value = default;
return false;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class MappedEnumerator<TSrc, TRes> : IEnumerator<TRes> {
readonly IEnumerator<TSrc> _src;
readonly Func<TSrc, TRes> _map;
public MappedEnumerator(IEnumerator<TSrc> src, Func<TSrc, TRes> map) {
_src = src;
_map = map;
}
public TRes Current => _map(_src.Current);
object IEnumerator.Current => Current;
public void Dispose() { _src.Dispose(); }
public bool MoveNext() => _src.MoveNext();
public void Reset() { _src.Reset(); }
}
}