Annotation of /trunk/DoubleDictionary.cs
Parent Directory
|
Revision Log
Revision 24 - (view) (download)
| 1 : | jhurliman | 24 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | |||
| 4 : | namespace AssetServer | ||
| 5 : | { | ||
| 6 : | public class DoubleDictionary<TKey1, TKey2, TValue> | ||
| 7 : | { | ||
| 8 : | Dictionary<TKey1, TValue> Dictionary1; | ||
| 9 : | Dictionary<TKey2, TValue> Dictionary2; | ||
| 10 : | object syncObject = new object(); | ||
| 11 : | |||
| 12 : | public DoubleDictionary() | ||
| 13 : | { | ||
| 14 : | Dictionary1 = new Dictionary<TKey1,TValue>(); | ||
| 15 : | Dictionary2 = new Dictionary<TKey2,TValue>(); | ||
| 16 : | } | ||
| 17 : | |||
| 18 : | public DoubleDictionary(int capacity) | ||
| 19 : | { | ||
| 20 : | Dictionary1 = new Dictionary<TKey1, TValue>(capacity); | ||
| 21 : | Dictionary2 = new Dictionary<TKey2, TValue>(capacity); | ||
| 22 : | } | ||
| 23 : | |||
| 24 : | public void Add(TKey1 key1, TKey2 key2, TValue value) | ||
| 25 : | { | ||
| 26 : | lock (syncObject) | ||
| 27 : | { | ||
| 28 : | if (Dictionary1.ContainsKey(key1)) | ||
| 29 : | { | ||
| 30 : | if (!Dictionary2.ContainsKey(key2)) | ||
| 31 : | throw new ArgumentException("key1 exists in the dictionary but not key2"); | ||
| 32 : | } | ||
| 33 : | else if (Dictionary2.ContainsKey(key2)) | ||
| 34 : | { | ||
| 35 : | if (!Dictionary1.ContainsKey(key1)) | ||
| 36 : | throw new ArgumentException("key2 exists in the dictionary but not key1"); | ||
| 37 : | } | ||
| 38 : | |||
| 39 : | Dictionary1[key1] = value; | ||
| 40 : | Dictionary2[key2] = value; | ||
| 41 : | } | ||
| 42 : | } | ||
| 43 : | |||
| 44 : | public bool Remove(TKey1 key1, TKey2 key2) | ||
| 45 : | { | ||
| 46 : | lock (syncObject) | ||
| 47 : | { | ||
| 48 : | Dictionary1.Remove(key1); | ||
| 49 : | return Dictionary2.Remove(key2); | ||
| 50 : | } | ||
| 51 : | } | ||
| 52 : | |||
| 53 : | public bool Remove(TKey1 key1) | ||
| 54 : | { | ||
| 55 : | // This is an O(n) operation! | ||
| 56 : | lock (syncObject) | ||
| 57 : | { | ||
| 58 : | TValue value; | ||
| 59 : | if (Dictionary1.TryGetValue(key1, out value)) | ||
| 60 : | { | ||
| 61 : | foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2) | ||
| 62 : | { | ||
| 63 : | if (kvp.Value.Equals(value)) | ||
| 64 : | { | ||
| 65 : | Dictionary1.Remove(key1); | ||
| 66 : | Dictionary2.Remove(kvp.Key); | ||
| 67 : | return true; | ||
| 68 : | } | ||
| 69 : | } | ||
| 70 : | } | ||
| 71 : | } | ||
| 72 : | |||
| 73 : | return false; | ||
| 74 : | } | ||
| 75 : | |||
| 76 : | public bool Remove(TKey2 key2) | ||
| 77 : | { | ||
| 78 : | // This is an O(n) operation! | ||
| 79 : | lock (syncObject) | ||
| 80 : | { | ||
| 81 : | TValue value; | ||
| 82 : | if (Dictionary2.TryGetValue(key2, out value)) | ||
| 83 : | { | ||
| 84 : | foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1) | ||
| 85 : | { | ||
| 86 : | if (kvp.Value.Equals(value)) | ||
| 87 : | { | ||
| 88 : | Dictionary2.Remove(key2); | ||
| 89 : | Dictionary1.Remove(kvp.Key); | ||
| 90 : | return true; | ||
| 91 : | } | ||
| 92 : | } | ||
| 93 : | } | ||
| 94 : | } | ||
| 95 : | |||
| 96 : | return false; | ||
| 97 : | } | ||
| 98 : | |||
| 99 : | public void Clear() | ||
| 100 : | { | ||
| 101 : | lock (syncObject) | ||
| 102 : | { | ||
| 103 : | Dictionary1.Clear(); | ||
| 104 : | Dictionary2.Clear(); | ||
| 105 : | } | ||
| 106 : | } | ||
| 107 : | |||
| 108 : | public int Count | ||
| 109 : | { | ||
| 110 : | get { return Dictionary1.Count; } | ||
| 111 : | } | ||
| 112 : | |||
| 113 : | public bool ContainsKey(TKey1 key) | ||
| 114 : | { | ||
| 115 : | return Dictionary1.ContainsKey(key); | ||
| 116 : | } | ||
| 117 : | |||
| 118 : | public bool ContainsKey(TKey2 key) | ||
| 119 : | { | ||
| 120 : | return Dictionary2.ContainsKey(key); | ||
| 121 : | } | ||
| 122 : | |||
| 123 : | public bool TryGetValue(TKey1 key, out TValue value) | ||
| 124 : | { | ||
| 125 : | return Dictionary1.TryGetValue(key, out value); | ||
| 126 : | } | ||
| 127 : | |||
| 128 : | public bool TryGetValue(TKey2 key, out TValue value) | ||
| 129 : | { | ||
| 130 : | return Dictionary2.TryGetValue(key, out value); | ||
| 131 : | } | ||
| 132 : | |||
| 133 : | public void ForEach(Action<TValue> action) | ||
| 134 : | { | ||
| 135 : | lock (syncObject) | ||
| 136 : | { | ||
| 137 : | foreach (TValue value in Dictionary1.Values) | ||
| 138 : | action(value); | ||
| 139 : | } | ||
| 140 : | } | ||
| 141 : | |||
| 142 : | public TValue this[TKey1 key1] | ||
| 143 : | { | ||
| 144 : | get { return Dictionary1[key1]; } | ||
| 145 : | } | ||
| 146 : | |||
| 147 : | public TValue this[TKey2 key2] | ||
| 148 : | { | ||
| 149 : | get { return Dictionary2[key2]; } | ||
| 150 : | } | ||
| 151 : | } | ||
| 152 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

