wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 38  →  ?path2? @ 39
/Collections/Specialized/MultiKeyDictionary.cs
@@ -5,61 +5,94 @@
///////////////////////////////////////////////////////////////////////////
// Based on the work of Herman Schoenfeld
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
 
namespace wasSharp.Collections.Specialized
{
public class MultiKeyDictionary<K1, K2, V> : Dictionary<K1, Dictionary<K2, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2]
{
get
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
throw new ArgumentOutOfRangeException();
return base[key1][key2];
{
SyncRoot.ExitReadLock();
return default(V);
}
var v = base[key1][key2];
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
 
this[key1][key2] = value;
SyncRoot.ExitWriteLock();
}
}
 
public new IEnumerable<V> Values =>
base.Values.SelectMany(baseDict => baseDict.Keys, (baseDict, baseKey) => baseDict[baseKey]);
public new IEnumerable<V> Values
{
get
{
SyncRoot.EnterReadLock();
var v = base.Values.SelectMany(baseDict => baseDict.Keys, (baseDict, baseKey) => baseDict[baseKey]);
SyncRoot.ExitReadLock();
return v;
}
}
 
public void Add(K1 key1, K2 key2, V value)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
 
this[key1][key2] = value;
SyncRoot.ExitWriteLock();
}
 
public void Remove(K1 key1, K2 key2)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
{
SyncRoot.ExitWriteLock();
return;
}
this[key1].Remove(key2);
Remove(key1);
SyncRoot.ExitWriteLock();
}
 
public bool ContainsKey(K1 key1, K2 key2)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2);
SyncRoot.ExitReadLock();
return c;
}
 
public bool TryGetValue(K1 key1, K2 key2, out V value)
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2))
{
SyncRoot.ExitReadLock();
value = default(V);
return false;
}
value = base[key1][key2];
SyncRoot.ExitReadLock();
return true;
}
}
@@ -66,46 +99,69 @@
 
public class MultiKeyDictionary<K1, K2, K3, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3]
{
get { return ContainsKey(key1) ? this[key1][key2, key3] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
SyncRoot.ExitReadLock();
return c;
}
 
public void Add(K1 key1, K2 key2, K3 key3, V value)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, V>();
this[key1][key2, key3] = value;
SyncRoot.ExitWriteLock();
}
 
public void Remove(K1 key1, K2 key2, K3 key3)
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2, key3))
{
SyncRoot.ExitWriteLock();
return;
}
this[key1][key2].Remove(key3);
this[key1].Remove(key2);
Remove(key1);
SyncRoot.ExitWriteLock();
}
 
public bool TryGetValue(K1 key1, K2 key2, K3 key3, out V value)
{
SyncRoot.EnterReadLock();
if (!ContainsKey(key1) || !this[key1].ContainsKey(key2, key3))
{
SyncRoot.ExitReadLock();
value = default(V);
return false;
}
value = base[key1][key2, key3];
SyncRoot.ExitReadLock();
return true;
}
}
@@ -112,39 +168,65 @@
 
public class MultiKeyDictionary<K1, K2, K3, K4, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, V>();
this[key1][key2, key3, key4] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
SyncRoot.ExitReadLock();
return c;
}
}
 
public class MultiKeyDictionary<K1, K2, K3, K4, K5, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, V>();
this[key1][key2, key3, key4, key5] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -151,20 +233,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, V>();
this[key1][key2, key3, key4, key5, key6] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -171,20 +266,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>();
this[key1][key2, key3, key4, key5, key6, key7] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -191,20 +299,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -211,20 +332,33 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9]
{
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V); }
get
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9] = value;
SyncRoot.ExitWriteLock();
}
}
 
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9)
{
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -231,17 +365,24 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10]
{
get
{
return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V);
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] = value;
SyncRoot.ExitWriteLock();
}
}
 
@@ -248,8 +389,11 @@
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9,
K10 key10)
{
return ContainsKey(key1) &&
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -256,20 +400,27 @@
public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V> :
Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>>
{
private readonly ReaderWriterLockSlim SyncRoot = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
public V this[
K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10, K11 key11]
{
get
{
return ContainsKey(key1)
SyncRoot.EnterReadLock();
var v = ContainsKey(key1)
? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11]
: default(V);
SyncRoot.ExitReadLock();
return v;
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>();
this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11] = value;
SyncRoot.ExitWriteLock();
}
}
 
@@ -276,8 +427,11 @@
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9,
K10 key10, K11 key11)
{
return ContainsKey(key1) &&
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10, key11);
SyncRoot.ExitReadLock();
return c;
}
}
}