| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library yaml.deep_equals; | |
| 6 | |
| 7 /// Returns whether two objects are structurally equivalent. | |
| 8 /// | |
| 9 /// This considers `NaN` values to be equivalent. It also handles | |
| 10 /// self-referential structures. | |
| 11 bool deepEquals(obj1, obj2) => new _DeepEquals().equals(obj1, obj2); | |
| 12 | |
| 13 /// A class that provides access to the list of parent objects used for loop | |
| 14 /// detection. | |
| 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 } | |
| 48 } | |
| 49 | |
| 50 /// Returns whether [list1] and [list2] are structurally equal. | |
| 51 bool _listEquals(List list1, List list2) { | |
| 52 if (list1.length != list2.length) return false; | |
| 53 | |
| 54 for (var i = 0; i < list1.length; i++) { | |
| 55 if (!equals(list1[i], list2[i])) return false; | |
| 56 } | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 /// Returns whether [map1] and [map2] are structurally equal. | |
| 62 bool _mapEquals(Map map1, Map map2) { | |
| 63 if (map1.length != map2.length) return false; | |
| 64 | |
| 65 for (var key in map1.keys) { | |
| 66 if (!map2.containsKey(key)) return false; | |
| 67 if (!equals(map1[key], map2[key])) return false; | |
| 68 } | |
| 69 | |
| 70 return true; | |
| 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; | |
| 80 } | |
| 81 } | |
| OLD | NEW |