/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Threading; using wasSharp.Collections.Specialized; namespace wasSharp.Collections.Utilities { public static class CollectionExtensions { /// /// Compares two dictionaries for equality. /// /// key type /// value type /// dictionary to compare /// dictionary to compare to /// true if the dictionaries contain the same elements public static bool ContentEquals(this IDictionary dictionary, IDictionary otherDictionary) { return (dictionary ?? new Dictionary()).Count().Equals( (otherDictionary ?? new Dictionary()).Count()) && (otherDictionary ?? new Dictionary()) .OrderBy(kvp => kvp.Key) .SequenceEqual((dictionary ?? new Dictionary()) .OrderBy(kvp => kvp.Key)); } public static void AddOrReplace(this IDictionary dictionary, TKey key, TValue value) { switch (dictionary.ContainsKey(key)) { case true: dictionary[key] = value; break; default: dictionary.Add(key, value); break; } } public static void AddIfNotExists(this IDictionary dictionary, TKey key, TValue value) { switch (!dictionary.ContainsKey(key)) { case true: dictionary.Add(key, value); break; } } public static ConcurrentMultiKeyDictionary ToMultiKeyDictionary(this IEnumerable items, Func key1, Func key2, Func value) { var dict = new ConcurrentMultiKeyDictionary(); foreach (var i in items) { dict.Add(key1(i), key2(i), value(i)); } return dict; } } }