OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 yaml.deep_equals; | 5 library yaml.equality; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:collection/collection.dart'; |
| 10 |
| 11 import 'yaml_node.dart'; |
| 12 |
| 13 /// Returns a [Map] that compares its keys based on [deepEquals]. |
| 14 Map deepEqualsMap() => new HashMap(equals: deepEquals, hashCode: deepHashCode); |
6 | 15 |
7 /// Returns whether two objects are structurally equivalent. | 16 /// Returns whether two objects are structurally equivalent. |
8 /// | 17 /// |
9 /// This considers `NaN` values to be equivalent. It also handles | 18 /// This considers `NaN` values to be equivalent, handles self-referential |
10 /// self-referential structures. | 19 /// structures, and considers [YamlScalar]s to be equal to their values. |
11 bool deepEquals(obj1, obj2) => new _DeepEquals().equals(obj1, obj2); | 20 bool deepEquals(obj1, obj2) => new _DeepEquals().equals(obj1, obj2); |
12 | 21 |
13 /// A class that provides access to the list of parent objects used for loop | 22 /// A class that provides access to the list of parent objects used for loop |
14 /// detection. | 23 /// detection. |
15 class _DeepEquals { | 24 class _DeepEquals { |
16 final _parents1 = []; | 25 final _parents1 = []; |
17 final _parents2 = []; | 26 final _parents2 = []; |
18 | 27 |
19 /// Returns whether [obj1] and [obj2] are structurally equivalent. | 28 /// Returns whether [obj1] and [obj2] are structurally equivalent. |
20 bool equals(obj1, obj2) { | 29 bool equals(obj1, obj2) { |
| 30 if (obj1 is YamlScalar) obj1 = obj1.value; |
| 31 if (obj2 is YamlScalar) obj2 = obj2.value; |
| 32 |
21 // _parents1 and _parents2 are guaranteed to be the same size. | 33 // _parents1 and _parents2 are guaranteed to be the same size. |
22 for (var i = 0; i < _parents1.length; i++) { | 34 for (var i = 0; i < _parents1.length; i++) { |
23 var loop1 = identical(obj1, _parents1[i]); | 35 var loop1 = identical(obj1, _parents1[i]); |
24 var loop2 = identical(obj2, _parents2[i]); | 36 var loop2 = identical(obj2, _parents2[i]); |
25 // If both structures loop in the same place, they're equal at that point | 37 // 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 | 38 // in the structure. If one loops and the other doesn't, they're not |
27 // equal. | 39 // equal. |
28 if (loop1 && loop2) return true; | 40 if (loop1 && loop2) return true; |
29 if (loop1 || loop2) return false; | 41 if (loop1 || loop2) return false; |
30 } | 42 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 84 |
73 /// Returns whether two numbers are equivalent. | 85 /// Returns whether two numbers are equivalent. |
74 /// | 86 /// |
75 /// This differs from `n1 == n2` in that it considers `NaN` to be equal to | 87 /// This differs from `n1 == n2` in that it considers `NaN` to be equal to |
76 /// itself. | 88 /// itself. |
77 bool _numEquals(num n1, num n2) { | 89 bool _numEquals(num n1, num n2) { |
78 if (n1.isNaN && n2.isNaN) return true; | 90 if (n1.isNaN && n2.isNaN) return true; |
79 return n1 == n2; | 91 return n1 == n2; |
80 } | 92 } |
81 } | 93 } |
| 94 |
| 95 /// Returns a hash code for [obj] such that structurally equivalent objects |
| 96 /// will have the same hash code. |
| 97 /// |
| 98 /// This supports deep equality for maps and lists, including those with |
| 99 /// self-referential structures, and returns the same hash code for |
| 100 /// [YamlScalar]s and their values. |
| 101 int deepHashCode(obj) { |
| 102 var parents = []; |
| 103 |
| 104 _deepHashCode(value) { |
| 105 if (parents.any((parent) => identical(parent, value))) return -1; |
| 106 |
| 107 parents.add(value); |
| 108 try { |
| 109 if (value is Map) { |
| 110 var equality = const UnorderedIterableEquality(); |
| 111 return equality.hash(value.keys.map(_deepHashCode)) ^ |
| 112 equality.hash(value.values.map(_deepHashCode)); |
| 113 } else if (value is Iterable) { |
| 114 return const IterableEquality().hash(value.map(deepHashCode)); |
| 115 } else if (value is YamlScalar) { |
| 116 return value.value.hashCode; |
| 117 } else { |
| 118 return value.hashCode; |
| 119 } |
| 120 } finally { |
| 121 parents.removeLast(); |
| 122 } |
| 123 } |
| 124 |
| 125 return _deepHashCode(obj); |
| 126 } |
OLD | NEW |