1 namespace Antlr.Runtime.Debug.Misc 2 { 3 using System.Collections.Generic; 4 5 public class DoubleKeyMap<TKey1, TKey2, TValue> 6 { 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 { 11 IDictionary<TKey2, TValue> data2; 12 data.TryGetValue(k1, out data2); 13 TValue prev = default(TValue); 14 if (data2 == null) 15 { 16 data2 = new Dictionary<TKey2, TValue>(); 17 data[k1]=data2; 18 } 19 else 20 { 21 data2.TryGetValue(k2, out prev); 22 } 23 data2[k2]= v; 24 return prev; 25 } 26 Get(TKey1 k1, TKey2 k2)27 public virtual TValue Get(TKey1 k1, TKey2 k2) 28 { 29 IDictionary<TKey2, TValue> data2; 30 data.TryGetValue(k1, out data2); 31 if (data2 == null) 32 return default(TValue); 33 34 TValue value; 35 data2.TryGetValue(k2, out value); 36 return value; 37 } 38 Get(TKey1 k1)39 public virtual IDictionary<TKey2, TValue> Get(TKey1 k1) 40 { 41 IDictionary<TKey2, TValue> value; 42 data.TryGetValue(k1, out value); 43 return value; 44 } 45 46 /** Get all values associated with primary key */ Values(TKey1 k1)47 public virtual ICollection<TValue> Values(TKey1 k1) 48 { 49 IDictionary<TKey2, TValue> data2; 50 data.TryGetValue(k1, out data2); 51 if (data2 == null) 52 return null; 53 54 return data2.Values; 55 } 56 57 /** get all primary keys */ KeySet()58 public virtual ICollection<TKey1> KeySet() 59 { 60 return data.Keys; 61 } 62 63 /** get all secondary keys associated with a primary key */ KeySet(TKey1 k1)64 public virtual ICollection<TKey2> KeySet(TKey1 k1) 65 { 66 IDictionary<TKey2, TValue> data2; 67 data.TryGetValue(k1, out data2); 68 if (data2 == null) 69 return null; 70 71 return data2.Keys; 72 } 73 Values()74 public virtual ICollection<TValue> Values() 75 { 76 Dictionary<TValue, bool> s = new Dictionary<TValue, bool>(); 77 foreach (IDictionary<TKey2, TValue> k2 in data.Values) 78 { 79 foreach (TValue v in k2.Values) 80 s[v] = true; 81 } 82 83 return s.Keys; 84 } 85 } 86 } 87