| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library deep_equals; | 5 library yaml.deep_equals; |
| 6 | 6 |
| 7 /// Returns whether two objects are structurally equivalent. | 7 /// Returns whether two objects are structurally equivalent. |
| 8 /// | 8 /// |
| 9 /// This considers `NaN` values to be equivalent. It also handles | 9 /// This considers `NaN` values to be equivalent. It also handles |
| 10 /// self-referential structures. | 10 /// self-referential structures. |
| 11 bool deepEquals(obj1, obj2, [List parents1, List parents2]) { | 11 bool deepEquals(obj1, obj2) => new _DeepEquals().equals(obj1, obj2); |
| 12 if (identical(obj1, obj2)) return true; | 12 |
| 13 if (parents1 == null) { | 13 /// A class that provides access to the list of parent objects used for loop |
| 14 parents1 = []; | 14 /// detection. |
| 15 parents2 = []; | 15 class _DeepEquals { |
| 16 final _parents1 = []; |
| 17 final _parents2 = []; |
| 18 |
| 19 /// Returns whether [obj1] and [obj2] are structurally equivalent. |
| 20 bool equals(obj1, obj2) { |
| 21 // _parents1 and _parents2 are guaranteed to be the same size. |
| 22 for (var i = 0; i < _parents1.length; i++) { |
| 23 var loop1 = identical(obj1, _parents1[i]); |
| 24 var loop2 = identical(obj2, _parents2[i]); |
| 25 // If both structures loop in the same place, they're equal at that point |
| 26 // in the structure. If one loops and the other doesn't, they're not |
| 27 // equal. |
| 28 if (loop1 && loop2) return true; |
| 29 if (loop1 || loop2) return false; |
| 30 } |
| 31 |
| 32 _parents1.add(obj1); |
| 33 _parents2.add(obj2); |
| 34 try { |
| 35 if (obj1 is List && obj2 is List) { |
| 36 return _listEquals(obj1, obj2); |
| 37 } else if (obj1 is Map && obj2 is Map) { |
| 38 return _mapEquals(obj1, obj2); |
| 39 } else if (obj1 is num && obj2 is num) { |
| 40 return _numEquals(obj1, obj2); |
| 41 } else { |
| 42 return obj1 == obj2; |
| 43 } |
| 44 } finally { |
| 45 _parents1.removeLast(); |
| 46 _parents2.removeLast(); |
| 47 } |
| 16 } | 48 } |
| 17 | 49 |
| 18 // parents1 and parents2 are guaranteed to be the same size. | 50 /// Returns whether [list1] and [list2] are structurally equal. |
| 19 for (var i = 0; i < parents1.length; i++) { | 51 bool _listEquals(List list1, List list2) { |
| 20 var loop1 = identical(obj1, parents1[i]); | 52 if (list1.length != list2.length) return false; |
| 21 var loop2 = identical(obj2, parents2[i]); | 53 |
| 22 // If both structures loop in the same place, they're equal at that point in | 54 for (var i = 0; i < list1.length; i++) { |
| 23 // the structure. If one loops and the other doesn't, they're not equal. | 55 if (!equals(list1[i], list2[i])) return false; |
| 24 if (loop1 && loop2) return true; | 56 } |
| 25 if (loop1 || loop2) return false; | 57 |
| 58 return true; |
| 26 } | 59 } |
| 27 | 60 |
| 28 parents1.add(obj1); | 61 /// Returns whether [map1] and [map2] are structurally equal. |
| 29 parents2.add(obj2); | 62 bool _mapEquals(Map map1, Map map2) { |
| 30 try { | 63 if (map1.length != map2.length) return false; |
| 31 if (obj1 is List && obj2 is List) { | 64 |
| 32 return _listEquals(obj1, obj2, parents1, parents2); | 65 for (var key in map1.keys) { |
| 33 } else if (obj1 is Map && obj2 is Map) { | 66 if (!map2.containsKey(key)) return false; |
| 34 return _mapEquals(obj1, obj2, parents1, parents2); | 67 if (!equals(map1[key], map2[key])) return false; |
| 35 } else if (obj1 is num && obj2 is num) { | |
| 36 return _numEquals(obj1, obj2); | |
| 37 } else { | |
| 38 return obj1 == obj2; | |
| 39 } | 68 } |
| 40 } finally { | 69 |
| 41 parents1.removeLast(); | 70 return true; |
| 42 parents2.removeLast(); | 71 } |
| 72 |
| 73 /// Returns whether two numbers are equivalent. |
| 74 /// |
| 75 /// This differs from `n1 == n2` in that it considers `NaN` to be equal to |
| 76 /// itself. |
| 77 bool _numEquals(num n1, num n2) { |
| 78 if (n1.isNaN && n2.isNaN) return true; |
| 79 return n1 == n2; |
| 43 } | 80 } |
| 44 } | 81 } |
| 45 | |
| 46 /// Returns whether [list1] and [list2] are structurally equal. | |
| 47 bool _listEquals(List list1, List list2, List parents1, List parents2) { | |
| 48 if (list1.length != list2.length) return false; | |
| 49 | |
| 50 for (var i = 0; i < list1.length; i++) { | |
| 51 if (!deepEquals(list1[i], list2[i], parents1, parents2)) return false; | |
| 52 } | |
| 53 | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 /// Returns whether [map1] and [map2] are structurally equal. | |
| 58 bool _mapEquals(Map map1, Map map2, List parents1, List parents2) { | |
| 59 if (map1.length != map2.length) return false; | |
| 60 | |
| 61 for (var key in map1.keys) { | |
| 62 if (!map2.containsKey(key)) return false; | |
| 63 if (!deepEquals(map1[key], map2[key], parents1, parents2)) return false; | |
| 64 } | |
| 65 | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 /// Returns whether two numbers are equivalent. | |
| 70 /// | |
| 71 /// This differs from `n1 == n2` in that it considers `NaN` to be equal to | |
| 72 /// itself. | |
| 73 bool _numEquals(num n1, num n2) { | |
| 74 if (n1.isNaN && n2.isNaN) return true; | |
| 75 return n1 == n2; | |
| 76 } | |
| OLD | NEW |