aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
authorJohn Hurliman2010-09-16 17:30:46 -0700
committerJohn Hurliman2010-09-16 17:30:46 -0700
commit860b2a502f797e5822c6705d4639f370f3ac5861 (patch)
tree5a74ddbd626142e27f6c3439ea267b8ea348ce9c /OpenSim/Framework
parentAdd the modules include line back that i dropped by mistake (diff)
downloadopensim-SC-860b2a502f797e5822c6705d4639f370f3ac5861.zip
opensim-SC-860b2a502f797e5822c6705d4639f370f3ac5861.tar.gz
opensim-SC-860b2a502f797e5822c6705d4639f370f3ac5861.tar.bz2
opensim-SC-860b2a502f797e5822c6705d4639f370f3ac5861.tar.xz
Changed SceneObjectGroup to store parts with the fast and thread-safe MapAndArray collection
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/MapAndArray.cs188
1 files changed, 188 insertions, 0 deletions
diff --git a/OpenSim/Framework/MapAndArray.cs b/OpenSim/Framework/MapAndArray.cs
new file mode 100644
index 0000000..bbe6a9e
--- /dev/null
+++ b/OpenSim/Framework/MapAndArray.cs
@@ -0,0 +1,188 @@
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
28using System;
29using System.Collections.Generic;
30
31namespace 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 private object m_syncRoot = new object();
45
46 /// <summary>Number of values currently stored in the collection</summary>
47 public int Count { get { return m_array.Length; } }
48 /// <summary>NOTE: This collection is thread safe. You do not need to
49 /// acquire a lock to add, remove, or enumerate entries. This
50 /// synchronization object should only be locked for larger
51 /// transactions</summary>
52 public object SyncRoot { get { return m_syncRoot; } }
53
54 /// <summary>
55 /// Constructor
56 /// </summary>
57 public MapAndArray()
58 {
59 m_dict = new Dictionary<TKey, TValue>();
60 m_array = new TValue[0];
61 }
62
63 /// <summary>
64 /// Constructor
65 /// </summary>
66 /// <param name="capacity">Initial capacity of the dictionary</param>
67 public MapAndArray(int capacity)
68 {
69 m_dict = new Dictionary<TKey, TValue>(capacity);
70 m_array = new TValue[0];
71 }
72
73 /// <summary>
74 /// Adds a key/value pair to the collection, or updates an existing key
75 /// with a new value
76 /// </summary>
77 /// <param name="key">Key to add or update</param>
78 /// <param name="value">Value to add</param>
79 /// <returns>True if a new key was added, false if an existing key was
80 /// updated</returns>
81 public bool AddOrReplace(TKey key, TValue value)
82 {
83 lock (m_syncRoot)
84 {
85 bool containedKey = m_dict.ContainsKey(key);
86
87 m_dict[key] = value;
88 CreateArray();
89
90 return !containedKey;
91 }
92 }
93
94 /// <summary>
95 /// Adds a key/value pair to the collection. This will throw an
96 /// exception if the key is already present in the collection
97 /// </summary>
98 /// <param name="key">Key to add or update</param>
99 /// <param name="value">Value to add</param>
100 /// <returns>Index of the inserted item</returns>
101 public int Add(TKey key, TValue value)
102 {
103 lock (m_syncRoot)
104 {
105 m_dict.Add(key, value);
106 CreateArray();
107 return m_array.Length;
108 }
109 }
110
111 /// <summary>
112 /// Removes a key/value pair from the collection
113 /// </summary>
114 /// <param name="key">Key to remove</param>
115 /// <returns>True if the key was found and removed, otherwise false</returns>
116 public bool Remove(TKey key)
117 {
118 lock (m_syncRoot)
119 {
120 bool removed = m_dict.Remove(key);
121 CreateArray();
122
123 return removed;
124 }
125 }
126
127 /// <summary>
128 /// Determines whether the collections contains a specified key
129 /// </summary>
130 /// <param name="key">Key to search for</param>
131 /// <returns>True if the key was found, otherwise false</returns>
132 public bool ContainsKey(TKey key)
133 {
134 return m_dict.ContainsKey(key);
135 }
136
137 /// <summary>
138 /// Gets the value associated with the specified key
139 /// </summary>
140 /// <param name="key">Key of the value to get</param>
141 /// <param name="value">Will contain the value associated with the
142 /// given key if the key is found. If the key is not found it will
143 /// contain the default value for the type of the value parameter</param>
144 /// <returns>True if the key was found and a value was retrieved,
145 /// otherwise false</returns>
146 public bool TryGetValue(TKey key, out TValue value)
147 {
148 lock (m_syncRoot)
149 return m_dict.TryGetValue(key, out value);
150 }
151
152 /// <summary>
153 /// Clears all key/value pairs from the collection
154 /// </summary>
155 public void Clear()
156 {
157 lock (m_syncRoot)
158 {
159 m_dict = new Dictionary<TKey, TValue>();
160 m_array = new TValue[0];
161 }
162 }
163
164 /// <summary>
165 /// Gets a reference to the immutable array of values stored in this
166 /// collection. This array is thread safe for iteration
167 /// </summary>
168 /// <returns>A thread safe reference ton an array of all of the stored
169 /// values</returns>
170 public TValue[] GetArray()
171 {
172 return m_array;
173 }
174
175 private void CreateArray()
176 {
177 // Rebuild the array from the dictionary. This method must be
178 // called from inside a lock
179 TValue[] array = new TValue[m_dict.Count];
180 int i = 0;
181
182 foreach (TValue value in m_dict.Values)
183 array[i++] = value;
184
185 m_array = array;
186 }
187 }
188}