OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MapAndArray.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 
31 namespace OpenSim.Framework
32 {
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 
47  public int Count { get { return m_array.Length; } }
52  public object SyncRoot { get { return m_syncRoot; } }
53 
57  public MapAndArray()
58  {
59  m_dict = new Dictionary<TKey, TValue>();
60  m_array = new TValue[0];
61  }
62 
67  public MapAndArray(int capacity)
68  {
69  m_dict = new Dictionary<TKey, TValue>(capacity);
70  m_array = new TValue[0];
71  }
72 
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 
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 
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 
132  public bool ContainsKey(TKey key)
133  {
134  lock (m_syncRoot)
135  return m_dict.ContainsKey(key);
136  }
137 
147  public bool TryGetValue(TKey key, out TValue value)
148  {
149  lock (m_syncRoot)
150  return m_dict.TryGetValue(key, out value);
151  }
152 
156  public void Clear()
157  {
158  lock (m_syncRoot)
159  {
160  m_dict = new Dictionary<TKey, TValue>();
161  m_array = new TValue[0];
162  }
163  }
164 
171  public TValue[] GetArray()
172  {
173  return m_array;
174  }
175 
176  private void CreateArray()
177  {
178  // Rebuild the array from the dictionary. This method must be
179  // called from inside a lock
180  TValue[] array = new TValue[m_dict.Count];
181  int i = 0;
182 
183  foreach (TValue value in m_dict.Values)
184  array[i++] = value;
185 
186  m_array = array;
187  }
188  }
189 }
int Add(TKey key, TValue value)
Adds a key/value pair to the collection. This will throw an exception if the key is already present i...
Definition: MapAndArray.cs:101
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
Definition: ICM_Api.cs:31
TValue[] GetArray()
Gets a reference to the immutable array of values stored in this collection. This array is thread saf...
Definition: MapAndArray.cs:171
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key
Definition: MapAndArray.cs:147
bool AddOrReplace(TKey key, TValue value)
Adds a key/value pair to the collection, or updates an existing key with a new value ...
Definition: MapAndArray.cs:81
void Clear()
Clears all key/value pairs from the collection
Definition: MapAndArray.cs:156
bool Remove(TKey key)
Removes a key/value pair from the collection
Definition: MapAndArray.cs:116
bool ContainsKey(TKey key)
Determines whether the collections contains a specified key
Definition: MapAndArray.cs:132