wasSharp – Diff between revs 18 and 20
?pathlinks?
Rev 18 | Rev 20 | |||
---|---|---|---|---|
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.Collections.Generic; |
8 | using System.Collections.Generic; |
|
- | 9 | using System.Linq; |
||
8 | using System.Linq; |
10 | using wasSharp.Collections.Specialized; |
|
9 | |
11 | |
|
10 | namespace wasSharp.Collections.Utilities |
12 | namespace wasSharp.Collections.Utilities |
|
11 | { |
13 | { |
|
12 | public static class CollectionExtensions |
14 | public static class CollectionExtensions |
|
13 | { |
15 | { |
|
14 | /// <summary> |
16 | /// <summary> |
|
15 | /// Compares two dictionaries for equality. |
17 | /// Compares two dictionaries for equality. |
|
16 | /// </summary> |
18 | /// </summary> |
|
17 | /// <typeparam name="TKey">key type</typeparam> |
19 | /// <typeparam name="TKey">key type</typeparam> |
|
18 | /// <typeparam name="TValue">value type</typeparam> |
20 | /// <typeparam name="TValue">value type</typeparam> |
|
19 | /// <param name="dictionary">dictionary to compare</param> |
21 | /// <param name="dictionary">dictionary to compare</param> |
|
20 | /// <param name="otherDictionary">dictionary to compare to</param> |
22 | /// <param name="otherDictionary">dictionary to compare to</param> |
|
21 | /// <returns>true if the dictionaries contain the same elements</returns> |
23 | /// <returns>true if the dictionaries contain the same elements</returns> |
|
22 | public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
24 | public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
|
23 | IDictionary<TKey, TValue> otherDictionary) |
25 | IDictionary<TKey, TValue> otherDictionary) |
|
24 | { |
26 | { |
|
25 | return |
27 | return |
|
26 | (dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
28 | (dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
|
27 | (otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
29 | (otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
|
28 | (otherDictionary ?? new Dictionary<TKey, TValue>()) |
30 | (otherDictionary ?? new Dictionary<TKey, TValue>()) |
|
29 | .OrderBy(kvp => kvp.Key) |
31 | .OrderBy(kvp => kvp.Key) |
|
30 | .SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
32 | .SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
|
31 | .OrderBy(kvp => kvp.Key)); |
33 | .OrderBy(kvp => kvp.Key)); |
|
32 | } |
34 | } |
|
33 | |
35 | |
|
34 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
36 | public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
|
35 | { |
37 | { |
|
36 | switch (dictionary.ContainsKey(key)) |
38 | switch (dictionary.ContainsKey(key)) |
|
37 | { |
39 | { |
|
38 | case true: |
40 | case true: |
|
39 | dictionary[key] = value; |
41 | dictionary[key] = value; |
|
40 | break; |
42 | break; |
|
41 | default: |
43 | default: |
|
42 | dictionary.Add(key, value); |
44 | dictionary.Add(key, value); |
|
43 | break; |
45 | break; |
|
44 | } |
46 | } |
|
45 | } |
47 | } |
|
46 | |
48 | |
|
47 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
49 | public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
|
48 | { |
50 | { |
|
49 | switch (!dictionary.ContainsKey(key)) |
51 | switch (!dictionary.ContainsKey(key)) |
|
50 | { |
52 | { |
|
51 | case true: |
53 | case true: |
|
52 | dictionary.Add(key, value); |
54 | dictionary.Add(key, value); |
|
53 | break; |
55 | break; |
|
54 | } |
56 | } |
|
55 | } |
57 | } |
|
- | 58 | |
||
- | 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) |
||
- | 60 | { |
||
- | 61 | var dict = new MultiKeyDictionary<K1, K2, V>(); |
||
- | 62 | foreach (S i in items) |
||
- | 63 | { |
||
- | 64 | dict.Add(key1(i), key2(i), value(i)); |
||
- | 65 | } |
||
- | 66 | return dict; |
||
- | 67 | } |
||
56 | } |
68 | } |
|
57 | } |
69 | } |
|
58 | |
70 | |
|
59 |
|
71 |
|
|
60 | |
72 | |
|
61 | |
73 | |
|
62 | |
74 | |