上面兩個IList<T>接口代碼調用了外部類的一些方法,需要把它們添加到ReversibleSortedList 類中:
公有方法如下:
//查找指定鍵索引
public int IndexOfKey(TKey key)
{
if (key.Equals(null))
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 < 0)
{
return -1;
}
return num1;
}
//查找指定值索引
public int IndexOfValue(TValue value)
{
return Array.IndexOf<TValue>(this.values, value, 0, this._size);
}
//判斷是否包含指定鍵
public bool ContainsKey(TKey key)
{
return (this.IndexOfKey(key) >= 0);
}
// 判斷是否包含指定值
public bool ContainsValue(TValue value)
{
return (this.IndexOfValue(value) >= 0);
}
私有方法如下:
private TValue GetByIndex(int index)
{
if ((index < 0) || (index >= this._size))
{
throw new ArgumentOutOfRangeException ("index", "Index out of range");
}
return this.values[index];
}
//返回指定索引的鍵
private TKey GetKey(int index)
{
if ((index < 0) || (index >= this._size))
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
return this.keys[index];
}
private void Insert(int index, TKey key, TValue value)
{ //在指定索引入插入數據
if (this._size == this.keys.Length)
{
this.EnsureCapacity(this._size + 1);
}
if (index < this._size)
{ //當 插入元素不是添加在未尾時,移動插入點後面的元素
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++;
}
private KeyList<TKey, TValue> GetKeyListHelper()
{ //獲取鍵的IList<T>集合
if (this.keyList == null)
{
this.keyList = new KeyList<TKey, TValue>(this);
}
return this.keyList;
}
private ValueList<TKey, TValue> GetValueListHelper()
{ //獲取值的IList<T>集合
if (this.valueList == null)
{
this.valueList = new ValueList<TKey, TValue>(this);
}
return this.valueList;
}