| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 IterableBase<E> | 88 class _ImmutableMapKeyIterable<E> extends IterableBase<E> |
| 89 implements EfficientLength { | 89 implements EfficientLength { |
| 90 final ImmutableMap _map; | 90 final _ImmutableMap _map; |
| 91 _ImmutableMapKeyIterable(this._map); | 91 _ImmutableMapKeyIterable(this._map); |
| 92 | 92 |
| 93 Iterator<E> get iterator { | 93 Iterator<E> get iterator { |
| 94 return new _ImmutableMapKeyIterator<E>(_map); | 94 return new _ImmutableMapKeyIterator<E>(_map); |
| 95 } | 95 } |
| 96 | 96 |
| 97 int get length => _map.length; | 97 int get length => _map.length; |
| 98 } | 98 } |
| 99 | 99 |
| 100 class _ImmutableMapValueIterable<E> extends IterableBase<E> | 100 class _ImmutableMapValueIterable<E> extends IterableBase<E> |
| 101 implements EfficientLength { | 101 implements EfficientLength { |
| 102 final ImmutableMap _map; | 102 final _ImmutableMap _map; |
| 103 _ImmutableMapValueIterable(this._map); | 103 _ImmutableMapValueIterable(this._map); |
| 104 | 104 |
| 105 Iterator<E> get iterator { | 105 Iterator<E> get iterator { |
| 106 return new _ImmutableMapValueIterator<E>(_map); | 106 return new _ImmutableMapValueIterator<E>(_map); |
| 107 } | 107 } |
| 108 | 108 |
| 109 int get length => _map.length; | 109 int get length => _map.length; |
| 110 } | 110 } |
| 111 | 111 |
| 112 class _ImmutableMapKeyIterator<E> implements Iterator<E> { | 112 class _ImmutableMapKeyIterator<E> implements Iterator<E> { |
| 113 ImmutableMap _map; | 113 _ImmutableMap _map; |
| 114 int _index = -1; | 114 int _index = -1; |
| 115 E _current; | 115 E _current; |
| 116 | 116 |
| 117 _ImmutableMapKeyIterator(this._map); | 117 _ImmutableMapKeyIterator(this._map); |
| 118 | 118 |
| 119 bool moveNext() { | 119 bool moveNext() { |
| 120 int newIndex = _index + 1; | 120 int newIndex = _index + 1; |
| 121 if (newIndex < _map.length) { | 121 if (newIndex < _map.length) { |
| 122 _index = newIndex; | 122 _index = newIndex; |
| 123 _current = _map._kvPairs[newIndex * 2]; | 123 _current = _map._kvPairs[newIndex * 2]; |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 _current = null; | 126 _current = null; |
| 127 _index = _map.length; | 127 _index = _map.length; |
| 128 return false; | 128 return false; |
| 129 } | 129 } |
| 130 | 130 |
| 131 E get current => _current; | 131 E get current => _current; |
| 132 } | 132 } |
| 133 | 133 |
| 134 class _ImmutableMapValueIterator<E> implements Iterator<E> { | 134 class _ImmutableMapValueIterator<E> implements Iterator<E> { |
| 135 ImmutableMap _map; | 135 _ImmutableMap _map; |
| 136 int _index = -1; | 136 int _index = -1; |
| 137 E _current; | 137 E _current; |
| 138 | 138 |
| 139 _ImmutableMapValueIterator(this._map); | 139 _ImmutableMapValueIterator(this._map); |
| 140 | 140 |
| 141 bool moveNext() { | 141 bool moveNext() { |
| 142 int newIndex = _index + 1; | 142 int newIndex = _index + 1; |
| 143 if (newIndex < _map.length) { | 143 if (newIndex < _map.length) { |
| 144 _index = newIndex; | 144 _index = newIndex; |
| 145 _current = _map._kvPairs[newIndex * 2 + 1]; | 145 _current = _map._kvPairs[newIndex * 2 + 1]; |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 _current = null; | 148 _current = null; |
| 149 _index = _map.length; | 149 _index = _map.length; |
| 150 return false; | 150 return false; |
| 151 } | 151 } |
| 152 | 152 |
| 153 E get current => _current; | 153 E get current => _current; |
| 154 } | 154 } |
| OLD | NEW |