• 构建可反转排序的泛型字典类(8)实现IDictionary接口


     

    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>接口,它是最主要的接口,所有成员均为公有的,前面我们已经实现了AddContainsKey方法,这里不再列出:

        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, 0this._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
    <stringstring> rs = new ReversibleSortedList<stringstring>();
            
    //添加元素
            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<stringstring> d in rs)
            {
                Console.WriteLine(d.Key 
    + "    " + d.Value);
            }
            Console.WriteLine(
    "删除索引为“2”的“5”的元素");
            rs.Remove(
    "2");
            rs.Remove(
    "5");
            
    foreach (KeyValuePair<stringstring> 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


  • 相关阅读:
    数组操作方法和迭代方法
    三元运算符
    数组求和/去重
    javascript保留字
    window.onload和document.ready区别
    alert()和consloe.log()区别
    Eventutil函数封装
    前端中的事件流
    react的生命周期
    小程序初体验
  • 原文地址:https://www.cnblogs.com/abatei/p/1073221.html
Copyright © 2020-2023  润新知