4.8 反转Sorted List里的内容
问题
您希望在数组和列表类型中可以反转sorted list里的内容同时又维持SortedList和SortedList<T>类原来的功能。无论是SortedList还是泛型SortedList<T>类都直接提供了完成这个功能的方法而又不需要重填列表。
解决方案
ReversibleSortedList<TKey, TValue>类提供了这些功能,它基于SortedList<TKey, TValue>类,所以拥有相同的功能,它提供了额外的功能是很容易反转已排序的列表。
在实例化ReversibleSortedList<TKey, TValue>之后,键是整数类型,值是字符串类型,一连串无序的数字和它们的文本表达被插入到列表中。这些项目是这样显示的:
ReversibleSortedList<int, string> rsl = new ReversibleSortedList<int, string>();
rsl.Add(2, "2");
rsl.Add(5, "5");
rsl.Add(3, "3");
rsl.Add(1, "1");
foreach (KeyValuePair<int, string> kvp in rsl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);
}
![](/Images/OutliningIndicators/None.gif)
列表输出显示为按升序排序(默认):
1 1
2 2
3 3
5 5
现在排列顺序通过设置ReversibleSortedList的SortDirection属性被反转为降序。为了重新排序需要调用Sort()方法。结果如下:
// 转换排序方向.
rsl.Comparer.SortDirection = ListSortDirection.Descending;
// 重排列表.
rsl.Sort();
foreach (KeyValuePair<int, string> kvp in rsl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);
}
![](/Images/OutliningIndicators/None.gif)
这一次,输出为降序:
5 5
3 3
2 2
1 1
当把一个新项添加进列表,它将按当前的排列顺序被添加进去,但在添加完所有项后马上进行反转,就可以保持列表中元素的顺序。
rsl.Add(4, "4");
foreach (KeyValuePair<int, string> kvp in rsl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);
}
// 转换排序方向.
rsl.Comparer.SortDirection = ListSortDirection.Ascending;
// 重排列表.
rsl.Sort();
foreach (KeyValuePair<int, string> kvp in rsl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);
}
![](/Images/OutliningIndicators/None.gif)
可以看到新项即按降序也按升序排列:
5 5
4 4
3 3
2 2
1 1
1 1
2 2
3 3
4 4
5 5
ReversibleSortedList<TKey, TValue>包含一个实现了IComparer<T>接口的嵌套类SortDirectionComparer<T>。这个类可以在“讨论”这一节中的ReversibleSortedList<TKey, TValue>代码中看到。一个实现了IComparer<T>接口的类可以做为ReversibleSortedList<TKey, TValue>构造方法的参数来改变默认的排序。IComparer<T>接口实现了Compare方法:
class Program
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public int Compare(T lhs, T rhs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int compareResult =
lhs.ToString().CompareTo(rhs.ToString());
// 如果为降序, 则反转
if (SortDirection == ListSortDirection.Descending)
compareResult *= -1;
return compareResult;
}
}
![](/Images/OutliningIndicators/None.gif)
Compare方法使用了SortDirectionComparer<T>类的SortDirection属性来决定项的排序。这个属性在ReversibleSortedList<TKey, TValue>的内部类SortDirectionComparer<T>实例中被设置。SortDirection属性是在构造方法中被设置的,代码如下:
public ReversibleSortedList()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
this._size = 0;
this._sortDirectionComparer = new SortDirectionComparer<TKey>();
this._currentSortDirection = this._sortDirectionComparer.SortDirection;
}
![](/Images/OutliningIndicators/None.gif)
这允许它在指定时间内反转排列顺序,但并没有重排列表中已存在的项。为了实现这个功能,需要在Reversible-SortedList<TKey, TValue>类中添加一个新的Sort()方法以重排列表。代码如下:
public void Sort()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//检查是否跟现有排序方向相同.
if (this._currentSortDirection != this._sortDirectionComparer.SortDirection)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 如果不同,则进行反转.
Array.Reverse(this.keys, 0, this._size);
Array.Reverse(this.values, 0, this._size);
// 设置当前排序.
this._currentSortDirection = this._sortDirectionComparer.SortDirection;
}
}
![](/Images/OutliningIndicators/None.gif)
讨论
例4-3是ReversibleSortedList<TKey, TValue>类的所有代码:
(译者注:这个类的代码很恐怖,接近1300行,不过代码很规范,感觉应该是商业代码,非常值得借鉴。将来有时间我会专门写文章分析它。请关注:我的博客:http://cgbluesky.blog.163.com/)
例4-3 ReversibleSortedList类
[Serializable, ComVisible(false), DebuggerDisplay("Count = {Count}")]
public class ReversibleSortedList<TKey, TValue> :
IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>,
IEnumerable<KeyValuePair<TKey, TValue>>,
IDictionary, ICollection, IEnumerable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
SortDirectionComparer类定义#region SortDirectionComparer类定义
public class SortDirectionComparer<T> : IComparer<T>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //ListSortDirection 枚举,有两个值:
//Ascending按升序排列,Descending按降序排列
private System.ComponentModel.ListSortDirection _sortDir;
//构造方法
public SortDirectionComparer()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //默认为升序
_sortDir = ListSortDirection.Ascending;
}
//重载构造方法
public SortDirectionComparer(ListSortDirection sortDir)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_sortDir = sortDir;
}
//排序方向属性
public System.ComponentModel.ListSortDirection SortDirection
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _sortDir; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _sortDir = value; }
}
//实现IComparer<T>接口的方法
public int Compare(T lhs, T rhs)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int compareResult =
lhs.ToString().CompareTo(rhs.ToString());
![](/Images/OutliningIndicators/InBlock.gif)
// If order is DESC, reverse this comparison.
if (SortDirection == ListSortDirection.Descending)
compareResult *= -1;
return compareResult;
}
}
#endregion // SortDirectionComparer
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
构造方法#region 构造方法
//类型构造器
static ReversibleSortedList()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ReversibleSortedList<TKey, TValue>.emptyKeys = new TKey[0];
ReversibleSortedList<TKey, TValue>.emptyValues = new TValue[0];
}
//无参构造方法
public ReversibleSortedList()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
this._size = 0;
this._sortDirectionComparer = new SortDirectionComparer<TKey>();
this._currentSortDirection = this._sortDirectionComparer.SortDirection;
}
//用于指定排序方向的构造方法
public ReversibleSortedList(SortDirectionComparer<TKey> comparer)
: this()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (comparer != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._sortDirectionComparer = comparer;
this._currentSortDirection = _sortDirectionComparer.SortDirection;
}
}
//用于指定字典的构造方法
public ReversibleSortedList(IDictionary<TKey, TValue> dictionary)
: this(dictionary, (SortDirectionComparer<TKey>)null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
//用于指定列表容量的构造方法
public ReversibleSortedList(int capacity)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (capacity < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException(
"capacity", "Non-negative number required");
}
this.keys = new TKey[capacity];
this.values = new TValue[capacity];
this._sortDirectionComparer = new SortDirectionComparer<TKey>();
this._currentSortDirection = _sortDirectionComparer.SortDirection;
}
//用于指定字典和排序方向的构造方法
public ReversibleSortedList(IDictionary<TKey, TValue> dictionary,
SortDirectionComparer<TKey> comparer)
: this((dictionary != null) ? dictionary.Count : 0, comparer)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (dictionary == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("dictionary");
}
dictionary.Keys.CopyTo(this.keys, 0);
dictionary.Values.CopyTo(this.values, 0);
Array.Sort<TKey, TValue>(this.keys, this.values,
this._sortDirectionComparer);
this._size = dictionary.Count;
}
//用于指定容量和排序方向的构造方法
public ReversibleSortedList(int capacity, SortDirectionComparer<TKey> comparer)
: this(comparer)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Capacity = capacity;
}
#endregion //CTORS
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
公有方法#region 公有方法
//添加元素
public void Add(TKey key, TValue value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (key.Equals(null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("Attempting to add duplicate");
}
this.Insert(~num1, key, value);
}
//ICollection<KeyValuePair<TKey, TValue>>接口方法实现
public void Clear()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.version++;
Array.Clear(this.keys, 0, this._size);
Array.Clear(this.values, 0, this._size);
this._size = 0;
}
//判断是否包含指定键
public bool ContainsKey(TKey key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (this.IndexOfKey(key) >= 0);
}
//判断是否包含指定值
public bool ContainsValue(TValue value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (this.IndexOfValue(value) >= 0);
}
![](/Images/OutliningIndicators/InBlock.gif)
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
//查找指定键
public int IndexOfKey(TKey key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (key.Equals(null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return -1;
}
return num1;
}
//查找指定值
public int IndexOfValue(TValue value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Array.IndexOf<TValue>(this.values, value, 0, this._size);
}
//IDictionary<TKey, TValue>接口方法实现
public bool Remove(TKey key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.RemoveAt(num1);
}
return (num1 >= 0);
}
//移除指定索引元素
public void RemoveAt(int index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((index < 0) || (index >= this._size))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
this._size--;
if (index < this._size)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this.keys, (int)(index + 1), this.keys, index,
(int)(this._size - index));
Array.Copy(this.values, (int)(index + 1), this.values, index,
(int)(this._size - index));
}
this.keys[this._size] = default(TKey);
this.values[this._size] = default(TValue);
this.version++;
}
//排序
public void Sort()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 检查是否跟现有排序方向相同.
if (this._currentSortDirection !=
this._sortDirectionComparer.SortDirection)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 如果不同,则进行反转.
Array.Reverse(this.keys, 0, this._size);
Array.Reverse(this.values, 0, this._size);
// 设置当前排序.
this._currentSortDirection = this._sortDirectionComparer.SortDirection;
}
}
//剪除多余空间
public void TrimExcess()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = (int)(this.keys.Length * 0.9);
if (this._size < num1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Capacity = this._size;
}
}
//获取指定键的值
public bool TryGetValue(TKey key, out TValue value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
value = this.values[num1];
return true;
}
value = default(TValue);
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion // Public Methods
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
私有方法#region 私有方法
private void EnsureCapacity(int min)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = (this.keys.Length == 0) ? 4 : (this.keys.Length * 2);
if (num1 < min)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1 = min;
}
this.InternalSetCapacity(num1, false);
}
//返回指定索引的值
private TValue GetByIndex(int index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((index < 0) || (index >= this._size))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
return this.values[index];
}
//返回指定索引的键
private TKey GetKey(int index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((index < 0) || (index >= this._size))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
return this.keys[index];
}
![](/Images/OutliningIndicators/InBlock.gif)
private KeyList<TKey, TValue> GetKeyListHelper()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.keyList == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.keyList = new KeyList<TKey, TValue>(this);
}
return this.keyList;
}
![](/Images/OutliningIndicators/InBlock.gif)
private ValueList<TKey, TValue> GetValueListHelper()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.valueList == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.valueList = new ValueList<TKey, TValue>(this);
}
return this.valueList;
}
//在指定位置插入元素
private void Insert(int index, TKey key, TValue value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._size == this.keys.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.EnsureCapacity(this._size + 1);
}
if (index < this._size)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this.keys, index, this.keys, (int)(index + 1),
(int)(this._size - index));
Array.Copy(this.values, index, this.values, (int)(index + 1),
(int)(this._size - index));
}
this.keys[index] = key;
this.values[index] = value;
this._size++;
this.version++;
}
![](/Images/OutliningIndicators/InBlock.gif)
private void InternalSetCapacity(int value, bool updateVersion)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value != this.keys.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value < this._size)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException(
"value", "Too small capacity");
}
if (value > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TKey[] localArray1 = new TKey[value];
TValue[] localArray2 = new TValue[value];
if (this._size > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this.keys, 0, localArray1, 0, this._size);
Array.Copy(this.values, 0, localArray2, 0, this._size);
}
this.keys = localArray1;
this.values = localArray2;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
}
if (updateVersion)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.version++;
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private static bool IsCompatibleKey(object key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (key.Equals(null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
return (key is TKey);
}
//显式接口成员实现
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Add(keyValuePair.Key, keyValuePair.Value);
}
//显式接口成员实现
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
KeyValuePair<TKey, TValue> keyValuePair)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
keyValuePair.Value))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
return false;
}
//显式接口成员实现
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
KeyValuePair<TKey,TValue>[] array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (array == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("array");
}
if ((arrayIndex < 0) || (arrayIndex > array.Length))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException(
"arrayIndex", "Need a non-negative number");
}
if ((array.Length - arrayIndex) < this.Count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("ArrayPlusOffTooSmall");
}
for (int num1 = 0; num1 < this.Count; num1++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
KeyValuePair<TKey, TValue> pair1;
pair1 = new KeyValuePair<TKey, TValue>(
this.keys[num1], this.values[num1]);
array[arrayIndex + num1] = pair1;
}
}
//显式接口成员实现
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
KeyValuePair<TKey, TValue> keyValuePair)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
this.values[num1], keyValuePair.Value))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.RemoveAt(num1);
return true;
}
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
IEnumerator<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
//显式接口成员实现
void ICollection.CopyTo(Array array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (array == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("array");
}
if (array.Rank != 1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(
"MultiDimensional array copies are not supported");
}
if (array.GetLowerBound(0) != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("A non-zero lower bound was provided");
}
if ((arrayIndex < 0) || (arrayIndex > array.Length))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentOutOfRangeException(
"arrayIndex", "Need non negative number");
}
if ((array.Length - arrayIndex) < this.Count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("Array plus the offset is too small");
}
KeyValuePair<TKey, TValue>[] pairArray1 =
array as KeyValuePair<TKey, TValue>[];
if (pairArray1 != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int num1 = 0; num1 < this.Count; num1++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pairArray1[num1 + arrayIndex] =
new KeyValuePair<TKey, TValue>(this.keys[num1],
this.values[num1]);
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object[] objArray1 = array as object[];
if (objArray1 == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("Invalid array type");
}
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int num2 = 0; num2 < this.Count; num2++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
objArray1[num2 + arrayIndex] =
new KeyValuePair<TKey, TValue>(this.keys[num2],
this.values[num2]);
}
}
catch (ArrayTypeMismatchException)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("Invalid array type");
}
}
}
//显式接口成员实现
void IDictionary.Add(object key, object value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ReversibleSortedList<TKey, TValue>.VerifyKey(key);
ReversibleSortedList<TKey, TValue>.VerifyValueType(value);
this.Add((TKey)key, (TValue)value);
}
//显式接口成员实现
bool IDictionary.Contains(object key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ContainsKey((TKey)key);
}
return false;
}
//显式接口成员实现
IDictionaryEnumerator IDictionary.GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
//显式接口成员实现
void IDictionary.Remove(object key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Remove((TKey)key);
}
}
//显式接口成员实现
IEnumerator IEnumerable.GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static void VerifyKey(object key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (key.Equals(null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
if (!(key is TKey))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(
"Argument passed is of wrong type", "key");
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private static void VerifyValueType(object value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!(value is TValue) && ((value != null) || typeof(TValue).IsValueType))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(
"Argument passed is of wrong type", "value");
}
}
#endregion // Private methods
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Properties#region Public Properties
public int Capacity
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.keys.Length;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.InternalSetCapacity(value, true);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public SortDirectionComparer<TKey> Comparer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._sortDirectionComparer;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public int Count
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._size;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public TValue this[TKey key]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TValue local1;
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.values[num1];
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new KeyNotFoundException();
local1 = default(TValue);
return local1;
}
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (key == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.values[num1] = value;
this.version++;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Insert(~num1, key, value);
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public IList<TKey> Keys
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetKeyListHelper();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public IList<TValue> Values
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetValueListHelper();
}
}
#endregion // Public Properties
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Properties#region Private Properties
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
ICollection<TKey> IDictionary<TKey, TValue>.Keys
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetKeyListHelper();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
ICollection<TValue> IDictionary<TKey, TValue>.Values
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetValueListHelper();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
bool ICollection.IsSynchronized
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object ICollection.SyncRoot
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
bool IDictionary.IsFixedSize
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
bool IDictionary.IsReadOnly
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object IDictionary.this[object key]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int num1 = this.IndexOfKey((TKey)key);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.values[num1];
}
}
return null;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ReversibleSortedList<TKey, TValue>.VerifyKey(key);
ReversibleSortedList<TKey, TValue>.VerifyValueType(value);
this[(TKey)key] = (TValue)value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
ICollection IDictionary.Keys
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetKeyListHelper();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
ICollection IDictionary.Values
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.GetValueListHelper();
}
}
#endregion // Private properties
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Fields#region Fields
private const int _defaultCapacity = 4;
private int _size;
//private IComparer<TKey> comparer;
private static TKey[] emptyKeys;
private static TValue[] emptyValues;
private KeyList<TKey, TValue> keyList;
private TKey[] keys;
private ValueList<TKey, TValue> valueList;
private TValue[] values;
private int version;
// Declare comparison object.
private SortDirectionComparer<TKey> _sortDirectionComparer = null;
// Default to ascending.
private ListSortDirection _currentSortDirection = ListSortDirection.Descending;
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Nested Types#region Nested Types
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Enumerator K, V#region Enumerator <K, V>
[Serializable, StructLayout(LayoutKind.Sequential)]
private struct Enumerator<K, V> : IEnumerator<KeyValuePair<K, V>>, IDisposable,
IDictionaryEnumerator, IEnumerator
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private ReversibleSortedList<K, V> _ReversibleSortedList;
private K key;
private V value;
private int index;
private int version;
internal Enumerator(ReversibleSortedList<K, V> ReversibleSortedList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._ReversibleSortedList = ReversibleSortedList;
this.index = 0;
this.version = this._ReversibleSortedList.version;
this.key = default(K);
this.value = default(V);
}
public void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.index = 0;
this.key = default(K);
this.value = default(V);
}
object IDictionaryEnumerator.Key
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation cannot occur.");
}
return this.key;
}
}
public bool MoveNext()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.key = this._ReversibleSortedList.keys[this.index];
this.value = this._ReversibleSortedList.values[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.key = default(K);
this.value = default(V);
return false;
}
DictionaryEntry IDictionaryEnumerator.Entry
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation cannot happen.");
}
return new DictionaryEntry(this.key, this.value);
}
}
public KeyValuePair<K, V> Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new KeyValuePair<K, V>(this.key, this.value);
}
}
object IEnumerator.Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation cannot occur");
}
return new DictionaryEntry(this.key, this.value);
}
}
object IDictionaryEnumerator.Value
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation cannot occur");
}
return this.value;
}
}
void IEnumerator.Reset()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration version check failed");
}
this.index = 0;
this.key = default(K);
this.value = default(V);
}
}
#endregion // Enumerator <K, V>
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
KeyListK,V#region KeyList<K,V>
[Serializable]
private sealed class KeyList<K, V> : IList<K>, ICollection<K>,
IEnumerable<K>, ICollection, IEnumerable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Methods
internal KeyList(ReversibleSortedList<K, V> dictionary)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._dict = dictionary;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Add(K key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Add is unsupported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Clear()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Clear is unsupported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool Contains(K key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict.ContainsKey(key);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void CopyTo(K[] array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this._dict.keys, 0, array, arrayIndex, this._dict.Count);
}
![](/Images/OutliningIndicators/InBlock.gif)
public IEnumerator<K> GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
this._dict);
}
![](/Images/OutliningIndicators/InBlock.gif)
public int IndexOf(K key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (key == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<K>(this._dict.keys, 0,
this._dict.Count, key,
this._dict._sortDirectionComparer);
if (num1 >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return num1;
}
return -1;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Insert(int index, K value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Insert is unsupported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool Remove(K key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new NotSupportedException("Remove is unsupported");
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void RemoveAt(int index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("RemoveAt is unsupported");
}
![](/Images/OutliningIndicators/InBlock.gif)
void ICollection.CopyTo(Array array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((array != null) && (array.Rank != 1))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(
"MultiDimensional arrays are not unsupported");
}
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this._dict.keys, 0, array, arrayIndex,
this._dict.Count);
}
catch (ArrayTypeMismatchException atme)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("InvalidArrayType", atme);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
IEnumerator IEnumerable.GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
this._dict);
}
![](/Images/OutliningIndicators/InBlock.gif)
// Properties
public int Count
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict._size;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool IsReadOnly
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public K this[int index]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict.GetKey(index);
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Set is an unsupported operation");
}
}
![](/Images/OutliningIndicators/InBlock.gif)
bool ICollection.IsSynchronized
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object ICollection.SyncRoot
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Fields
private ReversibleSortedList<K, V> _dict;
}
#endregion // KeyList<K,V>
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
ReversibleSortedListKeyEnumerator definition#region ReversibleSortedListKeyEnumerator definition
[Serializable]
private sealed class ReversibleSortedListKeyEnumerator : IEnumerator<TKey>,
IDisposable,
IEnumerator
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Methods
internal ReversibleSortedListKeyEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.index = 0;
this.currentKey = default(TKey);
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool MoveNext()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.currentKey = this._ReversibleSortedList.keys[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentKey = default(TKey);
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
void IEnumerator.Reset()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
this.index = 0;
this.currentKey = default(TKey);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Properties
public TKey Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.currentKey;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object IEnumerator.Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation could not occur");
}
return this.currentKey;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Fields
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TKey currentKey;
private int index;
private int version;
}
#endregion //ReversibleSortedListKeyEnumerator definition
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
ReversibleSortedListValueEnumerator definition#region ReversibleSortedListValueEnumerator definition
[Serializable]
private sealed class ReversibleSortedListValueEnumerator : IEnumerator<TValue>,
IDisposable,
IEnumerator
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Methods
internal ReversibleSortedListValueEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.index = 0;
this.currentValue = default(TValue);
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool MoveNext()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.currentValue = this._ReversibleSortedList.values[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentValue = default(TValue);
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
void IEnumerator.Reset()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.version != this._ReversibleSortedList.version)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
this.index = 0;
this.currentValue = default(TValue);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Properties
public TValue Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.currentValue;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object IEnumerator.Current
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new InvalidOperationException(
"Enumeration operation could not occur");
}
return this.currentValue;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Fields
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TValue currentValue;
private int index;
private int version;
}
#endregion //ReversibleSortedListValueEnumerator
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
ValueList K, V definition#region ValueList <K, V> definition
[Serializable]
private sealed class ValueList<K, V> : IList<V>, ICollection<V>,
IEnumerable<V>, ICollection, IEnumerable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Methods
internal ValueList(ReversibleSortedList<K, V> dictionary)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._dict = dictionary;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Add(V key)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Add is not supported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Clear()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Clear is not supported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool Contains(V value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict.ContainsValue(value);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void CopyTo(V[] array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this._dict.values, 0, array, arrayIndex, this._dict.Count);
}
![](/Images/OutliningIndicators/InBlock.gif)
public IEnumerator<V> GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
this._dict);
}
![](/Images/OutliningIndicators/InBlock.gif)
public int IndexOf(V value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Array.IndexOf<V>(this._dict.values, value, 0, this._dict.Count);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Insert(int index, V value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Insert is not supported");
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool Remove(V value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new NotSupportedException("Remove is not supported");
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void RemoveAt(int index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("RemoveAt is not supported");
}
![](/Images/OutliningIndicators/InBlock.gif)
void ICollection.CopyTo(Array array, int arrayIndex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((array != null) && (array.Rank != 1))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(
"MultiDimensional arrays not supported");
}
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(this._dict.values, 0, array, arrayIndex,
this._dict.Count);
}
catch (ArrayTypeMismatchException atme)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException("Invalid array type", atme);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
IEnumerator IEnumerable.GetEnumerator()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
this._dict);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// Properties
public int Count
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict._size;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool IsReadOnly
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public V this[int index]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict.GetByIndex(index);
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException("Set by indexer is not supported");
}
}
![](/Images/OutliningIndicators/InBlock.gif)
bool ICollection.IsSynchronized
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
object ICollection.SyncRoot
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._dict;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// Fields
private ReversibleSortedList<K, V> _dict;
}
#endregion // ValueList <TKey, TValue> definition
#endregion // Nested types
}
![](/Images/OutliningIndicators/None.gif)
在SortedList混合使用了数组和列表语法,这使得以任一方式访问数据变得非常容易。ReversibleSortedList<T>中数据也可以使用键/值对或索引来访问。和SortedList一样,它不允许重复键。另外不管值是引用类型还是值类型都可以为null,但键不行。ReversibleSortedList<T>的默认容量是16,这和SortedList是一样的。里面的项可以使用foreach循环进行迭代,它返回KeyValuePair,但这是只读的,迭代语法禁止在读取列表时进行元素的更新或删除,否则就是无效的迭代器。
阅读参考
查看秘诀6.3和MSDN文档中的“Generic KeyValuePairStructure”和“Generic SortedList”主题。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步