OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.utils; | 5 library yaml.utils; |
6 | 6 |
7 import 'package:collection/collection.dart'; | 7 import 'package:collection/collection.dart'; |
8 | 8 |
9 /// Returns a hash code for [obj] such that structurally equivalent objects | 9 /// Returns a hash code for [obj] such that structurally equivalent objects |
10 /// will have the same hash code. | 10 /// will have the same hash code. |
11 /// | 11 /// |
12 /// This supports deep equality for maps and lists, including those with | 12 /// This supports deep equality for maps and lists, including those with |
13 /// self-referential structures. | 13 /// self-referential structures. |
14 int hashCodeFor(obj) { | 14 int hashCodeFor(obj) { |
15 var parents = []; | 15 var parents = []; |
16 | 16 |
17 _hashCodeFor(value) { | 17 _hashCodeFor(value) { |
18 if (parents.any((parent) => identical(parent, value))) return -1; | 18 if (parents.any((parent) => identical(parent, value))) return -1; |
19 | 19 |
20 parents.add(value); | 20 parents.add(value); |
21 try { | 21 try { |
22 if (value is Map) { | 22 if (value is Map) { |
23 return _hashCodeFor(value.keys) ^ _hashCodeFor(value.values); | 23 var equality = const UnorderedIterableEquality(); |
| 24 return equality.hash(value.keys.map(_hashCodeFor)) ^ |
| 25 equality.hash(value.values.map(_hashCodeFor)); |
24 } else if (value is Iterable) { | 26 } else if (value is Iterable) { |
25 return const IterableEquality().hash(value.map(hashCodeFor)); | 27 return const IterableEquality().hash(value.map(hashCodeFor)); |
26 } | 28 } |
27 return value.hashCode; | 29 return value.hashCode; |
28 } finally { | 30 } finally { |
29 parents.removeLast(); | 31 parents.removeLast(); |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
33 return _hashCodeFor(obj); | 35 return _hashCodeFor(obj); |
34 } | 36 } |
OLD | NEW |