OrderedDictionary<TKey, TValue>
sealed class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
using Renci.SshNet.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Renci.SshNet
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class OrderedDictionary<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue> : IOrderedDictionary<TKey, TValue>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
0,
1
})]
private sealed class KeyCollection : KeyOrValueCollection<TKey>
{
public KeyCollection(OrderedDictionary<TKey, TValue> orderedDictionary)
: base(orderedDictionary)
{
}
public override bool Contains(TKey item)
{
return base.OrderedDictionary._dictionary.ContainsKey(item);
}
public override void CopyTo(TKey[] array, int arrayIndex)
{
base.CopyTo(array, arrayIndex);
foreach (KeyValuePair<TKey, TValue> item in base.OrderedDictionary._list) {
array[arrayIndex++] = item.Key;
}
}
public override IEnumerator<TKey> GetEnumerator()
{
return Enumerable.Select<KeyValuePair<TKey, TValue>, TKey>((IEnumerable<KeyValuePair<TKey, TValue>>)base.OrderedDictionary._list, (Func<KeyValuePair<TKey, TValue>, TKey>)((KeyValuePair<TKey, TValue> kvp) => kvp.Key)).GetEnumerator();
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
0,
0,
1
})]
private sealed class ValueCollection : KeyOrValueCollection<TValue>
{
public ValueCollection(OrderedDictionary<TKey, TValue> orderedDictionary)
: base(orderedDictionary)
{
}
public override bool Contains(TValue item)
{
return base.OrderedDictionary._dictionary.ContainsValue(item);
}
public override void CopyTo(TValue[] array, int arrayIndex)
{
base.CopyTo(array, arrayIndex);
foreach (KeyValuePair<TKey, TValue> item in base.OrderedDictionary._list) {
array[arrayIndex++] = item.Value;
}
}
public override IEnumerator<TValue> GetEnumerator()
{
return Enumerable.Select<KeyValuePair<TKey, TValue>, TValue>((IEnumerable<KeyValuePair<TKey, TValue>>)base.OrderedDictionary._list, (Func<KeyValuePair<TKey, TValue>, TValue>)((KeyValuePair<TKey, TValue> kvp) => kvp.Value)).GetEnumerator();
}
}
[System.Runtime.CompilerServices.Nullable(0)]
private abstract class KeyOrValueCollection<[System.Runtime.CompilerServices.Nullable(2)] T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
protected OrderedDictionary<TKey, TValue> OrderedDictionary { get; }
public int Count => OrderedDictionary.Count;
public bool IsReadOnly => true;
protected KeyOrValueCollection(OrderedDictionary<TKey, TValue> orderedDictionary)
{
OrderedDictionary = orderedDictionary;
}
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public abstract bool Contains(T item);
public virtual void CopyTo(T[] array, int arrayIndex)
{
ThrowHelper.ThrowIfNull(array, "array");
ThrowHelper.ThrowIfNegative(arrayIndex, "arrayIndex");
if (array.Length - arrayIndex < Count)
throw new ArgumentException("Destination array was not long enough. Check the destination index, length, and the array's lower bounds.", "array");
}
public abstract IEnumerator<T> GetEnumerator();
public bool Remove(T item)
{
throw new NotSupportedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
private readonly Dictionary<TKey, TValue> _dictionary;
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1
})]
private readonly List<KeyValuePair<TKey, TValue>> _list;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
0
})]
private KeyCollection _keys;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
0
})]
private ValueCollection _values;
public TValue this[TKey key] {
get {
return _dictionary[key];
}
set {
if (_dictionary.TryAdd(key, value))
_list.Add(new KeyValuePair<TKey, TValue>(key, value));
else {
_dictionary[key] = value;
_list[IndexOf(key)] = new KeyValuePair<TKey, TValue>(key, value);
}
}
}
public ICollection<TKey> Keys => _keys ?? (_keys = new KeyCollection(this));
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
get {
return Keys;
}
}
public ICollection<TValue> Values => _values ?? (_values = new ValueCollection(this));
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
get {
return Values;
}
}
public int Count => _list.Count;
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
get {
return false;
}
}
public OrderedDictionary([System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] EqualityComparer<TKey> comparer = null)
{
_dictionary = new Dictionary<TKey, TValue>(comparer);
_list = new List<KeyValuePair<TKey, TValue>>();
}
[Conditional("DEBUG")]
private void AssertConsistency()
{
foreach (KeyValuePair<TKey, TValue> item in _list) {
KeyValuePair<TKey, TValue> keyValuePair = item;
}
foreach (KeyValuePair<TKey, TValue> item2 in _dictionary) {
EnumeratingIndexOf(item2.Key);
}
}
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
_list.Add(new KeyValuePair<TKey, TValue>(key, value));
}
void ICollection<KeyValuePair<TKey, TValue>>.Add([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
public void Clear()
{
_dictionary.Clear();
_list.Clear();
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
}
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
public bool ContainsValue(TValue value)
{
return _dictionary.ContainsValue(value);
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1
})] KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})]
public KeyValuePair<TKey, TValue> GetAt(int index)
{
return _list[index];
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
1
})]
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int IndexOf(TKey key)
{
if (!_dictionary.ContainsKey(key))
return -1;
return EnumeratingIndexOf(key);
}
private int EnumeratingIndexOf(TKey key)
{
int num = -1;
foreach (KeyValuePair<TKey, TValue> item in _list) {
num++;
if (_dictionary.Comparer.Equals(key, item.Key))
return num;
}
return -1;
}
public void Insert(int index, TKey key, TValue value)
{
if ((uint)index > Count)
throw new ArgumentOutOfRangeException("index");
_dictionary.Add(key, value);
_list.Insert(index, new KeyValuePair<TKey, TValue>(key, value));
}
public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value)
{
if (_dictionary.Remove(key, out value)) {
_list.RemoveAt(EnumeratingIndexOf(key));
return true;
}
value = default(TValue);
return false;
}
public bool Remove(TKey key)
{
if (_dictionary.Remove(key)) {
_list.RemoveAt(EnumeratingIndexOf(key));
return true;
}
return false;
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
1
})] KeyValuePair<TKey, TValue> item)
{
if (((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Remove(item)) {
_list.RemoveAt(EnumeratingIndexOf(item.Key));
return true;
}
return false;
}
public void RemoveAt(int index)
{
TKey key = _list[index].Key;
_list.RemoveAt(index);
_dictionary.Remove(key);
}
public void SetAt(int index, TKey key, TValue value)
{
if ((uint)index >= Count)
throw new ArgumentOutOfRangeException("index");
if (TryGetValue(key, out TValue _, out int index2)) {
if (index != index2)
throw new ArgumentException("An item with the same key has already been added", "key");
} else
_dictionary.Remove(_list[index].Key);
_dictionary[key] = value;
_list[index] = new KeyValuePair<TKey, TValue>(key, value);
}
public void SetAt(int index, TValue value)
{
TKey key = _list[index].Key;
_list[index] = new KeyValuePair<TKey, TValue>(key, value);
_dictionary[key] = value;
}
public void SetPosition(int index, int newIndex)
{
if ((uint)newIndex >= Count)
throw new ArgumentOutOfRangeException("newIndex");
KeyValuePair<TKey, TValue> item = _list[index];
_list.RemoveAt(index);
_list.Insert(newIndex, item);
}
public void SetPosition(TKey key, int newIndex)
{
TValue val = _dictionary[key];
int index = EnumeratingIndexOf(key);
SetPosition(index, newIndex);
}
public bool TryAdd(TKey key, TValue value)
{
if (_dictionary.TryAdd(key, value)) {
_list.Add(new KeyValuePair<TKey, TValue>(key, value));
return true;
}
return false;
}
public bool TryAdd(TKey key, TValue value, out int index)
{
if (_dictionary.TryAdd(key, value)) {
_list.Add(new KeyValuePair<TKey, TValue>(key, value));
index = _list.Count - 1;
return true;
}
index = EnumeratingIndexOf(key);
return false;
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dictionary.TryGetValue(key, out value);
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value, out int index)
{
if (_dictionary.TryGetValue(key, out value)) {
index = EnumeratingIndexOf(key);
return true;
}
index = -1;
return false;
}
}
}