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 // Immutable map class for compiler generated map literals. | 4 // Immutable map class for compiler generated map literals. |
5 | 5 |
6 class _ImmutableMap<K, V> implements Map<K, V> { | 6 class _ImmutableMap<K, V> implements Map<K, V> { |
7 final _ImmutableList _kvPairs; | 7 final _ImmutableList _kvPairs; |
8 | 8 |
9 const _ImmutableMap._create(_ImmutableList keyValuePairs) | 9 const _ImmutableMap._create(_ImmutableList keyValuePairs) |
10 : _kvPairs = keyValuePairs; | 10 : _kvPairs = keyValuePairs; |
11 | 11 |
| 12 |
12 V operator [](Object key) { | 13 V operator [](Object key) { |
13 // To preserve the key-value order of the map literal, the keys are | 14 // To preserve the key-value order of the map literal, the keys are |
14 // not sorted. Need to do linear search or implement an additional | 15 // not sorted. Need to do linear search or implement an additional |
15 // lookup table. | 16 // lookup table. |
16 for (int i = 0; i < _kvPairs.length - 1; i += 2) { | 17 for (int i = 0; i < _kvPairs.length - 1; i += 2) { |
17 if (key == _kvPairs[i]) { | 18 if (key == _kvPairs[i]) { |
18 return _kvPairs[i + 1]; | 19 return _kvPairs[i+1]; |
19 } | 20 } |
20 } | 21 } |
21 return null; | 22 return null; |
22 } | 23 } |
23 | 24 |
24 bool get isEmpty { | 25 bool get isEmpty { |
25 return _kvPairs.length == 0; | 26 return _kvPairs.length == 0; |
26 } | 27 } |
27 | 28 |
28 bool get isNotEmpty => !isEmpty; | 29 bool get isNotEmpty => !isEmpty; |
29 | 30 |
30 int get length { | 31 int get length { |
31 return _kvPairs.length ~/ 2; | 32 return _kvPairs.length ~/ 2; |
32 } | 33 } |
33 | 34 |
34 void forEach(void f(K key, V value)) { | 35 void forEach(void f(K key, V value)) { |
35 for (int i = 0; i < _kvPairs.length; i += 2) { | 36 for (int i = 0; i < _kvPairs.length; i += 2) { |
36 f(_kvPairs[i], _kvPairs[i + 1]); | 37 f(_kvPairs[i], _kvPairs[i+1]); |
37 } | 38 } |
38 } | 39 } |
39 | 40 |
40 Iterable<K> get keys { | 41 Iterable<K> get keys { |
41 return new _ImmutableMapKeyIterable<K>(this); | 42 return new _ImmutableMapKeyIterable<K>(this); |
42 } | 43 } |
43 | 44 |
44 Iterable<V> get values { | 45 Iterable<V> get values { |
45 return new _ImmutableMapValueIterable<V>(this); | 46 return new _ImmutableMapValueIterable<V>(this); |
46 } | 47 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 _current = _map._kvPairs[newIndex * 2 + 1]; | 143 _current = _map._kvPairs[newIndex * 2 + 1]; |
143 return true; | 144 return true; |
144 } | 145 } |
145 _current = null; | 146 _current = null; |
146 _index = _map.length; | 147 _index = _map.length; |
147 return false; | 148 return false; |
148 } | 149 } |
149 | 150 |
150 E get current => _current; | 151 E get current => _current; |
151 } | 152 } |
OLD | NEW |