PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Aurora/Framework/Utilities/DoubleDictionarys.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 339 lines | 274 code | 38 blank | 27 comment | 17 complexity | 1d9c48c9c12bc75689e5e3bab4ee1e48 MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. namespace Aurora.Framework.Utilities
  31. {
  32. public class DoubleValueDictionary<TKey, TValue1, TValue2>
  33. {
  34. private readonly Dictionary<TKey, List<object>> Dictionary = new Dictionary<TKey, List<object>>();
  35. public TValue1 this[TKey key, TKey n]
  36. {
  37. get
  38. {
  39. if (!Dictionary.ContainsKey(key))
  40. return default(TValue1);
  41. List<Object> Values = new List<object>();
  42. Dictionary.TryGetValue(key, out Values);
  43. return (TValue1) Values[0];
  44. }
  45. set
  46. {
  47. List<Object> Values = new List<object>();
  48. if (Dictionary.ContainsKey(key))
  49. Dictionary.TryGetValue(key, out Values);
  50. Values[0] = value;
  51. Dictionary[key] = Values;
  52. }
  53. }
  54. public TValue2 this[TKey key]
  55. {
  56. get
  57. {
  58. if (!Dictionary.ContainsKey(key))
  59. return default(TValue2);
  60. List<Object> Values = new List<object>();
  61. Dictionary.TryGetValue(key, out Values);
  62. return (TValue2) Values[1];
  63. }
  64. set
  65. {
  66. List<Object> Values = new List<object>();
  67. if (Dictionary.ContainsKey(key))
  68. Dictionary.TryGetValue(key, out Values);
  69. Values[1] = value;
  70. Dictionary[key] = Values;
  71. }
  72. }
  73. public int Count
  74. {
  75. get { return Dictionary.Count; }
  76. }
  77. public void Add(TKey key, TValue1 value1, TValue2 value2)
  78. {
  79. if (Dictionary.ContainsKey(key))
  80. throw new ArgumentException("Key is already in the dictionary");
  81. List<object> Values = new List<object>(2) {value1, value2};
  82. Dictionary.Add(key, Values);
  83. }
  84. public bool Remove(TKey key)
  85. {
  86. if (!Dictionary.ContainsKey(key))
  87. return false;
  88. Dictionary.Remove(key);
  89. return true;
  90. }
  91. public void Clear()
  92. {
  93. Dictionary.Clear();
  94. }
  95. public bool ContainsKey(TKey key)
  96. {
  97. return Dictionary.ContainsKey(key);
  98. }
  99. public bool TryGetValue(TKey key, out TValue1 value)
  100. {
  101. value = default(TValue1);
  102. if (!Dictionary.ContainsKey(key))
  103. return false;
  104. List<object> Values = new List<object>();
  105. Dictionary.TryGetValue(key, out Values);
  106. value = (TValue1) Values[0];
  107. return true;
  108. }
  109. public bool TryGetValue(TKey key, out TValue2 value)
  110. {
  111. value = default(TValue2);
  112. if (!Dictionary.ContainsKey(key))
  113. return false;
  114. List<object> Values = new List<object>();
  115. Dictionary.TryGetValue(key, out Values);
  116. value = (TValue2) Values[1];
  117. return true;
  118. }
  119. }
  120. //Fixed version of the LibOMV class
  121. public class DoubleKeyDictionary<TKey1, TKey2, TValue>
  122. {
  123. private readonly Dictionary<TKey1, TValue> Dictionary1 = new Dictionary<TKey1, TValue>();
  124. private readonly Dictionary<TKey2, TValue> Dictionary2 = new Dictionary<TKey2, TValue>();
  125. private readonly object m_lock = new object();
  126. public int Count
  127. {
  128. get { return Dictionary1.Count; }
  129. }
  130. public void Add(TKey1 key1, TKey2 key2, TValue value)
  131. {
  132. lock (m_lock)
  133. {
  134. if (Dictionary1.ContainsKey(key1))
  135. throw new ArgumentException("Key1 (UUID, " + key1 + ") is already in the dictionary");
  136. if (Dictionary2.ContainsKey(key2))
  137. throw new ArgumentException("Key2 (LocalID, " + key2 + ") is already in the dictionary");
  138. Dictionary1.Add(key1, value);
  139. Dictionary2.Add(key2, value);
  140. }
  141. }
  142. public bool Remove(TKey1 key1)
  143. {
  144. lock (m_lock)
  145. {
  146. if (!Dictionary1.ContainsKey(key1))
  147. return false;
  148. Dictionary1.Remove(key1);
  149. return true;
  150. }
  151. }
  152. public bool Remove(TKey2 key2)
  153. {
  154. lock (m_lock)
  155. {
  156. if (!Dictionary2.ContainsKey(key2))
  157. return false;
  158. Dictionary2.Remove(key2);
  159. return true;
  160. }
  161. }
  162. public void Clear()
  163. {
  164. lock (m_lock)
  165. {
  166. Dictionary1.Clear();
  167. Dictionary2.Clear();
  168. }
  169. }
  170. public bool ContainsKey(TKey1 key)
  171. {
  172. lock (m_lock)
  173. {
  174. return Dictionary1.ContainsKey(key);
  175. }
  176. }
  177. public bool ContainsKey(TKey2 key)
  178. {
  179. lock (m_lock)
  180. {
  181. return Dictionary2.ContainsKey(key);
  182. }
  183. }
  184. public bool TryGetValue(TKey1 key, out TValue value)
  185. {
  186. lock (m_lock)
  187. {
  188. Dictionary1.TryGetValue(key, out value);
  189. return value != null;
  190. }
  191. }
  192. public bool TryGetValue(TKey2 key, out TValue value)
  193. {
  194. lock (m_lock)
  195. {
  196. Dictionary2.TryGetValue(key, out value);
  197. return value != null;
  198. }
  199. }
  200. public void ForEach(Action<TValue> action)
  201. {
  202. lock (m_lock)
  203. {
  204. foreach (TValue value in Dictionary1.Values)
  205. action(value);
  206. }
  207. }
  208. public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
  209. {
  210. lock (m_lock)
  211. {
  212. foreach (KeyValuePair<TKey1, TValue> entry in Dictionary1)
  213. action(entry);
  214. }
  215. }
  216. public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
  217. {
  218. lock (m_lock)
  219. {
  220. foreach (KeyValuePair<TKey2, TValue> entry in Dictionary2)
  221. action(entry);
  222. }
  223. }
  224. public TValue FindValue(Predicate<TValue> predicate)
  225. {
  226. lock (m_lock)
  227. {
  228. #if (!ISWIN)
  229. foreach (TValue value in Dictionary1.Values)
  230. {
  231. if (predicate(value))
  232. {
  233. return value;
  234. }
  235. }
  236. #else
  237. foreach (TValue value in Dictionary1.Values.Where(value => predicate(value)))
  238. {
  239. return value;
  240. }
  241. #endif
  242. return default(TValue);
  243. }
  244. }
  245. public IList<TValue> FindAll(Predicate<TValue> predicate)
  246. {
  247. lock (m_lock)
  248. {
  249. #if (!ISWIN)
  250. List<TValue> list = new List<TValue>();
  251. foreach (TValue value in Dictionary1.Values)
  252. {
  253. if (predicate(value)) list.Add(value);
  254. }
  255. return list;
  256. #else
  257. return Dictionary1.Values.Where(value => predicate(value)).ToList();
  258. #endif
  259. }
  260. }
  261. public int RemoveAll(Predicate<TValue> predicate)
  262. {
  263. lock (m_lock)
  264. {
  265. IList<TKey1> list = (from kvp in Dictionary1 where predicate(kvp.Value) select kvp.Key).ToList();
  266. IList<TKey2> list2 = new List<TKey2>(list.Count);
  267. #if (!ISWIN)
  268. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  269. {
  270. if (predicate(kvp.Value))
  271. {
  272. list2.Add(kvp.Key);
  273. }
  274. }
  275. #else
  276. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2.Where(kvp => predicate(kvp.Value)))
  277. {
  278. list2.Add(kvp.Key);
  279. }
  280. #endif
  281. foreach (TKey1 t in list)
  282. Dictionary1.Remove(t);
  283. foreach (TKey2 t in list2)
  284. Dictionary2.Remove(t);
  285. return list.Count;
  286. }
  287. }
  288. public TValue[] Values
  289. {
  290. get
  291. {
  292. lock (m_lock)
  293. {
  294. TValue[] values = new TValue[Dictionary1.Count];
  295. Dictionary1.Values.CopyTo(values, 0);
  296. return values;
  297. }
  298. }
  299. }
  300. }
  301. }