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