Key可以相同,而value不同的集合利用Dictionary<TKey,IList<TValue>>的一个实现

using System;
using System.Collections.Generic;
using System.Text;
------------IVisitableDictionary<TKey, TValue>接口------------
namespace DataStructures
{
    public interface IVisitableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IComparable, IVisitableCollection<KeyValuePair<TKey, TValue>>
    {
        
    }
}


--------IVisitableHashtable<TKey, TValue>的实现----------
using System;
using System.Collections.Generic;
using System.Text;
using NGenerics;
using System.Runtime.Serialization;
using NGenerics.Visitors;

namespace DataStructures
{
    [Serializable]
    public class VisitableHashtable<TKey, TValue> : Dictionary<TKey, TValue>, IVisitableDictionary<TKey, TValue>
    {
        #region Construction
       
        public VisitableHashtable() : base() { }

        public VisitableHashtable(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }

        public VisitableHashtable(IEqualityComparer<TKey> comparer) : base(comparer) { }


        public VisitableHashtable(int capacity) : base(capacity) { }

        public VisitableHashtable(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }

        public VisitableHashtable(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }


        protected VisitableHashtable(SerializationInfo info, StreamingContext context) : base(info, context) { }
       
        #endregion

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

        public void Accept(IVisitor<KeyValuePair<TKey, TValue>> visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            Dictionary<TKey, TValue>.Enumerator enumerator = this.GetEnumerator();

            while (enumerator.MoveNext())
            {
                visitor.Visit(enumerator.Current);

                if (visitor.HasCompleted)
                {
                    break;
                }
            }
        }

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

        public bool IsFull
        {
            get
            {
                return false;
            }
        }

        public bool IsFixedSize
        {
            get {
                return false;
            }
        }
               
        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (obj.GetType() == this.GetType())
            {
                VisitableHashtable<TKey, TValue> h = obj as VisitableHashtable<TKey, TValue>;
                return this.Count.CompareTo(h.Count);
            }
            else
            {
                return this.GetType().FullName.CompareTo(obj.GetType().FullName);
            }
        }

        #endregion
    }
}
----接受相同主键对应多值的KeyValuePair泛型集合类------------------
using System;
using System.Collections.Generic;
using System.Text;
using NGenerics.Misc;
using System.Runtime.Serialization;

namespace DataStructures
{
    /// <summary>
    /// 接受相同主键对应多值的KeyValuePair泛型集合类
    /// </summary>
    [Serializable]
    public sealed class HashList<TKey, TValue> : VisitableHashtable<TKey, IList<TValue>>
    {
        #region Globals


        #endregion

        #region Construction

        public HashList() : base() { }

        public HashList(IDictionary<TKey, IList<TValue>> dictionary) : base(dictionary) { }

        public HashList(IEqualityComparer<TKey> comparer) : base(comparer) { }

        public HashList(int capacity) : base(capacity) { }

        public HashList(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }

        private HashList(SerializationInfo info, StreamingContext context) : base(info, context) { }

        #endregion

        #region Public Members

        public int ValueCount
        {
            get
            {
                int count = 0;

                using (Dictionary<TKey, IList<TValue>>.Enumerator enumerator = this.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        if (enumerator.Current.Value != null)
                        {
                            count += enumerator.Current.Value.Count;
                        }
                    }
                }

                return count;
            }
        }

        public int KeyCount
        {
            get
            {
                return this.Count;
            }
        }

        public IEnumerator<TValue> GetValueEnumerator()
        {
            Dictionary<TKey, IList<TValue>>.Enumerator enumerator = base.GetEnumerator();
           
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Value != null)
                {
                    for (int i = 0; i < enumerator.Current.Value.Count; i++)
                    {
                        yield return enumerator.Current.Value[i];
                    }
                }
            }
        }
        public void Add(TKey key, TValue value)
        {
            IList<TValue> list;

            if (this.ContainsKey(key))
            {
                list = this[key];

                if (list == null)
                {
                    list = new List<TValue>();
                    this[key] = list;
                }
            }
            else
            {
                list = new List<TValue>();
                this[key] = list;
            }

            list.Add(value);
        }
        public void Add(TKey key, ICollection<TValue> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            IList<TValue> list;

            if (this.ContainsKey(key))
            {
                list = this[key];

                if (list == null)
                {
                    list = new List<TValue>();
                    this[key] = list;
                }
            }
            else
            {
                list = new List<TValue>();
                this[key] = list;
            }

            ((List<TValue>)list).AddRange(values);
        }

        public bool Remove(TValue item)
        {
            Dictionary<TKey, IList<TValue>>.KeyCollection dictKeys = this.Keys;
            IList<TKey> keys = new List<TKey>(dictKeys);

            for (int i = 0; i < keys.Count; i++)
            {
                IList<TValue> values = this[keys[i]];

                if (values != null)
                {
                    if (values.Remove(item)) {
                        return true;
                    }
                }
            }

            return false;
        }

        public void RemoveAll(TValue item)
        {
            Dictionary<TKey, IList<TValue>>.KeyCollection dictKeys = this.Keys;
            IList<TKey> keys = new List<TKey>(dictKeys);

            for (int i = 0; i < keys.Count; i++)
            {
                IList<TValue> values = this[keys[i]];

                if (values != null)
                {
                    for (int j = 0; j < values.Count; j++)
                    {
                        if (values[j].Equals(item))
                        {
                            values.RemoveAt(j);
                            j--;
                        }
                    }                   
                }
            }
        }


        public bool Remove(TKey key, TValue item)
        {
            if (!this.ContainsKey(key))
            {
                return false;
            }
            else
            {
                IList<TValue> values = this[key];

                if (values != null)
                {
                    return values.Remove(item);
                }
                else
                {
                    return false;
                }
            }
        }

        #endregion
    }
}

posted on 2008-06-24 15:12  花生1  阅读(1636)  评论(0编辑  收藏  举报

导航