| 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 /// A pair of values. |
| 8 class Pair<E, F> { |
| 9 final E first; |
| 10 final F last; |
| 8 | 11 |
| 9 /// Returns a hash code for [obj] such that structurally equivalent objects | 12 Pair(this.first, this.last); |
| 10 /// will have the same hash code. | |
| 11 /// | |
| 12 /// This supports deep equality for maps and lists, including those with | |
| 13 /// self-referential structures. | |
| 14 int hashCodeFor(obj) { | |
| 15 var parents = []; | |
| 16 | 13 |
| 17 _hashCodeFor(value) { | 14 String toString() => '($first, $last)'; |
| 18 if (parents.any((parent) => identical(parent, value))) return -1; | |
| 19 | |
| 20 parents.add(value); | |
| 21 try { | |
| 22 if (value is Map) { | |
| 23 var equality = const UnorderedIterableEquality(); | |
| 24 return equality.hash(value.keys.map(_hashCodeFor)) ^ | |
| 25 equality.hash(value.values.map(_hashCodeFor)); | |
| 26 } else if (value is Iterable) { | |
| 27 return const IterableEquality().hash(value.map(hashCodeFor)); | |
| 28 } | |
| 29 return value.hashCode; | |
| 30 } finally { | |
| 31 parents.removeLast(); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 return _hashCodeFor(obj); | |
| 36 } | 15 } |
| OLD | NEW |