| 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 |
| 13 V operator [](Object key) { | 13 V operator [](Object key) { |
| 14 // 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 |
| 15 // not sorted. Need to do linear search or implement an additional | 15 // not sorted. Need to do linear search or implement an additional |
| 16 // lookup table. | 16 // lookup table. |
| 17 for (int i = 0; i < _kvPairs.length - 1; i += 2) { | 17 for (int i = 0; i < _kvPairs.length - 1; i += 2) { |
| 18 if (key == _kvPairs[i]) { | 18 if (key == _kvPairs[i]) { |
| 19 return _kvPairs[i+1]; | 19 return _kvPairs[i+1]; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 V remove(Object key) { | 79 V remove(Object key) { |
| 80 throw new UnsupportedError("Cannot remove from unmodifiable Map"); | 80 throw new UnsupportedError("Cannot remove from unmodifiable Map"); |
| 81 } | 81 } |
| 82 | 82 |
| 83 String toString() { | 83 String toString() { |
| 84 return Maps.mapToString(this); | 84 return Maps.mapToString(this); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 class _ImmutableMapKeyIterable<E> extends EfficientLengthIterable<E> { | 88 class _ImmutableMapKeyIterable<E> extends EfficientLengthIterable<E> { |
| 89 final ImmutableMap _map; | 89 final _ImmutableMap _map; |
| 90 _ImmutableMapKeyIterable(this._map); | 90 _ImmutableMapKeyIterable(this._map); |
| 91 | 91 |
| 92 Iterator<E> get iterator { | 92 Iterator<E> get iterator { |
| 93 return new _ImmutableMapKeyIterator<E>(_map); | 93 return new _ImmutableMapKeyIterator<E>(_map); |
| 94 } | 94 } |
| 95 | 95 |
| 96 int get length => _map.length; | 96 int get length => _map.length; |
| 97 } | 97 } |
| 98 | 98 |
| 99 class _ImmutableMapValueIterable<E> extends EfficientLengthIterable<E> { | 99 class _ImmutableMapValueIterable<E> extends EfficientLengthIterable<E> { |
| 100 final ImmutableMap _map; | 100 final _ImmutableMap _map; |
| 101 _ImmutableMapValueIterable(this._map); | 101 _ImmutableMapValueIterable(this._map); |
| 102 | 102 |
| 103 Iterator<E> get iterator { | 103 Iterator<E> get iterator { |
| 104 return new _ImmutableMapValueIterator<E>(_map); | 104 return new _ImmutableMapValueIterator<E>(_map); |
| 105 } | 105 } |
| 106 | 106 |
| 107 int get length => _map.length; | 107 int get length => _map.length; |
| 108 } | 108 } |
| 109 | 109 |
| 110 class _ImmutableMapKeyIterator<E> implements Iterator<E> { | 110 class _ImmutableMapKeyIterator<E> implements Iterator<E> { |
| 111 ImmutableMap _map; | 111 _ImmutableMap _map; |
| 112 int _index = -1; | 112 int _index = -1; |
| 113 E _current; | 113 E _current; |
| 114 | 114 |
| 115 _ImmutableMapKeyIterator(this._map); | 115 _ImmutableMapKeyIterator(this._map); |
| 116 | 116 |
| 117 bool moveNext() { | 117 bool moveNext() { |
| 118 int newIndex = _index + 1; | 118 int newIndex = _index + 1; |
| 119 if (newIndex < _map.length) { | 119 if (newIndex < _map.length) { |
| 120 _index = newIndex; | 120 _index = newIndex; |
| 121 _current = _map._kvPairs[newIndex * 2]; | 121 _current = _map._kvPairs[newIndex * 2]; |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 _current = null; | 124 _current = null; |
| 125 _index = _map.length; | 125 _index = _map.length; |
| 126 return false; | 126 return false; |
| 127 } | 127 } |
| 128 | 128 |
| 129 E get current => _current; | 129 E get current => _current; |
| 130 } | 130 } |
| 131 | 131 |
| 132 class _ImmutableMapValueIterator<E> implements Iterator<E> { | 132 class _ImmutableMapValueIterator<E> implements Iterator<E> { |
| 133 ImmutableMap _map; | 133 _ImmutableMap _map; |
| 134 int _index = -1; | 134 int _index = -1; |
| 135 E _current; | 135 E _current; |
| 136 | 136 |
| 137 _ImmutableMapValueIterator(this._map); | 137 _ImmutableMapValueIterator(this._map); |
| 138 | 138 |
| 139 bool moveNext() { | 139 bool moveNext() { |
| 140 int newIndex = _index + 1; | 140 int newIndex = _index + 1; |
| 141 if (newIndex < _map.length) { | 141 if (newIndex < _map.length) { |
| 142 _index = newIndex; | 142 _index = newIndex; |
| 143 _current = _map._kvPairs[newIndex * 2 + 1]; | 143 _current = _map._kvPairs[newIndex * 2 + 1]; |
| 144 return true; | 144 return true; |
| 145 } | 145 } |
| 146 _current = null; | 146 _current = null; |
| 147 _index = _map.length; | 147 _index = _map.length; |
| 148 return false; | 148 return false; |
| 149 } | 149 } |
| 150 | 150 |
| 151 E get current => _current; | 151 E get current => _current; |
| 152 } | 152 } |
| OLD | NEW |