• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Note to JL: Changed Hashset to List
2 
3 namespace Antlr.Runtime.Debug.Misc {
4     using System.Collections.Generic;
5 
6     public class DoubleKeyMap<TKey1, TKey2, TValue> {
7         internal IDictionary<TKey1, IDictionary<TKey2, TValue>> data = new Dictionary<TKey1, IDictionary<TKey2, TValue>>();
8 
Put(TKey1 k1, TKey2 k2, TValue v)9         public virtual TValue Put(TKey1 k1, TKey2 k2, TValue v) {
10             IDictionary<TKey2, TValue> data2;
11             data.TryGetValue(k1, out data2);
12             TValue prev = default(TValue);
13             if (data2 == null) {
14                 data2 = new Dictionary<TKey2, TValue>();
15                 data[k1] = data2;
16             } else {
17                 data2.TryGetValue(k2, out prev);
18             }
19             data2[k2] = v;
20             return prev;
21         }
22 
Get(TKey1 k1, TKey2 k2)23         public virtual TValue Get(TKey1 k1, TKey2 k2) {
24             IDictionary<TKey2, TValue> data2;
25             data.TryGetValue(k1, out data2);
26             if (data2 == null)
27                 return default(TValue);
28 
29             TValue value;
30             data2.TryGetValue(k2, out value);
31             return value;
32         }
33 
Get(TKey1 k1)34         public virtual IDictionary<TKey2, TValue> Get(TKey1 k1) {
35             IDictionary<TKey2, TValue> value;
36             data.TryGetValue(k1, out value);
37             return value;
38         }
39 
40         /** Get all values associated with primary key */
Values(TKey1 k1)41         public virtual ICollection<TValue> Values(TKey1 k1) {
42             IDictionary<TKey2, TValue> data2;
43             data.TryGetValue(k1, out data2);
44             if (data2 == null)
45                 return null;
46 
47             return data2.Values;
48         }
49 
50         /** get all primary keys */
KeySet()51         public virtual ICollection<TKey1> KeySet() {
52             return data.Keys;
53         }
54 
55         /** get all secondary keys associated with a primary key */
KeySet(TKey1 k1)56         public virtual ICollection<TKey2> KeySet(TKey1 k1) {
57             IDictionary<TKey2, TValue> data2;
58             data.TryGetValue(k1, out data2);
59             if (data2 == null)
60                 return null;
61 
62             return data2.Keys;
63         }
64 
Values()65         public virtual ICollection<TValue> Values() {
66             List<TValue> s = new List<TValue>();
67             foreach (IDictionary<TKey2, TValue> k2 in data.Values) {
68                 foreach (TValue v in k2.Values) {
69                     s.Add(v);
70                 }
71             }
72             return s;
73         }
74     }
75 }
76