wasSharp

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 39  →  ?path2? @ 38
/Collections/Specialized/MultiKeyDictionary.cs
@@ -5,94 +5,61 @@
///////////////////////////////////////////////////////////////////////////
// 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))
{
SyncRoot.ExitReadLock();
return default(V);
}
var v = base[key1][key2];
SyncRoot.ExitReadLock();
return v;
throw new ArgumentOutOfRangeException();
return base[key1][key2];
}
set
{
SyncRoot.EnterWriteLock();
if (!ContainsKey(key1))
this[key1] = new Dictionary<K2, V>();
 
this[key1][key2] = value;
SyncRoot.ExitWriteLock();
}
}
 
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 new IEnumerable<V> Values =>
base.Values.SelectMany(baseDict => baseDict.Keys, (baseDict, baseKey) => baseDict[baseKey]);
 
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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2);
}
 
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;
}
}
@@ -99,69 +66,46 @@
 
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3);
}
 
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;
}
}
@@ -168,65 +112,39 @@
 
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4);
}
}
 
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5);
}
}
 
@@ -233,33 +151,20 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6);
}
}
 
@@ -266,33 +171,20 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7);
}
}
 
@@ -299,33 +191,20 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8);
}
}
 
@@ -332,33 +211,20 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V);
SyncRoot.ExitReadLock();
return v;
}
get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
SyncRoot.ExitReadLock();
return c;
return ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9);
}
}
 
@@ -365,24 +231,17 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V);
SyncRoot.ExitReadLock();
return v;
return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(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();
}
}
 
@@ -389,11 +248,8 @@
public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9,
K10 key10)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
return ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10);
SyncRoot.ExitReadLock();
return c;
}
}
 
@@ -400,27 +256,20 @@
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
{
SyncRoot.EnterReadLock();
var v = ContainsKey(key1)
return 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();
}
}
 
@@ -427,11 +276,8 @@
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)
{
SyncRoot.EnterReadLock();
var c = ContainsKey(key1) &&
return ContainsKey(key1) &&
this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10, key11);
SyncRoot.ExitReadLock();
return c;
}
}
}