PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenSim/Framework/Physics/DoubleDictionarys.cs

http://github.com/aurora-sim/Aurora-Sim
C# | 316 lines | 249 code | 40 blank | 27 comment | 20 complexity | 477326d64bbb48bc197283b7817be889 MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  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. using System.Text;
  31. namespace Aurora.Framework
  32. {
  33. public class DoubleValueDictionary<TKey, TValue1, TValue2>
  34. {
  35. Dictionary<TKey, List<object>> Dictionary = new Dictionary<TKey, List<object>>();
  36. public void Add(TKey key, TValue1 value1, TValue2 value2)
  37. {
  38. if (Dictionary.ContainsKey(key))
  39. throw new ArgumentException("Key is already in the dictionary");
  40. List<object> Values = new List<object>(2);
  41. Values.Add(value1);
  42. Values.Add(value2);
  43. Dictionary.Add(key, Values);
  44. }
  45. public bool Remove(TKey key)
  46. {
  47. if (!Dictionary.ContainsKey(key))
  48. return false;
  49. Dictionary.Remove(key);
  50. return true;
  51. }
  52. public TValue1 this[TKey key, TKey n]
  53. {
  54. get
  55. {
  56. if (!Dictionary.ContainsKey(key))
  57. return default(TValue1);
  58. List<Object> Values = new List<object>();
  59. Dictionary.TryGetValue(key, out Values);
  60. return (TValue1)Values[0];
  61. }
  62. set
  63. {
  64. List<Object> Values = new List<object>();
  65. if (Dictionary.ContainsKey(key))
  66. Dictionary.TryGetValue(key, out Values);
  67. Values[0] = value;
  68. Dictionary[key] = Values;
  69. }
  70. }
  71. public TValue2 this[TKey key]
  72. {
  73. get
  74. {
  75. if (!Dictionary.ContainsKey(key))
  76. return default(TValue2);
  77. List<Object> Values = new List<object>();
  78. Dictionary.TryGetValue(key, out Values);
  79. return (TValue2)Values[1];
  80. }
  81. set
  82. {
  83. List<Object> Values = new List<object>();
  84. if (Dictionary.ContainsKey(key))
  85. Dictionary.TryGetValue(key, out Values);
  86. Values[1] = value;
  87. Dictionary[key] = Values;
  88. }
  89. }
  90. public void Clear()
  91. {
  92. Dictionary.Clear();
  93. }
  94. public int Count
  95. {
  96. get { return Dictionary.Count; }
  97. }
  98. public bool ContainsKey(TKey key)
  99. {
  100. return Dictionary.ContainsKey(key);
  101. }
  102. public bool TryGetValue(TKey key, out TValue1 value)
  103. {
  104. value = default(TValue1);
  105. if (!Dictionary.ContainsKey(key))
  106. return false;
  107. List<object> Values = new List<object>();
  108. Dictionary.TryGetValue(key, out Values);
  109. value = (TValue1)Values[0];
  110. return true;
  111. }
  112. public bool TryGetValue(TKey key, out TValue2 value)
  113. {
  114. value = default(TValue2);
  115. if (!Dictionary.ContainsKey(key))
  116. return false;
  117. List<object> Values = new List<object>();
  118. Dictionary.TryGetValue(key, out Values);
  119. value = (TValue2)Values[1];
  120. return true;
  121. }
  122. }
  123. //Fixed version of the LibOMV class
  124. public class DoubleKeyDictionary<TKey1, TKey2, TValue>
  125. {
  126. private object m_lock = new object();
  127. Dictionary<TKey1, TValue> Dictionary1 = new Dictionary<TKey1, TValue>();
  128. Dictionary<TKey2, TValue> Dictionary2 = new Dictionary<TKey2, TValue>();
  129. public void Add(TKey1 key1, TKey2 key2, TValue value)
  130. {
  131. lock (m_lock)
  132. {
  133. if (Dictionary1.ContainsKey (key1))
  134. throw new ArgumentException ("Key1 (UUID, " + key1 + ") is already in the dictionary");
  135. if (Dictionary2.ContainsKey(key2))
  136. throw new ArgumentException("Key2 (LocalID, " + key2 + ") is already in the dictionary");
  137. Dictionary1.Add(key1, value);
  138. Dictionary2.Add(key2, value);
  139. }
  140. }
  141. public bool Remove(TKey1 key1)
  142. {
  143. lock (m_lock)
  144. {
  145. if (!Dictionary1.ContainsKey(key1))
  146. return false;
  147. Dictionary1.Remove(key1);
  148. return true;
  149. }
  150. }
  151. public bool Remove(TKey2 key2)
  152. {
  153. lock (m_lock)
  154. {
  155. if (!Dictionary2.ContainsKey(key2))
  156. return false;
  157. Dictionary2.Remove(key2);
  158. return true;
  159. }
  160. }
  161. public void Clear()
  162. {
  163. lock (m_lock)
  164. {
  165. Dictionary1.Clear();
  166. Dictionary2.Clear();
  167. }
  168. }
  169. public int Count
  170. {
  171. get { return Dictionary1.Count; }
  172. }
  173. public bool ContainsKey(TKey1 key)
  174. {
  175. lock (m_lock)
  176. {
  177. return Dictionary1.ContainsKey(key);
  178. }
  179. }
  180. public bool ContainsKey(TKey2 key)
  181. {
  182. lock (m_lock)
  183. {
  184. return Dictionary2.ContainsKey(key);
  185. }
  186. }
  187. public bool TryGetValue(TKey1 key, out TValue value)
  188. {
  189. lock (m_lock)
  190. {
  191. Dictionary1.TryGetValue(key, out value);
  192. return value != null;
  193. }
  194. }
  195. public bool TryGetValue(TKey2 key, out TValue value)
  196. {
  197. lock (m_lock)
  198. {
  199. Dictionary2.TryGetValue(key, out value);
  200. return value != null;
  201. }
  202. }
  203. public void ForEach(Action<TValue> action)
  204. {
  205. lock (m_lock)
  206. {
  207. foreach (TValue value in Dictionary1.Values)
  208. action(value);
  209. }
  210. }
  211. public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
  212. {
  213. lock (m_lock)
  214. {
  215. foreach (KeyValuePair<TKey1, TValue> entry in Dictionary1)
  216. action(entry);
  217. }
  218. }
  219. public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
  220. {
  221. lock (m_lock)
  222. {
  223. foreach (KeyValuePair<TKey2, TValue> entry in Dictionary2)
  224. action(entry);
  225. }
  226. }
  227. public TValue FindValue(Predicate<TValue> predicate)
  228. {
  229. lock (m_lock)
  230. {
  231. foreach (TValue value in Dictionary1.Values)
  232. {
  233. if (predicate(value))
  234. return value;
  235. }
  236. return default(TValue);
  237. }
  238. }
  239. public IList<TValue> FindAll(Predicate<TValue> predicate)
  240. {
  241. lock (m_lock)
  242. {
  243. IList<TValue> list = new List<TValue>();
  244. foreach (TValue value in Dictionary1.Values)
  245. {
  246. if (predicate(value))
  247. list.Add(value);
  248. }
  249. return list;
  250. }
  251. }
  252. public int RemoveAll(Predicate<TValue> predicate)
  253. {
  254. lock (m_lock)
  255. {
  256. IList<TKey1> list = new List<TKey1>();
  257. foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
  258. {
  259. if (predicate(kvp.Value))
  260. list.Add(kvp.Key);
  261. }
  262. IList<TKey2> list2 = new List<TKey2>(list.Count);
  263. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  264. {
  265. if (predicate(kvp.Value))
  266. list2.Add(kvp.Key);
  267. }
  268. for (int i = 0; i < list.Count; i++)
  269. Dictionary1.Remove(list[i]);
  270. for (int i = 0; i < list2.Count; i++)
  271. Dictionary2.Remove(list2[i]);
  272. return list.Count;
  273. }
  274. }
  275. }
  276. }