Skip to content

Commit bce9716

Browse files
Added LockSynchronizedOrderedDictionary
1 parent 0b2c6d8 commit bce9716

3 files changed

Lines changed: 61 additions & 6 deletions

File tree

benchmarking/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static void DictionaryTests()
198198
// _ => () => new ReadWriteSynchronizedDictionaryWrapper<int, object>());
199199

200200
report.AddBenchmark("LockSynchronized OrderedDictionary",
201-
_ => () => new LockSynchronizedDictionaryWrapper<int, object>(new OrderedDictionary<int, object>()));
201+
_ => () => new LockSynchronizedOrderedDictionary<int, object>());
202202
//report.AddBenchmark("ReadWriteSynchronized OrderedDictionary",
203203
// _ => () => new ReadWriteSynchronizedDictionaryWrapper<int, object>(new OrderedDictionary<int, object>()));
204204

source/Synchronized/LockSynchronizedDictionaryWrapper.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Open.Collections.Synchronized;
66

77
/// <inheritdoc />
8-
public class LockSynchronizedDictionaryWrapper<TKey, TValue>
9-
: LockSynchronizedCollectionWrapper<KeyValuePair<TKey, TValue>, IDictionary<TKey, TValue>>, IDictionary<TKey, TValue>
8+
public abstract class LockSynchronizedDictionaryWrapper<TKey, TValue, TDictionary>
9+
: LockSynchronizedCollectionWrapper<KeyValuePair<TKey, TValue>, TDictionary>, IDictionary<TKey, TValue>
10+
where TDictionary : class, IDictionary<TKey, TValue>
1011
{
1112
/// <inheritdoc />
12-
public LockSynchronizedDictionaryWrapper(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
13-
14-
public LockSynchronizedDictionaryWrapper() : this(new Dictionary<TKey, TValue>()) { }
13+
public LockSynchronizedDictionaryWrapper(TDictionary dictionary) : base(dictionary) { }
1514

1615
/// <inheritdoc />
1716
[ExcludeFromCodeCoverage]
@@ -64,3 +63,14 @@ public virtual bool Remove(TKey key)
6463
public bool TryGetValue(TKey key, out TValue value)
6564
=> InternalSource.TryGetValue(key, out value);
6665
}
66+
67+
68+
public class LockSynchronizedDictionaryWrapper<TKey, TValue>
69+
: LockSynchronizedDictionaryWrapper<TKey, TValue, IDictionary<TKey, TValue>>
70+
{
71+
public LockSynchronizedDictionaryWrapper(IDictionary<TKey, TValue> dictionary) : base(dictionary)
72+
{
73+
}
74+
75+
public LockSynchronizedDictionaryWrapper(int capacity = 0) : this(new Dictionary<TKey, TValue>(capacity)) { }
76+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Open.Collections.Synchronized;
2+
3+
/// <inheritdoc />
4+
public class LockSynchronizedOrderedDictionary<TKey, TValue>
5+
: LockSynchronizedDictionaryWrapper<TKey, TValue, OrderedDictionary<TKey, TValue>>, IOrderedDictionary<TKey, TValue>
6+
{
7+
/// <inheritdoc />
8+
public LockSynchronizedOrderedDictionary(int capacity = 0) : base(new OrderedDictionary<TKey, TValue>(capacity)) { }
9+
10+
/// <inheritdoc />
11+
public TKey GetKeyAt(int index) => InternalSource.GetKeyAt(index);
12+
13+
/// <inheritdoc />
14+
public TValue GetValueAt(int index) => InternalSource.GetValueAt(index);
15+
16+
/// <inheritdoc />
17+
public void Insert(int index, TKey key, TValue value)
18+
{
19+
lock(Sync) InternalSource.Insert(index, key, value);
20+
}
21+
22+
/// <inheritdoc />
23+
public void RemoveAt(int index)
24+
{
25+
lock (Sync) InternalSource.RemoveAt(index);
26+
}
27+
28+
/// <inheritdoc />
29+
public int SetValue(TKey key, TValue value)
30+
{
31+
lock (Sync) return InternalSource.SetValue(key, value);
32+
}
33+
34+
/// <inheritdoc />
35+
public bool SetValueAt(int index, TValue value)
36+
{
37+
lock (Sync) return InternalSource.SetValueAt(index, value);
38+
}
39+
40+
/// <inheritdoc />
41+
public new int Add(TKey key, TValue value)
42+
{
43+
lock (Sync) return InternalSource.Add(key, value);
44+
}
45+
}

0 commit comments

Comments
 (0)