Two Key Dictionary : DictionaryBase « Collections Data Structure « C# / C Sharp






Two Key Dictionary

  
//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
using System.Collections.Generic;

namespace Isotope.Collections
{
    public class TwoKeyDictionary<PK, SK, V>
    {
        Dictionary<PK, Dictionary<SK, V>> dic_pk;

        public TwoKeyDictionary()
        {
            this.dic_pk = new Dictionary<PK, Dictionary<SK, V>>();
        }

        public bool ContainsPrimaryKey(PK pk)
        {
            return this.dic_pk.ContainsKey(pk);
        }

        public bool ContainsKey(PK pk, SK sk)
        {
            V v;
            bool haskey = this.TryGetValue(pk, sk, out v);
            return haskey;
        }

        public bool TryGetValue(PK pk, SK sk, out V v)
        {
            Dictionary<SK, V> sk_dic;
            bool has_pk = this.dic_pk.TryGetValue(pk, out sk_dic);
            if (!has_pk)
            {
                v = default(V);
                return false;
            }

            return sk_dic.TryGetValue(sk, out v);
        }

        public V GetValue(PK pk, SK sk)
        {
            V v;
            bool haskey = this.TryGetValue(pk, sk, out v);
            if (!haskey)
            {
                string msg = string.Format("(pk,sk) missing");
                throw new KeyNotFoundException(msg);
            }

            return v;
        }

        public void SetValue(PK pk, SK sk, V v)
        {
            Dictionary<SK, V> sk_dic;
            bool has_pk = this.dic_pk.TryGetValue(pk, out sk_dic);
            if (!has_pk)
            {
                sk_dic = new Dictionary<SK, V>();
                this.dic_pk[pk] = sk_dic;
            }

            sk_dic[sk] = v;
        }

        public V this[PK pk, SK sk]
        {
            get
            {
                return this.GetValue(pk, sk);
            }

            set
            {
                this.SetValue(pk, sk, value);
            }
        }

        public int Count
        {
            get
            {
                int n = 0;
                foreach (var i in this.dic_pk.Values)
                {
                    n += i.Count;
                }

                return n;
            }
        }
    }
}

   
    
  








Related examples in the same category

1.extends DictionaryBase
2.Add a list of items into the dictionary.
3.Copy/Clone all the element from a dictionary to another.
4.Put an element into the Dictionary
5.Add one dictionary items into another. Copy all the first dictionary's items into the second dictionary.
6.A dictionary with keys of type string and values of type String
7.A dictionary with keys of type string and values of type Type
8.Dictionary to anonymous type
9.DictionaryBase Class Provides the abstract base class for a strongly typed collection of key/value pairs.