Chromium Code Reviews| Index: pkg/yaml/lib/src/deep_equals.dart |
| diff --git a/pkg/yaml/lib/src/deep_equals.dart b/pkg/yaml/lib/src/deep_equals.dart |
| index bce4aadf2e9c29b222c662d07d653c2674fa7c0e..6f6373799f67022fc9603f26048f52215071bb7c 100644 |
| --- a/pkg/yaml/lib/src/deep_equals.dart |
| +++ b/pkg/yaml/lib/src/deep_equals.dart |
| @@ -2,71 +2,79 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| -library deep_equals; |
| +library yaml.deep_equals; |
| -/// Returns whether two objects are structurally equivalent. This considers NaN |
| -/// values to be equivalent. It also handles self-referential structures. |
| -bool deepEquals(obj1, obj2, [List parents1, List parents2]) { |
| - if (identical(obj1, obj2)) return true; |
| - if (parents1 == null) { |
| - parents1 = []; |
| - parents2 = []; |
| - } |
| +/// Returns whether two objects are structurally equivalent. |
| +/// |
| +/// This considers `NaN` values to be equivalent. It also handles |
| +/// self-referential structures. |
| +bool deepEquals(obj1, obj2) => new _DeepEquals().equals(obj1, obj2); |
| - // parents1 and parents2 are guaranteed to be the same size. |
| - for (var i = 0; i < parents1.length; i++) { |
| - var loop1 = identical(obj1, parents1[i]); |
| - var loop2 = identical(obj2, parents2[i]); |
| - // If both structures loop in the same place, they're equal at that point in |
| - // the structure. If one loops and the other doesn't, they're not equal. |
| - if (loop1 && loop2) return true; |
| - if (loop1 || loop2) return false; |
| - } |
| +/// A class that provides access to the list of parent objects used for loop |
| +/// detection. |
| +class _DeepEquals { |
| + final _parents1 = []; |
| + final _parents2 = []; |
| + |
| + /// Returns whether [obj1] and [obj2] are structurally equivalent. |
| + bool equals(obj1, obj2) { |
| + // _parents1 and _parents2 are guaranteed to be the same size. |
| + for (var i = 0; i < _parents1.length; i++) { |
| + var loop1 = identical(obj1, _parents1[i]); |
| + var loop2 = identical(obj2, _parents2[i]); |
| + // If both structures loop in the same place, they're equal at that point in |
|
Bob Nystrom
2014/05/09 00:18:56
Long line.
nweiz
2014/05/20 00:15:07
Done.
|
| + // the structure. If one loops and the other doesn't, they're not equal. |
| + if (loop1 && loop2) return true; |
| + if (loop1 || loop2) return false; |
| + } |
| - parents1.add(obj1); |
| - parents2.add(obj2); |
| - try { |
| - if (obj1 is List && obj2 is List) { |
| - return _listEquals(obj1, obj2, parents1, parents2); |
| - } else if (obj1 is Map && obj2 is Map) { |
| - return _mapEquals(obj1, obj2, parents1, parents2); |
| - } else if (obj1 is double && obj2 is double) { |
| - return _doubleEquals(obj1, obj2); |
| - } else { |
| - return obj1 == obj2; |
| + _parents1.add(obj1); |
| + _parents2.add(obj2); |
| + try { |
| + if (obj1 is List && obj2 is List) { |
| + return _listEquals(obj1, obj2); |
| + } else if (obj1 is Map && obj2 is Map) { |
| + return _mapEquals(obj1, obj2); |
| + } else if (obj1 is num && obj2 is num) { |
| + return _numEquals(obj1, obj2); |
| + } else { |
| + return obj1 == obj2; |
| + } |
| + } finally { |
| + _parents1.removeLast(); |
| + _parents2.removeLast(); |
| } |
| - } finally { |
| - parents1.removeLast(); |
| - parents2.removeLast(); |
| } |
| -} |
| -/// Returns whether [list1] and [list2] are structurally equal. |
| -bool _listEquals(List list1, List list2, List parents1, List parents2) { |
| - if (list1.length != list2.length) return false; |
| + /// Returns whether [list1] and [list2] are structurally equal. |
| + bool _listEquals(List list1, List list2) { |
| + if (list1.length != list2.length) return false; |
| + |
| + for (var i = 0; i < list1.length; i++) { |
| + if (!equals(list1[i], list2[i])) return false; |
| + } |
| - for (var i = 0; i < list1.length; i++) { |
| - if (!deepEquals(list1[i], list2[i], parents1, parents2)) return false; |
| + return true; |
| } |
| - return true; |
| -} |
| + /// Returns whether [map1] and [map2] are structurally equal. |
| + bool _mapEquals(Map map1, Map map2) { |
| + if (map1.length != map2.length) return false; |
| -/// Returns whether [map1] and [map2] are structurally equal. |
| -bool _mapEquals(Map map1, Map map2, List parents1, List parents2) { |
| - if (map1.length != map2.length) return false; |
| + for (var key in map1.keys) { |
| + if (!map2.containsKey(key)) return false; |
|
Bob Nystrom
2014/05/09 00:18:56
What about extra keys in map2?
nweiz
2014/05/20 00:15:07
We check that they're the same length above. If th
|
| + if (!equals(map1[key], map2[key])) return false; |
| + } |
| - for (var key in map1.keys) { |
| - if (!map2.containsKey(key)) return false; |
| - if (!deepEquals(map1[key], map2[key], parents1, parents2)) return false; |
| + return true; |
| } |
| - return true; |
| -} |
| - |
| -/// Returns whether two doubles are equivalent. This differs from `d1 == d2` in |
| -/// that it considers NaN to be equal to itself. |
| -bool _doubleEquals(double d1, double d2) { |
| - if (d1.isNaN && d2.isNaN) return true; |
| - return d1 == d2; |
| + /// Returns whether two numbers are equivalent. |
| + /// |
| + /// This differs from `n1 == n2` in that it considers `NaN` to be equal to |
| + /// itself. |
| + bool _numEquals(num n1, num n2) { |
| + if (n1.isNaN && n2.isNaN) return true; |
| + return n1 == n2; |
| + } |
| } |