wasSharp – Blame information for rev 18

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  
7 using System.Collections.Generic;
8 using System.Linq;
9  
10 namespace wasSharp.Collections.Utilities
11 {
12 public static class CollectionExtensions
13 {
14 /// <summary>
15 /// Compares two dictionaries for equality.
16 /// </summary>
17 /// <typeparam name="TKey">key type</typeparam>
18 /// <typeparam name="TValue">value type</typeparam>
19 /// <param name="dictionary">dictionary to compare</param>
20 /// <param name="otherDictionary">dictionary to compare to</param>
21 /// <returns>true if the dictionaries contain the same elements</returns>
22 public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary,
23 IDictionary<TKey, TValue> otherDictionary)
24 {
25 return
26 (dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals(
27 (otherDictionary ?? new Dictionary<TKey, TValue>()).Count) &&
28 (otherDictionary ?? new Dictionary<TKey, TValue>())
29 .OrderBy(kvp => kvp.Key)
30 .SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>())
31 .OrderBy(kvp => kvp.Key));
32 }
33  
34 public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
35 {
36 switch (dictionary.ContainsKey(key))
37 {
38 case true:
39 dictionary[key] = value;
40 break;
41 default:
42 dictionary.Add(key, value);
43 break;
44 }
45 }
46  
47 public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
48 {
49 switch (!dictionary.ContainsKey(key))
50 {
51 case true:
52 dictionary.Add(key, value);
53 break;
54 }
55 }
56 }
57 }