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 /// This class wraps behaves almost identically to the normal Dart Map | 7 /// This class wraps behaves almost identically to the normal Dart Map |
8 /// implementation, with the following differences: | 8 /// implementation, with the following differences: |
9 /// | 9 /// |
10 /// * It allows null, NaN, boolean, list, and map keys. | 10 /// * It allows null, NaN, boolean, list, and map keys. |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 bool containsValue(value) => _map.containsValue(value); | 23 bool containsValue(value) => _map.containsValue(value); |
24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); | 24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); |
25 operator [](key) => _map[_wrapKey(key)]; | 25 operator [](key) => _map[_wrapKey(key)]; |
26 operator []=(key, value) { _map[_wrapKey(key)] = value; } | 26 operator []=(key, value) { _map[_wrapKey(key)] = value; } |
27 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); | 27 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); |
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 get keys => _map.keys.map(_unwrapKey); | 32 Iterable get keys => _map.keys.mappedBy(_unwrapKey); |
33 Collection get values => _map.values; | 33 Iterable get values => _map.values; |
34 int get length => _map.length; | 34 int get length => _map.length; |
35 bool get isEmpty => _map.isEmpty; | 35 bool get isEmpty => _map.isEmpty; |
36 String toString() => _map.toString(); | 36 String toString() => _map.toString(); |
37 | 37 |
38 int get 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 } |
(...skipping 30 matching lines...) Expand all Loading... |
74 if (other is! _WrappedHashKey) return false; | 74 if (other is! _WrappedHashKey) return false; |
75 return deepEquals(this.value, other.value); | 75 return deepEquals(this.value, other.value); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 /// Returns the hash code for [obj]. This includes null, true, false, maps, and | 79 /// Returns the hash code for [obj]. This includes null, true, false, maps, and |
80 /// lists. Also handles self-referential structures. | 80 /// lists. Also handles self-referential structures. |
81 int _hashCode(obj, [List parents]) { | 81 int _hashCode(obj, [List parents]) { |
82 if (parents == null) { | 82 if (parents == null) { |
83 parents = []; | 83 parents = []; |
84 } else if (parents.some((p) => identical(p, obj))) { | 84 } else if (parents.any((p) => identical(p, obj))) { |
85 return -1; | 85 return -1; |
86 } | 86 } |
87 | 87 |
88 parents.add(obj); | 88 parents.add(obj); |
89 try { | 89 try { |
90 if (obj == null) return 0; | 90 if (obj == null) return 0; |
91 if (obj == true) return 1; | 91 if (obj == true) return 1; |
92 if (obj == false) return 2; | 92 if (obj == false) return 2; |
93 if (obj is Map) { | 93 if (obj is Map) { |
94 return _hashCode(obj.keys, parents) ^ | 94 return _hashCode(obj.keys, parents) ^ |
95 _hashCode(obj.values, parents); | 95 _hashCode(obj.values, parents); |
96 } | 96 } |
97 if (obj is List) { | 97 if (obj is Iterable) { |
98 // This is probably a really bad hash function, but presumably we'll get | 98 // This is probably a really bad hash function, but presumably we'll get |
99 // this in the standard library before it actually matters. | 99 // this in the standard library before it actually matters. |
100 int hash = 0; | 100 int hash = 0; |
101 for (var e in obj) { | 101 for (var e in obj) { |
102 hash ^= _hashCode(e, parents); | 102 hash ^= _hashCode(e, parents); |
103 } | 103 } |
104 return hash; | 104 return hash; |
105 } | 105 } |
106 return obj.hashCode; | 106 return obj.hashCode; |
107 } finally { | 107 } finally { |
108 parents.removeLast(); | 108 parents.removeLast(); |
109 } | 109 } |
110 } | 110 } |
OLD | NEW |