using System; using System.Collections.Generic; namespace UIWidgetsSample { public class EquatableUtils { // private DeepCollectionEquality _equality = DeepCollectionEquality(); private int mapPropsToHashCode(List props) { var value = 0; foreach (var prop in props) { value = _combine(value, prop); } return _finish(props == null ? 0 : value); } /// Determines whether [list1] and [list2] are equal. private bool equals(List list1, List list2) { if (list1.Equals(list2)) return true; if (list1 == null || list2 == null) return false; var length = list1.Count; if (length != list2.Count) return false; for (var i = 0; i < length; i++) { var unit1 = list1[i]; var unit2 = list2[i]; if (_isEquatable(unit1) && _isEquatable(unit2)) { if (unit1 != unit2) return false; } else if (unit1 is List || unit1 is Dictionary) { //if (!_equality.equals(unit1, unit2)) if(!Equals(unit1,unit2)) return false; } else if (unit1?.GetType() != unit2?.GetType()) { return false; } else if (unit1 != unit2) { return false; } } return true; } private bool _isEquatable(object _object) { return _object is Equatable || _object is EquatableMixin; } /// Jenkins Hash Functions /// https://en.wikipedia.org/wiki/Jenkins_hash_function private int _combine(int hash, object _object) { if (_object is Dictionary) { var Keys = new List(); foreach (var key in ((Dictionary) _object).Keys) Keys.Add(key); Keys.Sort((a, b) => a.GetHashCode() - b.GetHashCode()); foreach (var key in Keys) { var value = (object) null; var keyValue = ((Dictionary) _object).TryGetValue(key, out value); hash = hash ^ _combine(hash, new Dictionary {{key, value}}); } ; return hash; } if (_object is List) { foreach (var value in (List) _object) hash = hash ^ _combine(hash, value); return hash ^ ((List) _object).Count; } hash = 0x1fffffff & (hash + _object.GetHashCode()); hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); return hash ^ (hash >> 6); } private int _finish(int hash) { hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); hash = hash ^ (hash >> 11); return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); } /// Returns a string for [props]. public static string mapPropsToString(Type runtimeType, List props) { var result = ""; var results = new List(); foreach (var prop in props) results.Add(prop.ToString()); //props.map((prop) => prop.toString()).join(', ') return string.Join(",", results); } } }