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 library yaml_map; |
| 6 |
| 7 import 'deep_equals.dart'; |
| 8 import 'utils.dart'; |
6 | 9 |
7 /// This class wraps behaves almost identically to the normal Dart Map | 10 /// This class wraps behaves almost identically to the normal Dart Map |
8 /// implementation, with the following differences: | 11 /// implementation, with the following differences: |
9 /// | 12 /// |
10 /// * It allows null, NaN, boolean, list, and map keys. | 13 /// * It allows null, NaN, boolean, list, and map keys. |
11 /// * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they | 14 /// * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they |
12 /// have the same contents. | 15 /// have the same contents. |
13 /// * It has a compatible [hashCode] method. | 16 /// * It has a compatible [hashCode] method. |
14 class YamlMap implements Map { | 17 class YamlMap implements Map { |
15 Map _map; | 18 final Map _map; |
16 | 19 |
17 YamlMap() : _map = new Map(); | 20 YamlMap() : _map = new Map(); |
18 | 21 |
19 YamlMap.from(Map map) : _map = new Map.from(map); | 22 YamlMap.from(Map map) : _map = new Map.from(map); |
20 | 23 |
21 YamlMap._wrap(this._map); | 24 YamlMap._wrap(this._map); |
22 | 25 |
23 bool containsValue(value) => _map.containsValue(value); | 26 bool containsValue(value) => _map.containsValue(value); |
24 bool containsKey(key) => _map.containsKey(_wrapKey(key)); | 27 bool containsKey(key) => _map.containsKey(_wrapKey(key)); |
25 operator [](key) => _map[_wrapKey(key)]; | 28 operator [](key) => _map[_wrapKey(key)]; |
26 operator []=(key, value) { _map[_wrapKey(key)] = value; } | 29 operator []=(key, value) { _map[_wrapKey(key)] = value; } |
27 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); | 30 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent); |
28 remove(key) => _map.remove(_wrapKey(key)); | 31 remove(key) => _map.remove(_wrapKey(key)); |
29 void clear() => _map.clear(); | 32 void clear() => _map.clear(); |
30 void forEach(void f(key, value)) => | 33 void forEach(void f(key, value)) => |
31 _map.forEach((k, v) => f(_unwrapKey(k), v)); | 34 _map.forEach((k, v) => f(_unwrapKey(k), v)); |
32 Iterable get keys => _map.keys.map(_unwrapKey); | 35 Iterable get keys => _map.keys.map(_unwrapKey); |
33 Iterable get values => _map.values; | 36 Iterable get values => _map.values; |
34 int get length => _map.length; | 37 int get length => _map.length; |
35 bool get isEmpty => _map.isEmpty; | 38 bool get isEmpty => _map.isEmpty; |
36 String toString() => _map.toString(); | 39 String toString() => _map.toString(); |
37 | 40 |
38 int get hashCode => _hashCode(_map); | 41 int get hashCode => hashCodeFor(_map); |
39 | 42 |
40 bool operator ==(other) { | 43 bool operator ==(other) { |
41 if (other is! YamlMap) return false; | 44 if (other is! YamlMap) return false; |
42 return deepEquals(this, other); | 45 return deepEquals(this, other); |
43 } | 46 } |
44 | 47 |
45 /// Wraps an object for use as a key in the map. | 48 /// Wraps an object for use as a key in the map. |
46 _wrapKey(obj) { | 49 _wrapKey(obj) { |
47 if (obj != null && obj is! bool && obj is! List && | 50 if (obj != null && obj is! bool && obj is! List && |
48 (obj is! double || !obj.isNan()) && | 51 (obj is! double || !obj.isNan()) && |
49 (obj is! Map || obj is YamlMap)) { | 52 (obj is! Map || obj is YamlMap)) { |
50 return obj; | 53 return obj; |
51 } else if (obj is Map) { | 54 } else if (obj is Map) { |
52 return new YamlMap._wrap(obj); | 55 return new YamlMap._wrap(obj); |
53 } | 56 } |
54 return new _WrappedHashKey(obj); | 57 return new _WrappedHashKey(obj); |
55 } | 58 } |
56 | 59 |
57 /// Unwraps an object that was used as a key in the map. | 60 /// Unwraps an object that was used as a key in the map. |
58 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; | 61 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj; |
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. |
63 class _WrappedHashKey { | 66 class _WrappedHashKey { |
64 var value; | 67 final value; |
65 | 68 |
66 _WrappedHashKey(this.value); | 69 _WrappedHashKey(this.value); |
67 | 70 |
68 int get hashCode => _hashCode(value); | 71 int get hashCode => hashCodeFor(value); |
69 | 72 |
70 String toString() => value.toString(); | 73 String toString() => value.toString(); |
71 | 74 |
72 /// This is defined as both values being structurally equal. | 75 /// This is defined as both values being structurally equal. |
73 bool operator ==(other) { | 76 bool operator ==(other) { |
74 if (other is! _WrappedHashKey) return false; | 77 if (other is! _WrappedHashKey) return false; |
75 return deepEquals(this.value, other.value); | 78 return deepEquals(this.value, other.value); |
76 } | 79 } |
77 } | 80 } |
78 | |
79 /// Returns the hash code for [obj]. This includes null, true, false, maps, and | |
80 /// lists. Also handles self-referential structures. | |
81 int _hashCode(obj, [List parents]) { | |
82 if (parents == null) { | |
83 parents = []; | |
84 } else if (parents.any((p) => identical(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.keys, parents) ^ | |
95 _hashCode(obj.values, parents); | |
96 } | |
97 if (obj is Iterable) { | |
98 // This is probably a really bad hash function, but presumably we'll get | |
99 // this 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 |