Chromium Code Reviews| 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 _ImmutableArray kvPairs_; | 7 final _ImmutableArray _kvPairs; |
| 8 | 8 |
| 9 const ImmutableMap._create(_ImmutableArray keyValuePairs) | 9 const ImmutableMap._create(_ImmutableArray keyValuePairs) |
| 10 : kvPairs_ = keyValuePairs; | 10 : _kvPairs = keyValuePairs; |
| 11 | 11 |
| 12 | 12 |
| 13 V operator [](Object key) { | 13 V operator [](Object key) { |
| 14 // TODO(hausner): Since the keys are sorted, we could do a binary | 14 // TODO(hausner): Since the keys are sorted, we could do a binary |
| 15 // search. But is it worth it? | 15 // search. But is it worth it? |
|
Lasse Reichstein Nielsen
2013/09/24 07:43:21
Add a comment here: Yes, it may be worth it if map
floitsch
2013/09/24 11:19:13
There is already a TODO. No need to add our expect
| |
| 16 for (int i = 0; i < kvPairs_.length - 1; i += 2) { | 16 for (int i = 0; i < _kvPairs.length - 1; i += 2) { |
| 17 if (key == kvPairs_[i]) { | 17 if (key == _kvPairs[i]) { |
| 18 return kvPairs_[i+1]; | 18 return _kvPairs[i+1]; |
| 19 } | 19 } |
| 20 } | 20 } |
| 21 return null; | 21 return null; |
| 22 } | 22 } |
| 23 | 23 |
| 24 bool get isEmpty { | 24 bool get isEmpty { |
| 25 return kvPairs_.length == 0; | 25 return _kvPairs.length == 0; |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool get isNotEmpty => !isEmpty; | 28 bool get isNotEmpty => !isEmpty; |
| 29 | 29 |
| 30 int get length { | 30 int get length { |
| 31 return kvPairs_.length ~/ 2; | 31 return _kvPairs.length ~/ 2; |
| 32 } | 32 } |
| 33 | 33 |
| 34 void forEach(void f(K key, V value)) { | 34 void forEach(void f(K key, V value)) { |
| 35 for (int i = 0; i < kvPairs_.length; i += 2) { | 35 for (int i = 0; i < _kvPairs.length; i += 2) { |
| 36 f(kvPairs_[i], kvPairs_[i+1]); | 36 f(_kvPairs[i], _kvPairs[i+1]); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 Iterable<K> get keys { | 40 Iterable<K> get keys { |
| 41 return new _ImmutableMapKeyIterable<K>(this); | 41 return new _ImmutableMapKeyIterable<K>(this); |
| 42 } | 42 } |
| 43 | 43 |
| 44 Iterable<V> get values { | 44 Iterable<V> get values { |
| 45 return new _ImmutableMapValueIterable<V>(this); | 45 return new _ImmutableMapValueIterable<V>(this); |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool containsKey(Object key) { | 48 bool containsKey(Object key) { |
| 49 for (int i = 0; i < kvPairs_.length; i += 2) { | 49 for (int i = 0; i < _kvPairs.length; i += 2) { |
| 50 if (key == kvPairs_[i]) { | 50 if (key == _kvPairs[i]) { |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 return false; | 54 return false; |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool containsValue(Object value) { | 57 bool containsValue(Object value) { |
| 58 for (int i = 1; i < kvPairs_.length; i += 2) { | 58 for (int i = 1; i < _kvPairs.length; i += 2) { |
| 59 if (value == kvPairs_[i]) { | 59 if (value == _kvPairs[i]) { |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 return false; | 63 return false; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void operator []=(K key, V value) { | 66 void operator []=(K key, V value) { |
| 67 throw new UnsupportedError("Cannot set value in unmodifiable Map"); | 67 throw new UnsupportedError("Cannot set value in unmodifiable Map"); |
| 68 } | 68 } |
| 69 | 69 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 ImmutableMap _map; | 106 ImmutableMap _map; |
| 107 int _index = -1; | 107 int _index = -1; |
| 108 E _current; | 108 E _current; |
| 109 | 109 |
| 110 _ImmutableMapKeyIterator(this._map); | 110 _ImmutableMapKeyIterator(this._map); |
| 111 | 111 |
| 112 bool moveNext() { | 112 bool moveNext() { |
| 113 int newIndex = _index + 1; | 113 int newIndex = _index + 1; |
| 114 if (newIndex < _map.length) { | 114 if (newIndex < _map.length) { |
| 115 _index = newIndex; | 115 _index = newIndex; |
| 116 _current = _map.kvPairs_[newIndex * 2]; | 116 _current = _map._kvPairs[newIndex * 2]; |
| 117 return true; | 117 return true; |
| 118 } | 118 } |
| 119 _current = null; | 119 _current = null; |
| 120 _index = _map.length; | 120 _index = _map.length; |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 | 123 |
| 124 E get current => _current; | 124 E get current => _current; |
| 125 } | 125 } |
| 126 | 126 |
| 127 class _ImmutableMapValueIterator<E> implements Iterator<E> { | 127 class _ImmutableMapValueIterator<E> implements Iterator<E> { |
| 128 ImmutableMap _map; | 128 ImmutableMap _map; |
| 129 int _index = -1; | 129 int _index = -1; |
| 130 E _current; | 130 E _current; |
| 131 | 131 |
| 132 _ImmutableMapValueIterator(this._map); | 132 _ImmutableMapValueIterator(this._map); |
| 133 | 133 |
| 134 bool moveNext() { | 134 bool moveNext() { |
| 135 int newIndex = _index + 1; | 135 int newIndex = _index + 1; |
| 136 if (newIndex < _map.length) { | 136 if (newIndex < _map.length) { |
| 137 _index = newIndex; | 137 _index = newIndex; |
| 138 _current = _map.kvPairs_[newIndex * 2 + 1]; | 138 _current = _map._kvPairs[newIndex * 2 + 1]; |
| 139 return true; | 139 return true; |
| 140 } | 140 } |
| 141 _current = null; | 141 _current = null; |
| 142 _index = _map.length; | 142 _index = _map.length; |
| 143 return false; | 143 return false; |
| 144 } | 144 } |
| 145 | 145 |
| 146 E get current => _current; | 146 E get current => _current; |
| 147 } | 147 } |
| OLD | NEW |