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