Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(723)

Side by Side Diff: pkg/yaml/lib/src/utils.dart

Issue 297753003: Ensure that YAML maps' map keys are order-independent. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/yaml/CHANGELOG.md ('k') | pkg/yaml/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 SetEquality();
Jennifer Messerly 2014/05/20 22:51:25 maybe use UnorderedIterableEquality and avoid the
nweiz 2014/05/20 22:58:54 Oh, I missed that, good idea.
24 return equality.hash(value.keys.map(_hashCodeFor).toSet()) ^
25 equality.hash(value.values.map(_hashCodeFor).toSet());
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 }
OLDNEW
« no previous file with comments | « pkg/yaml/CHANGELOG.md ('k') | pkg/yaml/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698