|
| 1 | +// |
| 2 | +// CollectionEquality.cs |
| 3 | +// |
| 4 | +// This file is auto-generated by Amplitude. |
| 5 | +// To update run 'ampli pull visual-studio-plugin' |
| 6 | +// |
| 7 | +// Questions? We're here to help: |
| 8 | +// https://developers.data.amplitude.com/dotnet |
| 9 | +// |
| 10 | + |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | + |
| 14 | +namespace Iteratively |
| 15 | +{ |
| 16 | + partial class CollectionUtils |
| 17 | + { |
| 18 | + private static EqualityComparer<object> _valueComparer = EqualityComparer<object>.Default; |
| 19 | + |
| 20 | + private static bool isDictionary(object maybeDictionary) => |
| 21 | + maybeDictionary.GetType().Equals(typeof(Dictionary<string, object>)); |
| 22 | + |
| 23 | + private static Dictionary<string, object> asDictionary(object o) => |
| 24 | + o as Dictionary<string, object>; |
| 25 | + |
| 26 | + private static ICollection<object> asCollection(object o) => |
| 27 | + o as ICollection<object>; |
| 28 | + |
| 29 | + private static List<object> asList(object o) => |
| 30 | + o as List<object>; |
| 31 | + |
| 32 | + public static int GetHashCode(object obj) |
| 33 | + { |
| 34 | + int code = 0; |
| 35 | + |
| 36 | + if (obj == null) |
| 37 | + return code; |
| 38 | + |
| 39 | + var aDictionary = asDictionary(obj); |
| 40 | + var aCollection = asCollection(obj); |
| 41 | + |
| 42 | + if (aDictionary != null) |
| 43 | + foreach(var kvp in aDictionary) { |
| 44 | + code += kvp.Key.GetHashCode(); |
| 45 | + code += GetHashCode(kvp.Value); |
| 46 | + } |
| 47 | + else if (aCollection != null) |
| 48 | + foreach(var item in aCollection) |
| 49 | + code += GetHashCode(item); |
| 50 | + else |
| 51 | + code += obj.GetHashCode(); |
| 52 | + |
| 53 | + return code; |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// |
| 58 | + /// There is no pre-given implementation of .Equals |
| 59 | + /// that deeply compares based on value instead of |
| 60 | + /// reference. The standard .Equals only returns |
| 61 | + /// true if the two Dictionaries are the same object |
| 62 | + /// |
| 63 | + /// </summary> |
| 64 | + /// <param name="a"></param> |
| 65 | + /// <param name="b"></param> |
| 66 | + /// <returns></returns> |
| 67 | + /// <remarks></remarks> |
| 68 | + public static bool CollectionsAreEqual(object a, object b) |
| 69 | + { |
| 70 | + if ((a.Equals(b))) |
| 71 | + return true; |
| 72 | + if ((a == null | b == null)) |
| 73 | + return true; |
| 74 | + var aCollection = asCollection(a); |
| 75 | + var bCollection = asCollection(b); |
| 76 | + if (aCollection != null && bCollection != null |
| 77 | + && aCollection.Count() != bCollection.Count()) |
| 78 | + return false; |
| 79 | + if ((!a.GetType().Equals(b.GetType()))) |
| 80 | + return false; |
| 81 | + |
| 82 | + var aDictionary = asDictionary(a); |
| 83 | + if (aDictionary != null) |
| 84 | + return CollectionDictionariesAreEqualHelper( |
| 85 | + aDictionary, asDictionary(b) |
| 86 | + ); |
| 87 | + |
| 88 | + var aList = asList(a); |
| 89 | + if (aList != null) |
| 90 | + return CollectionListsAreEqualHelper( |
| 91 | + aList, asList(b) |
| 92 | + ); |
| 93 | + |
| 94 | + if (aCollection != null) |
| 95 | + return CollectionListsAreEqualHelper( |
| 96 | + aCollection.ToList(), bCollection.ToList() |
| 97 | + ); |
| 98 | + |
| 99 | + // Nothing to iterate over, we should have returned before getting here |
| 100 | + return a.Equals(b); |
| 101 | + } |
| 102 | + |
| 103 | + private static bool CollectionDictionariesAreEqualHelper(Dictionary<string, object> a, Dictionary<string, object> b) |
| 104 | + { |
| 105 | + bool result = true; |
| 106 | + |
| 107 | + foreach (KeyValuePair<string, object> kvp in a) |
| 108 | + { |
| 109 | + object valueA = kvp.Value; |
| 110 | + object valueB; |
| 111 | + |
| 112 | + // if the second dictionary has the key |
| 113 | + if ((!b.TryGetValue(kvp.Key, out valueB))) |
| 114 | + return false; |
| 115 | + |
| 116 | + // We don't care what a and b are, but the CollectionsAreEqual |
| 117 | + // Function should take care of every case of object |
| 118 | + result = (result & CollectionsAreEqual(valueA, valueB)); |
| 119 | + |
| 120 | + // Terminate if this loop caused an inequality |
| 121 | + if ((!result)) |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | + private static bool CollectionListsAreEqualHelper(List<object> a, List<object> b) |
| 129 | + { |
| 130 | + bool result = true; |
| 131 | + |
| 132 | + int i; |
| 133 | + for (i = 0; i <= a.Count - 1; i++) |
| 134 | + { |
| 135 | + object valueA = a[i]; |
| 136 | + object valueB = b[i]; |
| 137 | + |
| 138 | + // We don't care what a and b are, but the CollectionsAreEqual |
| 139 | + // Function should take care of every case of object |
| 140 | + result = (result & CollectionsAreEqual(valueA, valueB)); |
| 141 | + if ((!result)) |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return result; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments