Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: lib/yaml/yaml_map.dart

Issue 10153004: Add a basic YAML processor. Much of the language is still unimplemented. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * This class wraps behaves almost identically to the normal Dart Map
Bob Nystrom 2012/04/20 20:28:55 "wraps behaves"?
nweiz 2012/04/23 23:06:33 Fixed.
7 * implementation, with the following differences:
8 *
9 * * It allows null, NaN, boolean, list, and map keys.
10 * * It is itself Hashable.
11 * * It defines `==` structurally. That is, `yamlMap1 == yamlMap2` if they have
12 * the same contents.
13 */
14 class YamlMap implements Map, Hashable {
15 Map _map;
16
17 YamlMap() : _map = new Map();
18
19 YamlMap.from(Map map) : _map = new Map.from(map);
20
21 bool containsValue(value) => _map.containsValue(value);
22 bool containsKey(key) => _map.containsKey(_wrapKey(key));
23 operator [](key) => _map[_wrapKey(key)];
24 operator []=(key, value) { _map[_wrapKey(key)] = value; }
25 putIfAbsent(key, ifAbsent()) => _map.putIfAbsent(_wrapKey(key), ifAbsent);
26 remove(key) => _map.remove(_wrapKey(key));
27 void clear() => _map.clear();
28 void forEach(void f(key, value)) =>
29 _map.forEach((k, v) => f(_unwrapKey(k), v));
30 Collection getKeys() => _map.getKeys().map(_unwrapKey);
31 Collection getValues() => _map.getValues();
32 int get length() => _map.length;
33 bool isEmpty() => _map.isEmpty();
34 String toString() => _map.toString();
35
36 int hashCode() => _hashCode(_map);
37
38 bool operator ==(other) {
39 if (other is! YamlMap) return false;
40 return _mapEquals(this, other);
41 }
42
43 /** Wraps an object for use as a key in the map. */
44 _wrapKey(obj) {
45 if (obj != null && obj is! bool && obj is! List &&
46 (obj is! double || !obj.isNan()) &&
47 (obj is! Map || obj is YamlMap)) {
48 return obj;
49 }
50 return new _WrappedHashKey._(obj);
51 }
52
53 /** Unwraps an object that was used as a key in the map. */
54 _unwrapKey(obj) => obj is _WrappedHashKey ? obj.value : obj;
55 }
56
57 /**
58 * A class for wrapping normally-unhashable objects that are being used as keys
59 * in a YamlMap.
60 */
61 class _WrappedHashKey implements Hashable {
62 var value;
63
64 _WrappedHashKey._(this.value);
65
66 int hashCode() => _hashCode(value);
67
68 String toString() => value.toString();
69
70 /** This is defined as both values being structurally equal. */
71 bool operator ==(other) {
72 if (other is! _WrappedHashKey) return false;
73 _WrappedHashKey otherWrappedKey = other;
74 return _deepEquals(this.value, other.value);
75 }
76 }
77
78 /**
79 * Returns the hash code for [obj]. This includes null, true, false, maps, and
80 * lists.
81 */
82 int _hashCode(obj) {
Bob Nystrom 2012/04/20 20:28:55 This will do Bad Things with cyclic structures. Do
nweiz 2012/04/23 23:06:33 I've fixed hashCode and deepEquals to work properl
83 if (obj == null) return 0;
84 if (obj == true) return 1;
85 if (obj == false) return 2;
86 if (obj is Map) return _hashCode(obj.getKeys()) ^ _hashCode(obj.getValues());
87 if (obj is List) {
88 // This is probably a really bad hash function, but presumably we'll get thi s
89 // in the standard library before it actually matters.
90 int hash = 0;
91 for (var e in obj) {
92 hash ^= _hashCode(e);
93 }
94 return hash;
95 }
96 return obj.hashCode();
97 }
98
99 /** Returns whether [list1] and [list2] are structurally equal. */
100 bool _listEquals(List list1, List list2) {
101 if (list1.length != list2.length) return false;
102
103 for (var i = 0; i < list1.length; i++) {
104 if (!_deepEquals(list1[i], list2[i])) return false;
105 }
106
107 return true;
108 }
109
110 /** Returns whether [map1] and [map2] are structurally equal. */
111 bool _mapEquals(Map map1, Map map2) {
112 if (map1.length != map2.length) return false;
113
114 for (var key in map1.getKeys()) {
115 if (!map2.containsKey(key)) return false;
116 if (!_deepEquals(map1[key], map2[key])) return false;
117 }
118
119 return true;
120 }
121
122 /**
123 * Returns whether two doubles are equivalent. This differs from `d1 == d2` in
124 * that it considers NaN to be equal to itself.
125 */
126 bool _doubleEquals(double d1, double d2) {
127 if (d1.isNaN() && d2.isNaN()) return true;
128 return d1 == d2;
129 }
130
131 /** Returns whether two objects are structurally equivalent. */
132 bool _deepEquals(obj1, obj2) {
133 if (obj1 is List && obj2 is List) return _listEquals(obj1, obj2);
134 if (obj1 is Map && obj2 is Map) return _mapEquals(obj1, obj2);
135 if (obj1 is double && obj2 is double) return _doubleEquals(obj1, obj2);
136 return obj1 == obj2;
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698