定制Dictionary

Posted on 2011-12-14 10:49  leon_ALiang  阅读(212)  评论(0编辑  收藏  举报

在日常开发中,经常会碰到对Dictionary的大量操作,判断等。当然可以用Dictionary本省,也可以重写一个符合自己逻辑处理的Dictionary.

public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
        IList<TKey> keys = new List<TKey>();
        IList<TValue> values = new List<TValue>();

        public MyDictionary() 
        {
        }

        public MyDictionary(IDictionary<TKey, TValue> dictionary)
        {
            foreach (var key in dictionary.Keys)
            {
                this[(TKey)key] = (TValue)dictionary[key];
            }
        }

        public ICollection<TKey> keySet()
        {
            return this.Keys;
        }

        public bool IsEmpty()
        {
            return (this.Count == 0);
        }

        #region MyDictionary<TKey,TValue> Members

        public void Add(TKey key, TValue value)
        {
            keys.Add(key);
            values.Add(value);
        }

        public bool ContainsKey(TKey key)
        {
            return keys.Contains(key);
        }

        public ICollection<TKey> Keys
        {
            get { return new ReadOnlyCollection<TKey>(keys); }
        }

        public bool Remove(TKey key)
        {
            int removeLocation = keys.IndexOf(key);
            if (removeLocation < 0)
                return false;

            values.RemoveAt(removeLocation);
            keys.RemoveAt(removeLocation);
            return true;
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            if (keys.Contains(key))
            {
                value = values[keys.IndexOf(key)];
                return true;
            }
            value = default(TValue);
            return false;
        }

        public ICollection<TValue> Values
        {
            get { return new ReadOnlyCollection<TValue>(values); }
        }

        public TValue this[TKey key]
        {
            get
            {
                if (values.Count <= 0)
                    return default(TValue);
                else
                {
                    int nIndex = keys.IndexOf(key);
                    if (nIndex < 0)
                        return default(TValue);
                    else
                        return values[nIndex];
                }
            }
            set
            {
                if (keys.Contains(key))
                    values[keys.IndexOf(key)] = value;
                else
                    Add(key, value);
            }
        }

        #endregion

        #region ICollection<KeyValuePair<TKey,TValue>> Members

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            Add(item.Key, item.Value);
        }

        public void Clear()
        {
            keys.Clear();
            values.Clear();
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            if (keys.Contains(item.Key))
                return (this[item.Key].Equals(item.Value));
            return false;
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array was null");
            }
            if ((arrayIndex < 0) || (arrayIndex > array.Length))
            {
                throw new ArgumentOutOfRangeException("arrayIndex");
            }
            if ((array.Length - arrayIndex) < this.Count)
            {
                throw new ArgumentException("Array plus offset is too small");
            }

            foreach (KeyValuePair<TKey, TValue> keyValue in this)
            {
                array[arrayIndex++] = keyValue;
            }

        }

        public int Count
        {
            get { return keys.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            if (keys.Contains(item.Key) && values[keys.IndexOf(item.Key)].Equals(item.Value))
            {
                Remove(item.Key);
                return true;
            }
            return false;
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            int index = 0;
            foreach (TKey key in keys)
                yield return new KeyValuePair<TKey, TValue>(key, values[index++]);
        }

        #endregion

        #region IEnumerable Members

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.Keys.GetEnumerator();
        }

        #endregion
    }

 

Copyright © 2024 leon_ALiang
Powered by .NET 9.0 on Kubernetes