6. 实现IDictionary接口中的Keys和Values属性
现在我们可以着眼于IDictionary接口的实现。第4节中,专门针对这个接口做了一个最简化的例子,我们来回顾一下,它是怎么实现IDictionary接口中的Keys和Values属性的。
public ICollection Keys

{ //返回所有键的集合
get

{ //把所有键的集合拷贝到新数组中并返回
Object[] keys = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
keys[n] = items[n].Key;
return keys;
}
}
public ICollection Values

{ //返回所有值的集合
get

{ //把所有值的集合拷贝到新数组中并返回
Object[] values = new Object[ItemsInUse];
for (Int32 n = 0; n < ItemsInUse; n++)
values[n] = items[n].Value;
return values;
}
}

可以很清楚地看到,它把数组里的所有元素拷贝到另一块内存空间中并返回,这再一次带来了性能问题,如果频繁地访问Keys和Values属性还会给垃圾回收带来压力。最好的解决办法当然是直接引用而不是拷贝数组里的元素,你还希望增加一些功能,可以使用索引访问Keys属性或Values属性所返回的ICollection。但从第5节中的图2(最好直接下载下来以方便观看)中可以看到ICollection接口只有寥寥几个成员,并没有Item属性,怎么办呢?当然是从ICollection的子接口中寻找合适的接口了。我们知道,ICollection接口是集合接口的基接口,而它的子接口则是更专用的集合接口,如IDictionary表示带有键\值对的集合,IList表示值的集合,它们都可以按索引访问。
所以这一次你决定另外实现公有的Keys和Values属性,并返回一个ILst<T>接口,并手动实现它,一方面满足所有的功能,另一方面也可以实现IDictionary和IDictionary<TKey, TValue>接口的Keys和Values属性。好,先来看看ILst<T>接口的关系图:
图4 IList<T>接口关系图
可以看到,实现它并不简单,好在我们将屏蔽所有对元素进行改动的功能。当然,首先还是要要实现一个枚举器(感觉又回到了第2节:http://cgbluesky.blog.163.com/blog/static/241235582008113103320661/ ),不同的地方在于,这次枚举器跟调用类的关系不再是嵌套关系,它们处于同一层次,都是ReversibleSortedList的嵌套类。另外由于引用外部类集合,这里也不需要考虑反向排序的问题。
下面是Keys属性枚举时所需要的枚举器:

ReversibleSortedListKeyEnumerator#region ReversibleSortedListKeyEnumerator
private sealed class ReversibleSortedListKeyEnumerator :
IEnumerator<TKey>, IDisposable, IEnumerator

{
// 成员变量
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TKey currentKey; //记录当前值
private int index; //记录当前索引
private int version; //记录此类创建时,外部类的版本号
//构造方法
internal ReversibleSortedListKeyEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)

{ //传递外部类元素所在集合的引用
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
public void Dispose()

{
this.index = 0;
this.currentKey = default(TKey);
}
public bool MoveNext()

{
if (this.version != this._ReversibleSortedList.version)

{
throw new InvalidOperationException(
"枚举版本检查错误");
}
if (this.index < this._ReversibleSortedList.Count)

{
this.currentKey = this._ReversibleSortedList.keys[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentKey = default(TKey);
return false;
}
void IEnumerator.Reset()

{
if (this.version != this._ReversibleSortedList.version)

{
throw new InvalidOperationException(
"枚举版本检查错误");
}
this.index = 0;
this.currentKey = default(TKey);
}
// 属性
public TKey Current

{
get

{
return this.currentKey;
}
}
object IEnumerator.Current

{
get

{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))

{
throw new InvalidOperationException(
"不能进行枚举操作");
}
return this.currentKey;
}
}
}
#endregion

同理,Values属性枚举时所需要的枚举器代码类似:

ReversibleSortedListValueEnumerator#region ReversibleSortedListValueEnumerator
private sealed class ReversibleSortedListValueEnumerator :
IEnumerator<TValue>, IDisposable, IEnumerator

{
// 成员变量
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TValue currentValue;
private int index;
private int version;
internal ReversibleSortedListValueEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)

{
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
public void Dispose()

{
this.index = 0;
this.currentValue = default(TValue);
}
public bool MoveNext()

{
if (this.version != this._ReversibleSortedList.version)

{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)

{
this.currentValue = this._ReversibleSortedList.values[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentValue = default(TValue);
return false;
}
void IEnumerator.Reset()

{
if (this.version != this._ReversibleSortedList.version)

{
throw new InvalidOperationException(
"Enumeration failed version check");
}
this.index = 0;
this.currentValue = default(TValue);
}
// 属性
public TValue Current

{
get

{
return this.currentValue;
}
}
object IEnumerator.Current

{
get

{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))

{
throw new InvalidOperationException(
"Enumeration operation could not occur");
}
return this.currentValue;
}
}
}
#endregion

枚举器实现了,接下来实现Keys所需要的IList<T>。注意,这里通过弹出异常屏蔽了所有企图对元素进行修改的操作。

KeyListK,V#region KeyList<K,V>
private sealed class KeyList<K, V> : IList<K>, ICollection<K>,
IEnumerable<K>, ICollection, IEnumerable

{
// 成员变量
private ReversibleSortedList<K, V> _dict;
//成员方法
internal KeyList(ReversibleSortedList<K, V> dictionary)

{
this._dict = dictionary;
}
public void Add(K key)

{
throw new NotSupportedException("不支持Add操作");
}
public void Clear()

{
throw new NotSupportedException("不支持Clear操作");
}
public bool Contains(K key)

{
return this._dict.ContainsKey(key);
}
public void CopyTo(K[] array, int arrayIndex)

{
Array.Copy(this._dict.keys, 0, array, arrayIndex, this._dict.Count);
}
public IEnumerator<K> GetEnumerator()

{
return new
ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
this._dict);
}
public int IndexOf(K key)

{
if (key == null)

{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<K>(this._dict.keys, 0,
this._dict.Count, key,
this._dict._sortDirectionComparer);
if (num1 >= 0)

{
return num1;
}
return -1;
}
public void Insert(int index, K value)

{
throw new NotSupportedException("<