| 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 [](K 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? |
| 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 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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(K 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(V 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 |
| 70 V putIfAbsent(K key, V ifAbsent()) { | 70 V putIfAbsent(K key, V ifAbsent()) { |
| 71 throw new UnsupportedError("Cannot set value in unmodifiable Map"); | 71 throw new UnsupportedError("Cannot set value in unmodifiable Map"); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void clear() { | 74 void clear() { |
| 75 throw new UnsupportedError("Cannot clear unmodifiable Map"); | 75 throw new UnsupportedError("Cannot clear unmodifiable Map"); |
| 76 } | 76 } |
| 77 | 77 |
| 78 V remove(K key) { | 78 V remove(Object key) { |
| 79 throw new UnsupportedError("Cannot remove from unmodifiable Map"); | 79 throw new UnsupportedError("Cannot remove from unmodifiable Map"); |
| 80 } | 80 } |
| 81 | 81 |
| 82 String toString() { | 82 String toString() { |
| 83 return Maps.mapToString(this); | 83 return Maps.mapToString(this); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 class _ImmutableMapKeyIterable<E> extends IterableBase<E> { | 87 class _ImmutableMapKeyIterable<E> extends IterableBase<E> { |
| 88 final ImmutableMap _map; | 88 final ImmutableMap _map; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |