OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 [](K 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 |
24 bool isEmpty() { | 24 bool isEmpty() { |
25 return kvPairs_.length === 0; | 25 return kvPairs_.length === 0; |
26 } | 26 } |
27 | 27 |
28 int get length() { | 28 int get length { |
29 return kvPairs_.length ~/ 2; | 29 return kvPairs_.length ~/ 2; |
30 } | 30 } |
31 | 31 |
32 void forEach(void f(K key, V value)) { | 32 void forEach(void f(K key, V value)) { |
33 for (int i = 0; i < kvPairs_.length; i += 2) { | 33 for (int i = 0; i < kvPairs_.length; i += 2) { |
34 f(kvPairs_[i], kvPairs_[i+1]); | 34 f(kvPairs_[i], kvPairs_[i+1]); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 Collection<K> getKeys() { | 38 Collection<K> getKeys() { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 85 |
86 V remove(K key) { | 86 V remove(K key) { |
87 throw const IllegalAccessException(); | 87 throw const IllegalAccessException(); |
88 } | 88 } |
89 | 89 |
90 String toString() { | 90 String toString() { |
91 return Maps.mapToString(this); | 91 return Maps.mapToString(this); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
OLD | NEW |