Dictionary
Represents various extensions for types Dictionary<T, U>
and IDictionary<T, U>.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Collections.Generic
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Dictionary
{
[System.Runtime.CompilerServices.Nullable(0)]
public static class Indexer<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>
{
public static Func<IReadOnlyDictionary<TKey, TValue>, TKey, TValue> ReadOnly { get; }
public static Func<IDictionary<TKey, TValue>, TKey, TValue> Getter { get; }
public static Action<IDictionary<TKey, TValue>, TKey, TValue> Setter { get; }
static Indexer()
{
RuntimeMethodHandle handle = (RuntimeMethodHandle);
RuntimeTypeHandle declaringType = typeof(IReadOnlyDictionary<TKey, TValue>).TypeHandle;
ReadOnly = DelegateHelpers.CreateDelegate<Func<IReadOnlyDictionary<TKey, TValue>, TKey, TValue>>((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType), (object)null);
handle = (RuntimeMethodHandle);
declaringType = typeof(IDictionary<TKey, TValue>).TypeHandle;
Getter = DelegateHelpers.CreateDelegate<Func<IDictionary<TKey, TValue>, TKey, TValue>>((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType), (object)null);
handle = (RuntimeMethodHandle);
Setter = DelegateHelpers.CreateDelegate<Action<IDictionary<TKey, TValue>, TKey, TValue>>((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType), (object)null);
}
}
public static Func<TKey, TValue> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
{
return dictionary.get_Item;
}
public static Func<IEnumerable<TKey>> KeysGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
{
return dictionary.get_Keys;
}
public static Func<IEnumerable<TValue>> ValuesGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
{
return (Func<IEnumerable<TValue>>)new Func<IEnumerable<TKey>>(dictionary.get_Values);
}
public static Func<TKey, TValue> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary.get_Item;
}
public static Action<TKey, TValue> IndexerSetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary.set_Item;
}
public static Func<ICollection<TKey>> KeysGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary.get_Keys;
}
public static Func<ICollection<TValue>> ValuesGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary)
{
return (Func<ICollection<TValue>>)new Func<ICollection<TKey>>(dictionary.get_Values);
}
public static TValue GetOrAdd<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : new
{
if (!dictionary.TryGetValue(key, out TValue value))
dictionary.Add(key, value = new TValue());
return value;
}
public static TValue GetOrAdd<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary.TryGetValue(key, out TValue value2))
value = value2;
else
dictionary.Add(key, value);
return value;
}
public static TValue GetOrAdd<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<TKey, TValue> valueFactory)
{
if (dictionary.TryGetValue(key, out TValue value))
return value;
value = valueFactory.Invoke(key);
dictionary.Add(key, value);
return value;
}
public static TValue GetOrAdd<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)
{
ValueFunc<TKey, TValue> valueFactory2 = new ValueFunc<TKey, TValue>(valueFactory, true);
return dictionary.GetOrAdd(key, ref valueFactory2);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static void ForEach<TKey, TValue>([System.Runtime.CompilerServices.Nullable(1)] this IDictionary<TKey, TValue> dictionary, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueAction<TKey, TValue> action)
{
foreach (KeyValuePair<TKey, TValue> item in (IEnumerable<KeyValuePair<TKey, TValue>>)dictionary) {
item.Deconstruct(out TKey key, out TValue value);
TKey arg = key;
TValue arg2 = value;
action.Invoke(arg, arg2);
}
}
public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
{
ValueAction<TKey, TValue> action2 = new ValueAction<TKey, TValue>(action, true);
dictionary.ForEach(ref action2);
}
public static TValue GetOrInvoke<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})] ref ValueFunc<TValue> defaultValue)
{
if (!dictionary.TryGetValue(key, out TValue value))
return defaultValue.Invoke();
return value;
}
public static TValue GetOrInvoke<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultValue)
{
ValueFunc<TValue> defaultValue2 = new ValueFunc<TValue>(defaultValue, true);
return dictionary.GetOrInvoke(key, ref defaultValue2);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Optional<TValue> TryGetValue<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
if (!dictionary.TryGetValue(key, out TValue value))
return Optional<TValue>.Empty;
return new Optional<TValue>(value);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Optional<TValue> TryRemove<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
if (!dictionary.TryGetValue(key, out TValue value) || !dictionary.Remove(key))
return Optional<TValue>.Empty;
return new Optional<TValue>(value);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public static Optional<TValue> TryGetValue<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
{
if (!dictionary.TryGetValue(key, out TValue value))
return Optional<TValue>.Empty;
return new Optional<TValue>(value);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})]
public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>([System.Runtime.CompilerServices.Nullable(1)] this IReadOnlyDictionary<TKey, TValue> dictionary, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] ref ValueFunc<TValue, TResult> mapper)
{
return new ReadOnlyDictionaryView<TKey, TValue, TResult>(dictionary, ref mapper);
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1,
1
})]
public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>([System.Runtime.CompilerServices.Nullable(1)] this IReadOnlyDictionary<TKey, TValue> dictionary, [System.Runtime.CompilerServices.Nullable(1)] Converter<TValue, TResult> mapper)
{
ValueFunc<TValue, TResult> mapper2 = mapper.AsValueFunc(true);
return dictionary.ConvertValues(ref mapper2);
}
}
}