OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library utils; |
| 6 |
| 7 /// Returns the hash code for [obj]. This includes null, true, false, maps, and |
| 8 /// lists. Also handles self-referential structures. |
| 9 int hashCodeFor(obj, [List parents]) { |
| 10 if (parents == null) { |
| 11 parents = []; |
| 12 } else if (parents.any((p) => identical(p, obj))) { |
| 13 return -1; |
| 14 } |
| 15 |
| 16 parents.add(obj); |
| 17 try { |
| 18 if (obj == null) return 0; |
| 19 if (obj == true) return 1; |
| 20 if (obj == false) return 2; |
| 21 if (obj is Map) { |
| 22 return hashCodeFor(obj.keys, parents) ^ |
| 23 hashCodeFor(obj.values, parents); |
| 24 } |
| 25 if (obj is Iterable) { |
| 26 // This is probably a really bad hash function, but presumably we'll get |
| 27 // this in the standard library before it actually matters. |
| 28 int hash = 0; |
| 29 for (var e in obj) { |
| 30 hash ^= hashCodeFor(e, parents); |
| 31 } |
| 32 return hash; |
| 33 } |
| 34 return obj.hashCode; |
| 35 } finally { |
| 36 parents.removeLast(); |
| 37 } |
| 38 } |
| 39 |
OLD | NEW |