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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » DictionaryBaseScreenshots 
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 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.