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 deep_equals; |
6 | 6 |
7 /** | 7 /** |
8 * Returns whether two objects are structurally equivalent. This considers NaN | 8 * Returns whether two objects are structurally equivalent. This considers NaN |
9 * values to be equivalent. It also handles self-referential structures. | 9 * values to be equivalent. It also handles self-referential structures. |
10 */ | 10 */ |
11 bool deepEquals(obj1, obj2, [List parents1, List parents2]) { | 11 bool deepEquals(obj1, obj2, [List parents1, List parents2]) { |
12 if (obj1 === obj2) return true; | 12 if (identical(obj1, obj2)) return true; |
13 if (parents1 == null) { | 13 if (parents1 == null) { |
14 parents1 = []; | 14 parents1 = []; |
15 parents2 = []; | 15 parents2 = []; |
16 } | 16 } |
17 | 17 |
18 // parents1 and parents2 are guaranteed to be the same size. | 18 // parents1 and parents2 are guaranteed to be the same size. |
19 for (var i = 0; i < parents1.length; i++) { | 19 for (var i = 0; i < parents1.length; i++) { |
20 var loop1 = obj1 === parents1[i]; | 20 var loop1 = identical(obj1, parents1[i]); |
21 var loop2 = obj2 === parents2[i]; | 21 var loop2 = identical(obj2, parents2[i]); |
22 // If both structures loop in the same place, they're equal at that point in | 22 // If both structures loop in the same place, they're equal at that point in |
23 // the structure. If one loops and the other doesn't, they're not equal. | 23 // the structure. If one loops and the other doesn't, they're not equal. |
24 if (loop1 && loop2) return true; | 24 if (loop1 && loop2) return true; |
25 if (loop1 || loop2) return false; | 25 if (loop1 || loop2) return false; |
26 } | 26 } |
27 | 27 |
28 parents1.add(obj1); | 28 parents1.add(obj1); |
29 parents2.add(obj2); | 29 parents2.add(obj2); |
30 try { | 30 try { |
31 if (obj1 is List && obj2 is List) { | 31 if (obj1 is List && obj2 is List) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 67 } |
68 | 68 |
69 /** | 69 /** |
70 * Returns whether two doubles are equivalent. This differs from `d1 == d2` in | 70 * Returns whether two doubles are equivalent. This differs from `d1 == d2` in |
71 * that it considers NaN to be equal to itself. | 71 * that it considers NaN to be equal to itself. |
72 */ | 72 */ |
73 bool _doubleEquals(double d1, double d2) { | 73 bool _doubleEquals(double d1, double d2) { |
74 if (d1.isNaN && d2.isNaN) return true; | 74 if (d1.isNaN && d2.isNaN) return true; |
75 return d1 == d2; | 75 return d1 == d2; |
76 } | 76 } |
OLD | NEW |