MapAndArray.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) Contributors, 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 OpenSimulator 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. namespace OpenSim.Framework
  30. {
  31. /// <summary>
  32. /// Stores two synchronized collections: a mutable dictionary and an
  33. /// immutable array. Slower inserts/removes than a normal dictionary,
  34. /// but provides safe iteration while maintaining fast hash lookups
  35. /// </summary>
  36. /// <typeparam name="TKey">Key type to use for hash lookups</typeparam>
  37. /// <typeparam name="TValue">Value type to store</typeparam>
  38. public sealed class MapAndArray<TKey, TValue>
  39. {
  40. private Dictionary<TKey, TValue> m_dict;
  41. private TValue[] m_array;
  42. /// <summary>Number of values currently stored in the collection</summary>
  43. public int Count { get { return m_dict.Count; } }
  44. /// <summary>NOTE: This collection is thread safe. You do not need to
  45. /// acquire a lock to add, remove, or enumerate entries. This
  46. /// synchronization object should only be locked for larger
  47. /// transactions</summary>
  48. private object m_syncRoot = new object();
  49. public object SyncRoot { get { return m_syncRoot; } }
  50. /// <summary>
  51. /// Constructor
  52. /// </summary>
  53. public MapAndArray()
  54. {
  55. m_dict = new Dictionary<TKey, TValue>();
  56. m_array = null;
  57. }
  58. /// <summary>
  59. /// Constructor
  60. /// </summary>
  61. /// <param name="capacity">Initial capacity of the dictionary</param>
  62. public MapAndArray(int capacity)
  63. {
  64. m_dict = new Dictionary<TKey, TValue>(capacity);
  65. m_array = null;
  66. }
  67. /// <summary>
  68. /// Adds a key/value pair to the collection, or updates an existing key
  69. /// with a new value
  70. /// </summary>
  71. /// <param name="key">Key to add or update</param>
  72. /// <param name="value">Value to add</param>
  73. /// <returns>True if a new key was added, false if an existing key was
  74. /// updated</returns>
  75. public bool AddOrReplace(TKey key, TValue value)
  76. {
  77. lock (m_syncRoot)
  78. {
  79. bool containedKey = m_dict.ContainsKey(key);
  80. m_dict[key] = value;
  81. m_array = null;
  82. return !containedKey;
  83. }
  84. }
  85. /// <summary>
  86. /// Adds a key/value pair to the collection. This will throw an
  87. /// exception if the key is already present in the collection
  88. /// </summary>
  89. /// <param name="key">Key to add or update</param>
  90. /// <param name="value">Value to add</param>
  91. /// <returns>Index of the inserted item</returns>
  92. public int Add(TKey key, TValue value)
  93. {
  94. lock (m_syncRoot)
  95. {
  96. m_dict.Add(key, value);
  97. m_array = null;
  98. return m_dict.Count;
  99. }
  100. }
  101. /// <summary>
  102. /// Removes a key/value pair from the collection
  103. /// </summary>
  104. /// <param name="key">Key to remove</param>
  105. /// <returns>True if the key was found and removed, otherwise false</returns>
  106. public bool Remove(TKey key)
  107. {
  108. lock (m_syncRoot)
  109. {
  110. bool removed = m_dict.Remove(key);
  111. m_array = null;
  112. return removed;
  113. }
  114. }
  115. /// <summary>
  116. /// Determines whether the collections contains a specified key
  117. /// </summary>
  118. /// <param name="key">Key to search for</param>
  119. /// <returns>True if the key was found, otherwise false</returns>
  120. public bool ContainsKey(TKey key)
  121. {
  122. lock (m_syncRoot)
  123. return m_dict.ContainsKey(key);
  124. }
  125. /// <summary>
  126. /// Gets the value associated with the specified key
  127. /// </summary>
  128. /// <param name="key">Key of the value to get</param>
  129. /// <param name="value">Will contain the value associated with the
  130. /// given key if the key is found. If the key is not found it will
  131. /// contain the default value for the type of the value parameter</param>
  132. /// <returns>True if the key was found and a value was retrieved,
  133. /// otherwise false</returns>
  134. public bool TryGetValue(TKey key, out TValue value)
  135. {
  136. lock (m_syncRoot)
  137. return m_dict.TryGetValue(key, out value);
  138. }
  139. /// <summary>
  140. /// Clears all key/value pairs from the collection
  141. /// </summary>
  142. public void Clear()
  143. {
  144. lock (m_syncRoot)
  145. {
  146. m_dict = new Dictionary<TKey, TValue>();
  147. m_array = null;
  148. }
  149. }
  150. /// <summary>
  151. /// Gets a reference to the immutable array of values stored in this
  152. /// collection. This array is thread safe for iteration
  153. /// </summary>
  154. /// <returns>A thread safe reference ton an array of all of the stored
  155. /// values</returns>
  156. public TValue[] GetArray()
  157. {
  158. lock (m_syncRoot)
  159. {
  160. if (m_array == null)
  161. {
  162. if(m_dict.Count == 0)
  163. return new TValue[0];
  164. m_array = new TValue[m_dict.Count];
  165. m_dict.Values.CopyTo(m_array, 0);
  166. }
  167. return m_array;
  168. }
  169. }
  170. }
  171. }