/* * Copyright 2018 faddenSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace CommonUtil { public class Container { /// /// Compares two lists of strings to see if their contents are equal. The lists /// must contain the same strings, in the same order. /// /// List #1. /// List #2. /// String comparer (e.g. StringComparer.InvariantCulture). If /// null, the default string comparer is used. /// True if the lists are equal. public static bool StringListEquals(IList l1, IListl2, StringComparer comparer) { // Quick check for reference equality. if (l1 == l2) { return true; } return Enumerable.SequenceEqual(l1, l2, comparer); } /// /// Makes a deep copy of a string list. /// /// String list to copy. /// New string list. public static List CopyStringList(IList src) { List dst = new List(src.Count); foreach (string str in src) { dst.Add(str); } return dst; } /// /// Compares two Dictionaries to see if their contents are equal. Key and value types /// must have correctly-implemented equality checks. (I contend this works incorrectly /// for float -- 5.0f is equal to the integer 5.) /// /// /// https://stackoverflow.com/q/3804367/294248 /// /// TODO: make this work right for float/int comparisons /// /// Dictionary key type. /// Dictionary value type. /// Dictionary #1. /// Dictionary #2. /// True if equal, false if not. public static bool CompareDicts( Dictionary dict1, Dictionary dict2) { if (dict1 == dict2) { return true; } if (dict1 == null || dict2 == null) { return false; } if (dict1.Count != dict2.Count) { return false; } #if false var valueComparer = EqualityComparer.Default; foreach (var kvp in dict1) { TValue value2; if (!dict2.TryGetValue(kvp.Key, out value2)) return false; if (!valueComparer.Equals(kvp.Value, value2)) return false; } return true; #else // Check to see if there are any elements in the first that are not in the second. return !dict1.Except(dict2).Any(); #endif } public static bool CompareDicts( ReadOnlyDictionary dict1, ReadOnlyDictionary dict2) { if (dict1 == dict2) { return true; } if (dict1 == null || dict2 == null) { return false; } if (dict1.Count != dict2.Count) { return false; } // Check to see if there are any elements in the first that are not in the second. return !dict1.Except(dict2).Any(); } } }