ReadOnlyDictionaryView<K, I, O>
public struct ReadOnlyDictionaryView<K, I, O> : IReadOnlyDictionary<K, O>, IEnumerable<KeyValuePair<K, O>>, IEnumerable, IReadOnlyCollection<KeyValuePair<K, O>>, IEquatable<ReadOnlyDictionaryView<K, I, O>>
Represents lazily converted read-only dictionary.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace DotNext.Collections.Generic
{
public readonly struct ReadOnlyDictionaryView<K, I, O> : IReadOnlyDictionary<K, O>, IEnumerable<KeyValuePair<K, O>>, IEnumerable, IReadOnlyCollection<KeyValuePair<K, O>>, IEquatable<ReadOnlyDictionaryView<K, I, O>>
{
private readonly IReadOnlyDictionary<K, I> source;
private readonly Converter<I, O> mapper;
public O this[K key] {
get {
return mapper(source[key]);
}
}
public IEnumerable<K> Keys => source.Keys;
public IEnumerable<O> Values => Enumerable.Select<I, O>(source.Values, Converter.AsFunc<I, O>(mapper));
public int Count => source.Count;
public ReadOnlyDictionaryView(IReadOnlyDictionary<K, I> dictionary, Converter<I, O> mapper)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
source = dictionary;
if (mapper == null)
throw new ArgumentNullException("mapper");
this.mapper = mapper;
}
public bool ContainsKey(K key)
{
return source.ContainsKey(key);
}
public IEnumerator<KeyValuePair<K, O>> GetEnumerator()
{
Converter<I, O> mapper = this.mapper;
return Enumerable.Select<KeyValuePair<K, I>, KeyValuePair<K, O>>((IEnumerable<KeyValuePair<K, I>>)source, (Func<KeyValuePair<K, I>, KeyValuePair<K, O>>)((KeyValuePair<K, I> entry) => new KeyValuePair<K, O>(entry.Key, mapper(entry.Value)))).GetEnumerator();
}
public bool TryGetValue(K key, out O value)
{
if (source.TryGetValue(key, out I value2)) {
value = mapper(value2);
return true;
}
value = default(O);
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Equals(ReadOnlyDictionaryView<K, I, O> other)
{
if (source == other.source)
return object.Equals(mapper, other.mapper);
return false;
}
public override int GetHashCode()
{
if (source != null && mapper != null)
return RuntimeHelpers.GetHashCode(source) ^ mapper.GetHashCode();
return 0;
}
public override bool Equals(object other)
{
if (!(other is ReadOnlyDictionaryView<K, I, O>))
return object.Equals(source, other);
ReadOnlyDictionaryView<K, I, O> other2 = (ReadOnlyDictionaryView<K, I, O>)other;
return Equals(other2);
}
public static bool operator ==(ReadOnlyDictionaryView<K, I, O> first, ReadOnlyDictionaryView<K, I, O> second)
{
return first.Equals(second);
}
public static bool operator !=(ReadOnlyDictionaryView<K, I, O> first, ReadOnlyDictionaryView<K, I, O> second)
{
return !first.Equals(second);
}
}
}