28 using System.Threading;
29 using System.Collections.Generic;
31 namespace OpenSim.Framework
40 public class DoubleDictionaryThreadAbortSafe<TKey1, TKey2, TValue>
42 Dictionary<TKey1, TValue> Dictionary1;
43 Dictionary<TKey2, TValue> Dictionary2;
44 ReaderWriterLockSlim rwLock =
new ReaderWriterLockSlim();
48 Dictionary1 =
new Dictionary<TKey1,TValue>();
49 Dictionary2 =
new Dictionary<TKey2,TValue>();
54 Dictionary1 =
new Dictionary<TKey1, TValue>(capacity);
55 Dictionary2 =
new Dictionary<TKey2, TValue>(capacity);
58 public void Add(TKey1 key1, TKey2 key2, TValue value)
70 rwLock.EnterWriteLock();
74 if (Dictionary1.ContainsKey(key1))
76 if (!Dictionary2.ContainsKey(key2))
77 throw new ArgumentException(
"key1 exists in the dictionary but not key2");
79 else if (Dictionary2.ContainsKey(key2))
81 if (!Dictionary1.ContainsKey(key1))
82 throw new ArgumentException(
"key2 exists in the dictionary but not key1");
85 Dictionary1[key1] = value;
86 Dictionary2[key2] = value;
91 rwLock.ExitWriteLock();
95 public bool Remove(TKey1 key1, TKey2 key2)
108 rwLock.EnterWriteLock();
112 Dictionary1.Remove(key1);
113 success = Dictionary2.Remove(key2);
118 rwLock.ExitWriteLock();
127 bool gotLock =
false;
137 rwLock.EnterWriteLock();
143 if (Dictionary1.TryGetValue(key1, out value))
145 foreach (KeyValuePair<TKey2, TValue> kvp
in Dictionary2)
147 if (kvp.Value.Equals(value))
149 Dictionary1.Remove(key1);
150 Dictionary2.Remove(kvp.Key);
160 rwLock.ExitWriteLock();
169 bool gotLock =
false;
179 rwLock.EnterWriteLock();
185 if (Dictionary2.TryGetValue(key2, out value))
187 foreach (KeyValuePair<TKey1, TValue> kvp
in Dictionary1)
189 if (kvp.Value.Equals(value))
191 Dictionary2.Remove(key2);
192 Dictionary1.Remove(kvp.Key);
202 rwLock.ExitWriteLock();
210 bool gotLock =
false;
220 rwLock.EnterWriteLock();
230 rwLock.ExitWriteLock();
236 get {
return Dictionary1.Count; }
241 return Dictionary1.ContainsKey(
key);
246 return Dictionary2.ContainsKey(
key);
252 bool gotLock =
false;
262 rwLock.EnterReadLock();
266 success = Dictionary1.TryGetValue(
key, out value);
271 rwLock.ExitReadLock();
280 bool gotLock =
false;
290 rwLock.EnterReadLock();
294 success = Dictionary2.TryGetValue(
key, out value);
299 rwLock.ExitReadLock();
307 bool gotLock =
false;
317 rwLock.EnterReadLock();
321 foreach (TValue value
in Dictionary1.Values)
327 rwLock.ExitReadLock();
331 public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
333 bool gotLock =
false;
343 rwLock.EnterReadLock();
347 foreach (KeyValuePair<TKey1, TValue> entry
in Dictionary1)
353 rwLock.ExitReadLock();
357 public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
359 bool gotLock =
false;
369 rwLock.EnterReadLock();
373 foreach (KeyValuePair<TKey2, TValue> entry
in Dictionary2)
379 rwLock.ExitReadLock();
385 bool gotLock =
false;
395 rwLock.EnterReadLock();
399 foreach (TValue value
in Dictionary1.Values)
401 if (predicate(value))
408 rwLock.ExitReadLock();
411 return default(TValue);
414 public IList<TValue>
FindAll(Predicate<TValue> predicate)
416 IList<TValue> list =
new List<TValue>();
417 bool gotLock =
false;
427 rwLock.EnterReadLock();
431 foreach (TValue value
in Dictionary1.Values)
433 if (predicate(value))
440 rwLock.ExitReadLock();
448 IList<TKey1> list =
new List<TKey1>();
449 bool gotUpgradeableLock =
false;
459 rwLock.EnterUpgradeableReadLock();
460 gotUpgradeableLock =
true;
463 foreach (KeyValuePair<TKey1, TValue> kvp
in Dictionary1)
465 if (predicate(kvp.Value))
469 IList<TKey2> list2 =
new List<TKey2>(list.Count);
470 foreach (KeyValuePair<TKey2, TValue> kvp
in Dictionary2)
472 if (predicate(kvp.Value))
476 bool gotWriteLock =
false;
483 rwLock.EnterUpgradeableReadLock();
487 for (
int i = 0; i < list.Count; i++)
488 Dictionary1.Remove(list[i]);
490 for (
int i = 0; i < list2.Count; i++)
491 Dictionary2.Remove(list2[i]);
496 rwLock.ExitWriteLock();
501 if (gotUpgradeableLock)
502 rwLock.ExitUpgradeableReadLock();
bool ContainsKey(TKey1 key)
IList< TValue > FindAll(Predicate< TValue > predicate)
TValue FindValue(Predicate< TValue > predicate)
void ForEach(Action< KeyValuePair< TKey2, TValue >> action)
bool ContainsKey(TKey2 key)
bool TryGetValue(TKey1 key, out TValue value)
void ForEach(Action< TValue > action)
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
DoubleDictionaryThreadAbortSafe()
void Add(TKey1 key1, TKey2 key2, TValue value)
bool Remove(TKey1 key1, TKey2 key2)
DoubleDictionaryThreadAbortSafe(int capacity)
void ForEach(Action< KeyValuePair< TKey1, TValue >> action)
bool TryGetValue(TKey2 key, out TValue value)
int RemoveAll(Predicate< TValue > predicate)