wasSharp – Diff between revs 20 and 24
?pathlinks?
Rev 20 | Rev 24 | |||
---|---|---|---|---|
Line 31... | Line 31... | |||
31 | .OrderBy(kvp => kvp.Key) |
31 | .OrderBy(kvp => kvp.Key) |
|
32 | .SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
32 | .SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
|
33 | .OrderBy(kvp => kvp.Key)); |
33 | .OrderBy(kvp => kvp.Key)); |
|
34 | } |
34 | } |
|
Line -... | Line 35... | |||
- | 35 | |
||
- | 36 | public static void UnionWith<TKey, TValue>(this IDictionary<TKey, TValue> target, |
||
- | 37 | IDictionary<TKey, TValue> source) |
||
- | 38 | { |
||
- | 39 | var LockObject = new object(); |
||
- | 40 | source.AsParallel().ForAll(o => |
||
- | 41 | { |
||
- | 42 | bool hasElement; |
||
- | 43 | lock (LockObject) |
||
- | 44 | { |
||
- | 45 | hasElement = target.ContainsKey(o.Key); |
||
- | 46 | } |
||
- | 47 | switch (hasElement) |
||
- | 48 | { |
||
- | 49 | case true: |
||
- | 50 | lock (LockObject) |
||
- | 51 | { |
||
- | 52 | target[o.Key] = o.Value; |
||
- | 53 | } |
||
- | 54 | break; |
||
- | 55 | default: |
||
- | 56 | lock (LockObject) |
||
- | 57 | { |
||
- | 58 | target.Add(o.Key, o.Value); |
||
- | 59 | } |
||
- | 60 | break; |
||
- | 61 | } |
||
- | 62 | }); |
||
- | 63 | } |
||
35 | |
64 | |
|
36 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
65 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
|
37 | { |
66 | { |
|
38 | switch (dictionary.ContainsKey(key)) |
67 | switch (dictionary.ContainsKey(key)) |
|
39 | { |
68 | { |
|
Line 44... | Line 73... | |||
44 | dictionary.Add(key, value); |
73 | dictionary.Add(key, value); |
|
45 | break; |
74 | break; |
|
46 | } |
75 | } |
|
47 | } |
76 | } |
|
Line 48... | Line 77... | |||
48 | |
77 | |
|
- | 78 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, |
||
49 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
79 | TValue value) |
|
50 | { |
80 | { |
|
51 | switch (!dictionary.ContainsKey(key)) |
81 | switch (!dictionary.ContainsKey(key)) |
|
52 | { |
82 | { |
|
53 | case true: |
83 | case true: |
|
54 | dictionary.Add(key, value); |
84 | dictionary.Add(key, value); |
|
55 | break; |
85 | break; |
|
56 | } |
86 | } |
|
Line 57... | Line 87... | |||
57 | } |
87 | } |
|
- | 88 | |
||
58 | |
89 | public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, |
|
59 | 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) |
90 | Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) |
|
60 | { |
91 | { |
|
61 | var dict = new MultiKeyDictionary<K1, K2, V>(); |
92 | var dict = new MultiKeyDictionary<K1, K2, V>(); |
|
62 | foreach (S i in items) |
93 | foreach (var i in items) |
|
63 | { |
94 | { |
|
64 | dict.Add(key1(i), key2(i), value(i)); |
95 | dict.Add(key1(i), key2(i), value(i)); |
|
65 | } |
96 | } |