wasSharp – Diff between revs 24 and 27
?pathlinks?
Rev 24 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
9 | using System.Linq; |
9 | using System.Linq; |
|
10 | using wasSharp.Collections.Specialized; |
10 | using wasSharp.Collections.Specialized; |
|
11 | |
11 | |
|
12 | namespace wasSharp.Collections.Utilities |
12 | namespace wasSharp.Collections.Utilities |
|
13 | { |
13 | { |
|
14 | public static class CollectionExtensions |
14 | public static class CollectionExtensions |
|
15 | { |
15 | { |
|
16 | /// <summary> |
16 | /// <summary> |
|
17 | /// Compares two dictionaries for equality. |
17 | /// Compares two dictionaries for equality. |
|
18 | /// </summary> |
18 | /// </summary> |
|
19 | /// <typeparam name="TKey">key type</typeparam> |
19 | /// <typeparam name="TKey">key type</typeparam> |
|
20 | /// <typeparam name="TValue">value type</typeparam> |
20 | /// <typeparam name="TValue">value type</typeparam> |
|
21 | /// <param name="dictionary">dictionary to compare</param> |
21 | /// <param name="dictionary">dictionary to compare</param> |
|
22 | /// <param name="otherDictionary">dictionary to compare to</param> |
22 | /// <param name="otherDictionary">dictionary to compare to</param> |
|
23 | /// <returns>true if the dictionaries contain the same elements</returns> |
23 | /// <returns>true if the dictionaries contain the same elements</returns> |
|
24 | public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
24 | public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
|
25 | IDictionary<TKey, TValue> otherDictionary) |
25 | IDictionary<TKey, TValue> otherDictionary) |
|
26 | { |
26 | { |
|
27 | return |
27 | return |
|
28 | (dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
28 | (dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
|
29 | (otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
29 | (otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
|
30 | (otherDictionary ?? new Dictionary<TKey, TValue>()) |
30 | (otherDictionary ?? new Dictionary<TKey, TValue>()) |
|
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 | } |
|
35 | |
35 | |
|
36 | public static void UnionWith<TKey, TValue>(this IDictionary<TKey, TValue> target, |
36 | public static void UnionWith<TKey, TValue>(this IDictionary<TKey, TValue> target, |
|
37 | IDictionary<TKey, TValue> source) |
37 | IDictionary<TKey, TValue> source) |
|
38 | { |
38 | { |
|
39 | var LockObject = new object(); |
39 | var LockObject = new object(); |
|
40 | source.AsParallel().ForAll(o => |
40 | source.AsParallel().ForAll(o => |
|
41 | { |
41 | { |
|
42 | bool hasElement; |
42 | bool hasElement; |
|
43 | lock (LockObject) |
43 | lock (LockObject) |
|
44 | { |
44 | { |
|
45 | hasElement = target.ContainsKey(o.Key); |
45 | hasElement = target.ContainsKey(o.Key); |
|
46 | } |
46 | } |
|
47 | switch (hasElement) |
47 | switch (hasElement) |
|
48 | { |
48 | { |
|
49 | case true: |
49 | case true: |
|
50 | lock (LockObject) |
50 | lock (LockObject) |
|
51 | { |
51 | { |
|
52 | target[o.Key] = o.Value; |
52 | target[o.Key] = o.Value; |
|
53 | } |
53 | } |
|
54 | break; |
54 | break; |
|
- | 55 | |
||
55 | default: |
56 | default: |
|
56 | lock (LockObject) |
57 | lock (LockObject) |
|
57 | { |
58 | { |
|
58 | target.Add(o.Key, o.Value); |
59 | target.Add(o.Key, o.Value); |
|
59 | } |
60 | } |
|
60 | break; |
61 | break; |
|
61 | } |
62 | } |
|
62 | }); |
63 | }); |
|
63 | } |
64 | } |
|
64 | |
65 | |
|
65 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
66 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
|
66 | { |
67 | { |
|
67 | switch (dictionary.ContainsKey(key)) |
68 | switch (dictionary.ContainsKey(key)) |
|
68 | { |
69 | { |
|
69 | case true: |
70 | case true: |
|
70 | dictionary[key] = value; |
71 | dictionary[key] = value; |
|
71 | break; |
72 | break; |
|
- | 73 | |
||
72 | default: |
74 | default: |
|
73 | dictionary.Add(key, value); |
75 | dictionary.Add(key, value); |
|
74 | break; |
76 | break; |
|
75 | } |
77 | } |
|
76 | } |
78 | } |
|
77 | |
79 | |
|
78 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, |
80 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, |
|
79 | TValue value) |
81 | TValue value) |
|
80 | { |
82 | { |
|
81 | switch (!dictionary.ContainsKey(key)) |
83 | switch (!dictionary.ContainsKey(key)) |
|
82 | { |
84 | { |
|
83 | case true: |
85 | case true: |
|
84 | dictionary.Add(key, value); |
86 | dictionary.Add(key, value); |
|
85 | break; |
87 | break; |
|
86 | } |
88 | } |
|
87 | } |
89 | } |
|
88 | |
90 | |
|
89 | public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, |
91 | public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, |
|
90 | Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) |
92 | Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) |
|
91 | { |
93 | { |
|
92 | var dict = new MultiKeyDictionary<K1, K2, V>(); |
94 | var dict = new MultiKeyDictionary<K1, K2, V>(); |
|
93 | foreach (var i in items) |
95 | foreach (var i in items) |
|
94 | { |
96 | { |
|
95 | dict.Add(key1(i), key2(i), value(i)); |
97 | dict.Add(key1(i), key2(i), value(i)); |
|
96 | } |
98 | } |
|
97 | return dict; |
99 | return dict; |
|
98 | } |
100 | } |
|
99 | } |
101 | } |
|
100 | } |
- | ||
101 | |
102 | } |
|
- | 103 | |
||
102 |
|
- | ||
103 | |
- | ||
104 | |
- | ||
105 | |
- |