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 /** |
6 * This class wraps behaves almost identically to the normal Dart Map | 6 * This class wraps behaves almost identically to the normal Dart Map |
7 * implementation, with the following differences: | 7 * implementation, with the following differences: |
8 * | 8 * |
9 * * It allows null, NaN, boolean, list, and map keys. | 9 * * It allows null, NaN, boolean, list, and map keys. |
10 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have | 10 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have |
(...skipping 17 matching lines...) Expand all Loading... |
28 remove(key) => _map.remove(_wrapKey(key)); | 28 remove(key) => _map.remove(_wrapKey(key)); |
29 void clear() => _map.clear(); | 29 void clear() => _map.clear(); |
30 void forEach(void f(key, value)) => | 30 void forEach(void f(key, value)) => |
31 _map.forEach((k, v) => f(_unwrapKey(k), v)); | 31 _map.forEach((k, v) => f(_unwrapKey(k), v)); |
32 Collection getKeys() => _map.getKeys().map(_unwrapKey); | 32 Collection getKeys() => _map.getKeys().map(_unwrapKey); |
33 Collection getValues() => _map.getValues(); | 33 Collection getValues() => _map.getValues(); |
34 int get length => _map.length; | 34 int get length => _map.length; |
35 bool isEmpty() => _map.isEmpty(); | 35 bool isEmpty() => _map.isEmpty(); |
36 String toString() => _map.toString(); | 36 String toString() => _map.toString(); |
37 | 37 |
38 int hashCode() => _hashCode(_map); | 38 int get hashCode => _hashCode(_map); |
39 | 39 |
40 bool operator ==(other) { | 40 bool operator ==(other) { |
41 if (other is! YamlMap) return false; | 41 if (other is! YamlMap) return false; |
42 return deepEquals(this, other); | 42 return deepEquals(this, other); |
43 } | 43 } |
44 | 44 |
45 /** Wraps an object for use as a key in the map. */ | 45 /** Wraps an object for use as a key in the map. */ |
46 _wrapKey(obj) { | 46 _wrapKey(obj) { |
47 if (obj != null && obj is! bool && obj is! List && | 47 if (obj != null && obj is! bool && obj is! List && |
48 (obj is! double || !obj.isNan()) && | 48 (obj is! double || !obj.isNan()) && |
(...skipping 11 matching lines...) Expand all Loading... |
60 | 60 |
61 /** | 61 /** |
62 * 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 |
63 * in a YamlMap. | 63 * in a YamlMap. |
64 */ | 64 */ |
65 class _WrappedHashKey { | 65 class _WrappedHashKey { |
66 var value; | 66 var value; |
67 | 67 |
68 _WrappedHashKey(this.value); | 68 _WrappedHashKey(this.value); |
69 | 69 |
70 int hashCode() => _hashCode(value); | 70 int get hashCode => _hashCode(value); |
71 | 71 |
72 String toString() => value.toString(); | 72 String toString() => value.toString(); |
73 | 73 |
74 /** This is defined as both values being structurally equal. */ | 74 /** This is defined as both values being structurally equal. */ |
75 bool operator ==(other) { | 75 bool operator ==(other) { |
76 if (other is! _WrappedHashKey) return false; | 76 if (other is! _WrappedHashKey) return false; |
77 return deepEquals(this.value, other.value); | 77 return deepEquals(this.value, other.value); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
(...skipping 19 matching lines...) Expand all Loading... |
100 } | 100 } |
101 if (obj is List) { | 101 if (obj is List) { |
102 // This is probably a really bad hash function, but presumably we'll get t
his | 102 // This is probably a really bad hash function, but presumably we'll get t
his |
103 // in the standard library before it actually matters. | 103 // in the standard library before it actually matters. |
104 int hash = 0; | 104 int hash = 0; |
105 for (var e in obj) { | 105 for (var e in obj) { |
106 hash ^= _hashCode(e, parents); | 106 hash ^= _hashCode(e, parents); |
107 } | 107 } |
108 return hash; | 108 return hash; |
109 } | 109 } |
110 return obj.hashCode(); | 110 return obj.hashCode; |
111 } finally { | 111 } finally { |
112 parents.removeLast(); | 112 parents.removeLast(); |
113 } | 113 } |
114 } | 114 } |
OLD | NEW |