OLD | NEW |
| (Empty) |
1 part of dart.collection; | |
2 abstract class MapBase<K, V> = Object with MapMixin<K, V>; | |
3 abstract class MapMixin<K, V> implements Map<K, V> {Iterable<K> get keys; | |
4 V operator [](Object key); | |
5 operator []=(K key, V value); | |
6 V remove(Object key); | |
7 void clear(); | |
8 void forEach(void action(K key, V value)) { | |
9 for (K key in keys) { | |
10 action(key, this[key]); | |
11 } | |
12 } | |
13 void addAll(Map<K, V> other) { | |
14 for (K key in other.keys) { | |
15 this[key] = other[key]; | |
16 } | |
17 } | |
18 bool containsValue(Object value) { | |
19 for (K key in keys) { | |
20 if (this[key] == value) return true; | |
21 } | |
22 return false; | |
23 } | |
24 V putIfAbsent(K key, V ifAbsent()) { | |
25 if (keys.contains(key)) { | |
26 return this[key]; | |
27 } | |
28 return this[key] = ifAbsent(); | |
29 } | |
30 bool containsKey(Object key) => keys.contains(key); | |
31 int get length => keys.length; | |
32 bool get isEmpty => keys.isEmpty; | |
33 bool get isNotEmpty => keys.isNotEmpty; | |
34 Iterable<V> get values => new _MapBaseValueIterable<V>(this); | |
35 String toString() => Maps.mapToString(this); | |
36 } | |
37 abstract class UnmodifiableMapBase<K, V> = MapBase<K, V> with _UnmodifiableMapM
ixin<K, V>; | |
38 class _MapBaseValueIterable<V> extends IterableBase<V> implements EfficientLeng
th {final Map _map; | |
39 _MapBaseValueIterable(this._map); | |
40 int get length => _map.length; | |
41 bool get isEmpty => _map.isEmpty; | |
42 bool get isNotEmpty => _map.isNotEmpty; | |
43 V get first => ((__x7) => DEVC$RT.cast(__x7, dynamic, V, "CompositeCast", """li
ne 122, column 18 of dart:collection/maps.dart: """, __x7 is V, false))(_map[_ma
p.keys.first]); | |
44 V get single => ((__x8) => DEVC$RT.cast(__x8, dynamic, V, "CompositeCast", """l
ine 123, column 19 of dart:collection/maps.dart: """, __x8 is V, false))(_map[_m
ap.keys.single]); | |
45 V get last => ((__x9) => DEVC$RT.cast(__x9, dynamic, V, "CompositeCast", """lin
e 124, column 17 of dart:collection/maps.dart: """, __x9 is V, false))(_map[_map
.keys.last]); | |
46 Iterator<V> get iterator => new _MapBaseValueIterator<V>(_map); | |
47 } | |
48 class _MapBaseValueIterator<V> implements Iterator<V> {final Iterator _keys; | |
49 final Map _map; | |
50 V _current = null; | |
51 _MapBaseValueIterator(Map map) : _map = map, _keys = map.keys.iterator; | |
52 bool moveNext() { | |
53 if (_keys.moveNext()) { | |
54 _current = ((__x10) => DEVC$RT.cast(__x10, dynamic, V, "CompositeCast", """line
144, column 18 of dart:collection/maps.dart: """, __x10 is V, false))(_map[_keys
.current]); | |
55 return true; | |
56 } | |
57 _current = null; | |
58 return false; | |
59 } | |
60 V get current => _current; | |
61 } | |
62 abstract class _UnmodifiableMapMixin<K, V> implements Map<K, V> {void operator
[]=(K key, V value) { | |
63 throw new UnsupportedError("Cannot modify unmodifiable map"); | |
64 } | |
65 void addAll(Map<K, V> other) { | |
66 throw new UnsupportedError("Cannot modify unmodifiable map"); | |
67 } | |
68 void clear() { | |
69 throw new UnsupportedError("Cannot modify unmodifiable map"); | |
70 } | |
71 V remove(Object key) { | |
72 throw new UnsupportedError("Cannot modify unmodifiable map"); | |
73 } | |
74 V putIfAbsent(K key, V ifAbsent()) { | |
75 throw new UnsupportedError("Cannot modify unmodifiable map"); | |
76 } | |
77 } | |
78 class MapView<K, V> implements Map<K, V> {final Map<K, V> _map; | |
79 const MapView(Map<K, V> map) : _map = map; | |
80 V operator [](Object key) => _map[key]; | |
81 void operator []=(K key, V value) { | |
82 _map[key] = value; | |
83 } | |
84 void addAll(Map<K, V> other) { | |
85 _map.addAll(other); | |
86 } | |
87 void clear() { | |
88 _map.clear(); | |
89 } | |
90 V putIfAbsent(K key, V ifAbsent()) => _map.putIfAbsent(key, ifAbsent); | |
91 bool containsKey(Object key) => _map.containsKey(key); | |
92 bool containsValue(Object value) => _map.containsValue(value); | |
93 void forEach(void action(K key, V value)) { | |
94 _map.forEach(action); | |
95 } | |
96 bool get isEmpty => _map.isEmpty; | |
97 bool get isNotEmpty => _map.isNotEmpty; | |
98 int get length => _map.length; | |
99 Iterable<K> get keys => _map.keys; | |
100 V remove(Object key) => _map.remove(key); | |
101 String toString() => _map.toString(); | |
102 Iterable<V> get values => _map.values; | |
103 } | |
104 class UnmodifiableMapView<K, V> = MapView<K, V> with _UnmodifiableMapMixin<K, V
>; | |
105 class Maps {static bool containsValue(Map map, value) { | |
106 for (final v in map.values) { | |
107 if (value == v) { | |
108 return true; | |
109 } | |
110 } | |
111 return false; | |
112 } | |
113 static bool containsKey(Map map, key) { | |
114 for (final k in map.keys) { | |
115 if (key == k) { | |
116 return true; | |
117 } | |
118 } | |
119 return false; | |
120 } | |
121 static putIfAbsent(Map map, key, ifAbsent()) { | |
122 if (map.containsKey(key)) { | |
123 return map[key]; | |
124 } | |
125 final v = ifAbsent(); | |
126 map[key] = v; | |
127 return v; | |
128 } | |
129 static clear(Map map) { | |
130 for (final k in map.keys.toList()) { | |
131 map.remove(k); | |
132 } | |
133 } | |
134 static forEach(Map map, void f(key, value)) { | |
135 for (final k in map.keys) { | |
136 f(k, map[k]); | |
137 } | |
138 } | |
139 static Iterable getValues(Map map) { | |
140 return map.keys.map((key) => map[key]); | |
141 } | |
142 static int length(Map map) => map.keys.length; | |
143 static bool isEmpty(Map map) => map.keys.isEmpty; | |
144 static bool isNotEmpty(Map map) => map.keys.isNotEmpty; | |
145 static String mapToString(Map m) { | |
146 if (IterableBase._isToStringVisiting(m)) { | |
147 return '{...}'; | |
148 } | |
149 var result = new StringBuffer(); | |
150 try { | |
151 IterableBase._toStringVisiting.add(m); | |
152 result.write('{'); | |
153 bool first = true; | |
154 m.forEach((k, v) { | |
155 if (!first) { | |
156 result.write(', '); | |
157 } | |
158 first = false; | |
159 result.write(k); | |
160 result.write(': '); | |
161 result.write(v); | |
162 } | |
163 ); | |
164 result.write('}'); | |
165 } | |
166 finally { | |
167 assert (identical(IterableBase._toStringVisiting.last, m)); IterableBase._toStri
ngVisiting.removeLast(); | |
168 } | |
169 return result.toString(); | |
170 } | |
171 static _id(x) => x; | |
172 static void _fillMapWithMappedIterable(Map map, Iterable iterable, key(element)
, value(element)) { | |
173 if (key == null) key = _id; | |
174 if (value == null) value = _id; | |
175 for (var element in iterable) { | |
176 map[key(element)] = value(element); | |
177 } | |
178 } | |
179 static void _fillMapWithIterables(Map map, Iterable keys, Iterable values) { | |
180 Iterator keyIterator = keys.iterator; | |
181 Iterator valueIterator = values.iterator; | |
182 bool hasNextKey = keyIterator.moveNext(); | |
183 bool hasNextValue = valueIterator.moveNext(); | |
184 while (hasNextKey && hasNextValue) { | |
185 map[keyIterator.current] = valueIterator.current; | |
186 hasNextKey = keyIterator.moveNext(); | |
187 hasNextValue = valueIterator.moveNext(); | |
188 } | |
189 if (hasNextKey || hasNextValue) { | |
190 throw new ArgumentError("Iterables do not have same length."); | |
191 } | |
192 } | |
193 } | |
OLD | NEW |