| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, 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 /** |
| 6 * This class wraps behaves almost identically to the normal Dart Map |
| 7 * implementation, with the following differences: |
| 8 * |
| 9 * * It allows null, NaN, boolean, list, and map keys. |
| 10 * * It is itself Hashable. |
| 11 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have |
| 12 * the same contents. |
| 13 */ |
| 14 class YamlMap implements Map, Hashable { |
| 15 Map _map; |
| 16 |
| 17 YamlMap() : _map = new Map(); |
| 18 |
| 19 YamlMap.from(Map map) : _map = new Map.from(map); |
| 20 |
| 21 bool containsValue(value) => _map.containsValue(value); |
| 22 bool containsKey(key) => _map.containsKey(_wrapKey(key)); |
| 23 operator [](key) => _map[_wrapKey(key)]; |
| 24 operator []=(key, value) { _map[_wrapKey(key)] = value; } |
| 25 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); |
| 26 remove(key) => _map.remove(_wrapKey(key)); |
| 27 void clear() => _map.clear(); |
| 28 void forEach(void f(key, value)) => |
| 29 _map.forEach((k, v) => f(_unwrapKey(k), v)); |
| 30 Collection getKeys() => _map.getKeys().map(_unwrapKey); |
| 31 Collection getValues() => _map.getValues(); |
| 32 int get length() => _map.length; |
| 33 bool isEmpty() => _map.isEmpty(); |
| 34 String toString() => _map.toString(); |
| 35 |
| 36 int hashCode() => _hashCode(_map); |
| 37 |
| 38 bool operator ==(other) { |
| 39 if (other is! YamlMap) return false; |
| 40 return deepEquals(this, other); |
| 41 } |
| 42 |
| 43 /** Wraps an object for use as a key in the map. */ |
| 44 _wrapKey(obj) { |
| 45 if (obj != null && obj is! bool && obj is! List && |
| 46 (obj is! double || !obj.isNan()) && |
| 47 (obj is! Map || obj is YamlMap)) { |
| 48 return obj; |
| 49 } |
| 50 return new _WrappedHashKey(obj); |
| 51 } |
| 52 |
| 53 /** Unwraps an object that was used as a key in the map. */ |
| 54 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; |
| 55 } |
| 56 |
| 57 /** |
| 58 * A class for wrapping normally-unhashable objects that are being used as keys |
| 59 * in a YamlMap. |
| 60 */ |
| 61 class _WrappedHashKey implements Hashable { |
| 62 var value; |
| 63 |
| 64 _WrappedHashKey(this.value); |
| 65 |
| 66 int hashCode() => _hashCode(value); |
| 67 |
| 68 String toString() => value.toString(); |
| 69 |
| 70 /** This is defined as both values being structurally equal. */ |
| 71 bool operator ==(other) { |
| 72 if (other is! _WrappedHashKey) return false; |
| 73 return deepEquals(this.value, other.value); |
| 74 } |
| 75 } |
| 76 |
| 77 /** |
| 78 * Returns the hash code for [obj]. This includes null, true, false, maps, and |
| 79 * lists. Also handles self-referential structures. |
| 80 */ |
| 81 int _hashCode(obj, [List parents]) { |
| 82 if (parents == null) { |
| 83 parents = []; |
| 84 } else if (parents.some((p) => p === obj)) { |
| 85 return -1; |
| 86 } |
| 87 |
| 88 parents.add(obj); |
| 89 try { |
| 90 if (obj == null) return 0; |
| 91 if (obj == true) return 1; |
| 92 if (obj == false) return 2; |
| 93 if (obj is Map) { |
| 94 return _hashCode(obj.getKeys(), parents) ^ |
| 95 _hashCode(obj.getValues(), parents); |
| 96 } |
| 97 if (obj is List) { |
| 98 // This is probably a really bad hash function, but presumably we'll get t
his |
| 99 // in the standard library before it actually matters. |
| 100 int hash = 0; |
| 101 for (var e in obj) { |
| 102 hash ^= _hashCode(e, parents); |
| 103 } |
| 104 return hash; |
| 105 } |
| 106 return obj.hashCode(); |
| 107 } finally { |
| 108 parents.removeLast(); |
| 109 } |
| 110 } |
| OLD | NEW |