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(ImmutableArray keyValuePairs) | 9 const ImmutableMap(ImmutableArray keyValuePairs) |
10 : kvPairs_ = keyValuePairs; | 10 : kvPairs_ = keyValuePairs; |
(...skipping 19 matching lines...) Expand all Loading... |
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() { |
39 int numKeys = length; | 39 int numKeys = length; |
40 Array<K> array = new Array<K>(numKeys); | 40 List<K> list = new List<K>(numKeys); |
41 for (int i = 0; i < numKeys; i++) { | 41 for (int i = 0; i < numKeys; i++) { |
42 array[i] = kvPairs_[i*2]; | 42 list[i] = kvPairs_[i*2]; |
43 } | 43 } |
44 return array; | 44 return list; |
45 } | 45 } |
46 | 46 |
47 Collection<V> getValues() { | 47 Collection<V> getValues() { |
48 int numValues = length; | 48 int numValues = length; |
49 Array<K> array = new Array<K>(numValues); | 49 List<K> list = new List<K>(numValues); |
50 for (int i = 0; i < numValues; i++) { | 50 for (int i = 0; i < numValues; i++) { |
51 array[i] = kvPairs_[i*2 + 1]; | 51 list[i] = kvPairs_[i*2 + 1]; |
52 } | 52 } |
53 return array; | 53 return list; |
54 } | 54 } |
55 | 55 |
56 bool containsKey(K key) { | 56 bool containsKey(K key) { |
57 for (int i = 0; i < kvPairs_.length; i += 2) { | 57 for (int i = 0; i < kvPairs_.length; i += 2) { |
58 if (key == kvPairs_[i]) { | 58 if (key == kvPairs_[i]) { |
59 return true; | 59 return true; |
60 } | 60 } |
61 } | 61 } |
62 return false; | 62 return false; |
63 } | 63 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // 2*n, the values at position 2*n+1. | 99 // 2*n, the values at position 2*n+1. |
100 static fromLiteral(Array elements) { | 100 static fromLiteral(Array elements) { |
101 var map = new LinkedHashMap(); | 101 var map = new LinkedHashMap(); |
102 var len = elements.length; | 102 var len = elements.length; |
103 for (int i = 1; i < len; i += 2) { | 103 for (int i = 1; i < len; i += 2) { |
104 map[elements[i-1]] = elements[i]; | 104 map[elements[i-1]] = elements[i]; |
105 } | 105 } |
106 return map; | 106 return map; |
107 } | 107 } |
108 } | 108 } |
OLD | NEW |