1
0

MapAndArray.cs 7.2 KB

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