程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 構建可反轉排序的泛型字典類(8)--實現IDictionary接口

構建可反轉排序的泛型字典類(8)--實現IDictionary接口

編輯:關於C#

8. 實現IDictionary<TKey, TValue>接口

由於前面實現了 IDictionary接口,現在實現IDictionary<TKey, TValue>也就沒什麼困難 的了,照葫蘆畫瓢。

首先改變類聲明:

public class ReversibleSortedList<TKey, TValue> :IDictionary<TKey, TValue>
  IDictionary, IEnumerable<KeyValuePair<TKey, TValue>>, ICollection,
IEnumerable, ICollection<KeyValuePair<TKey, TValue>>

然後 實現ICollection<KeyValuePair<TKey, TValue>>接口成員:

bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
  {
    get
     {
      return false;
    }
  }
   void ICollection<KeyValuePair<TKey, TValue>>.Add(
     KeyValuePair<TKey, TValue> keyValuePair)
  {
    this.Add(keyValuePair.Key, keyValuePair.Value);
  }
  bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
    KeyValuePair<TKey, TValue> keyValuePair)
  {
    int num1 = this.IndexOfKey (keyValuePair.Key);
    if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
      keyValuePair.Value))
    {
       return true;
    }
    return false;
  }
  void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (
    KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  {
    if (array == null)
     {
      throw new ArgumentNullException ("array");
    }
    if ((arrayIndex < 0) || (arrayIndex > array.Length))
    {
       throw new ArgumentOutOfRangeException(
          "arrayIndex", "Need a non-negative number");
    }
    if ((array.Length - arrayIndex) < this.Count)
    {
      throw new ArgumentException("ArrayPlusOffTooSmall");
    }
    for (int num1 = 0; num1 < this.Count; num1++)
     {
      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)
  {
    int num1 = this.IndexOfKey(keyValuePair.Key);
     if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
       this.values[num1], keyValuePair.Value))
    {
       this.RemoveAt(num1);
      return true;
    }
    return false;
}

需要注意,Count屬性和Clear 方法都是共用方法,不需要再次實現。

接下來實現 IDictionary<TKey, TValue>接口,它是最主要的接口,所有成員均為公 有的,前面我們已經實現了Add、ContainsKey方法,這裡不再列出:

ICollection<TKey> IDictionary<TKey, TValue>.Keys
  {
    get
    {
       return this.GetKeyListHelper();
    }
  }
  ICollection<TValue> IDictionary<TKey, TValue>.Values
  {
    get
    {
       return this.GetValueListHelper();
    }
  }
  public TValue this[TKey key] //Item屬性,就是索引器
  { 
    get
    {
      TValue local1;
      int num1 = this.IndexOfKey(key);
      if (num1 >= 0)
      {
        return this.values [num1];
      }
      else
      {
        local1 = default(TValue);
         return local1;
      }
    }
    set
    {
      if (key == null)
      {
        throw new ArgumentNullException("key");
      }
      int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
         this._sortDirectionComparer);
      if (num1 >= 0)
      {
        this.values[num1] = value;
        this.version++;
      }
       else
      {
        this.Insert (~num1, key, value);
      }
    }
  }
  public bool Remove(TKey key)
  {
    int num1 = this.IndexOfKey(key);
    if (num1 >= 0)
    {
      this.RemoveAt(num1);
    }
    return (num1 >= 0);
  }
  public bool TryGetValue(TKey key, out TValue value)
  {
    int num1 = this.IndexOfKey (key);
    if (num1 >= 0)
    {
       value = this.values[num1];
      return true;
     }
    value = default(TValue);
    return false;
  }

另外需要添加一個RemoveAt方法:

  // 移除指定索引元素
  public void RemoveAt(int index)
   {
    if ((index < 0) || (index >= this._size))
     {
      throw new ArgumentOutOfRangeException ("index", "Index out of range");
    }
    this._size--;
    if (index < this._size)
    {
      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++;
  }

最後,進行測試,這一次 不再使用int做為鍵,而改用string:

static void Main()
  {
    ReversibleSortedList<string, string> rs = new ReversibleSortedList<string, string>();
    //添加元素
    rs.Add("3", "a");
     rs.Add("1", "b");
    rs.Add ("2", "c");
    rs.Add("6", "d");
    rs.Add("5", "e");
    rs.Add("4", "f");
    //使用 DictionaryEntry打印鍵/值
    foreach (KeyValuePair<string, string> d in rs)
    {
      Console.WriteLine (d.Key + "  " + d.Value);
    }
     Console.WriteLine("刪除索引為“2”的“5”的元 素");
    rs.Remove("2");
     rs.Remove("5");
    foreach (KeyValuePair<string, string> d in rs)
    {
       Console.WriteLine(d.Key + "  " + d.Value);
     }
}

ReversibleSortedList 0.7版本:實現 IDictionary<TKey, TValue>接口

運行結果:

1  b
2  c
3  a
4  f
5  e
6  d

刪除索引為“2”的“5”的元素

1  b
3  a
4  f
6  d

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved