wasSharp – Blame information for rev 27

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