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