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