/Collections/Utilities/CollectionExtensions.cs |
@@ -33,6 +33,35 @@ |
.OrderBy(kvp => kvp.Key)); |
} |
|
public static void UnionWith<TKey, TValue>(this IDictionary<TKey, TValue> target, |
IDictionary<TKey, TValue> source) |
{ |
var LockObject = new object(); |
source.AsParallel().ForAll(o => |
{ |
bool hasElement; |
lock (LockObject) |
{ |
hasElement = target.ContainsKey(o.Key); |
} |
switch (hasElement) |
{ |
case true: |
lock (LockObject) |
{ |
target[o.Key] = o.Value; |
} |
break; |
default: |
lock (LockObject) |
{ |
target.Add(o.Key, o.Value); |
} |
break; |
} |
}); |
} |
|
public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
{ |
switch (dictionary.ContainsKey(key)) |
@@ -46,7 +75,8 @@ |
} |
} |
|
public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, |
TValue value) |
{ |
switch (!dictionary.ContainsKey(key)) |
{ |
@@ -56,10 +86,11 @@ |
} |
} |
|
public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) |
public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, |
Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) |
{ |
var dict = new MultiKeyDictionary<K1, K2, V>(); |
foreach (S i in items) |
foreach (var i in items) |
{ |
dict.Add(key1(i), key2(i), value(i)); |
} |