aboutsummaryrefslogtreecommitdiff
path: root/runtime/CSharp3/Sources/Antlr3.Runtime.Debug/Misc/DoubleKeyMap`3.cs
blob: 16cfc263fd2001b0896d67fe25f962614f21bd88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace Antlr.Runtime.Debug.Misc
{
    using System.Collections.Generic;

    public class DoubleKeyMap<TKey1, TKey2, TValue>
    {
        internal IDictionary<TKey1, IDictionary<TKey2, TValue>> data = new Dictionary<TKey1, IDictionary<TKey2, TValue>>();

        public virtual TValue Put(TKey1 k1, TKey2 k2, TValue v)
        {
            IDictionary<TKey2, TValue> data2;
            data.TryGetValue(k1, out data2);
            TValue prev = default(TValue);
            if (data2 == null)
            {
                data2 = new Dictionary<TKey2, TValue>();
                data[k1]=data2;
            }
            else
            {
                data2.TryGetValue(k2, out prev);
            }
            data2[k2]= v;
            return prev;
        }

        public virtual TValue Get(TKey1 k1, TKey2 k2)
        {
            IDictionary<TKey2, TValue> data2;
            data.TryGetValue(k1, out data2);
            if (data2 == null)
                return default(TValue);

            TValue value;
            data2.TryGetValue(k2, out value);
            return value;
        }

        public virtual IDictionary<TKey2, TValue> Get(TKey1 k1)
        {
            IDictionary<TKey2, TValue> value;
            data.TryGetValue(k1, out value);
            return value;
        }

        /** Get all values associated with primary key */
        public virtual ICollection<TValue> Values(TKey1 k1)
        {
            IDictionary<TKey2, TValue> data2;
            data.TryGetValue(k1, out data2);
            if (data2 == null)
                return null;

            return data2.Values;
        }

        /** get all primary keys */
        public virtual ICollection<TKey1> KeySet()
        {
            return data.Keys;
        }

        /** get all secondary keys associated with a primary key */
        public virtual ICollection<TKey2> KeySet(TKey1 k1)
        {
            IDictionary<TKey2, TValue> data2;
            data.TryGetValue(k1, out data2);
            if (data2 == null)
                return null;

            return data2.Keys;
        }

        public virtual ICollection<TValue> Values()
        {
            Dictionary<TValue, bool> s = new Dictionary<TValue, bool>();
            foreach (IDictionary<TKey2, TValue> k2 in data.Values)
            {
                foreach (TValue v in k2.Values)
                    s[v] = true;
            }

            return s.Keys;
        }
    }
}