OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 | 4 |
5 /// Defines [LookupMap], a simple map that can be optimized by dart2js. | 5 /// Defines [LookupMap], a simple map that can be optimized by dart2js. |
6 library lookup_map; | 6 library lookup_map; |
7 | 7 |
8 /// [LookupMap] is a simple, but very restricted map. The map can only hold | 8 /// [LookupMap] is a simple, but very restricted map. The map can only hold |
9 /// constant keys and the only way to use the map is to retrieve values with a | 9 /// constant keys and the only way to use the map is to retrieve values with a |
10 /// key you already have. Except for lookup, any other operation in [Map] (like | 10 /// key you already have. Except for lookup, any other operation in [Map] (like |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 /// const map = const LookupMap(const [A, 1], | 53 /// const map = const LookupMap(const [A, 1], |
54 /// const [const LookupMap(const [A, 2, B, 4]), | 54 /// const [const LookupMap(const [A, 2, B, 4]), |
55 /// const LookupMap(const [A, 3, B, 5])); | 55 /// const LookupMap(const [A, 3, B, 5])); |
56 /// | 56 /// |
57 /// `map[A]` returns `1` and `map[B]` returns `5`. | 57 /// `map[A]` returns `1` and `map[B]` returns `5`. |
58 /// | 58 /// |
59 /// Note: in the future we expect to change [entries] to be a const map | 59 /// Note: in the future we expect to change [entries] to be a const map |
60 /// instead of a list of key-value pairs. | 60 /// instead of a list of key-value pairs. |
61 // TODO(sigmund): make entries a map once we fix TypeImpl.== (issue #17207). | 61 // TODO(sigmund): make entries a map once we fix TypeImpl.== (issue #17207). |
62 const LookupMap(List entries, [List<LookupMap<K, V>> nestedMaps = const []]) | 62 const LookupMap(List entries, [List<LookupMap<K, V>> nestedMaps = const []]) |
63 : _key = null, _value = null, _entries = entries, _nestedMaps = nestedMaps; | 63 : _key = null, |
| 64 _value = null, |
| 65 _entries = entries, |
| 66 _nestedMaps = nestedMaps; |
64 | 67 |
65 /// Creates a lookup map with a single key-value pair. | 68 /// Creates a lookup map with a single key-value pair. |
66 const LookupMap.pair(K key, V value) | 69 const LookupMap.pair(K key, V value) |
67 : _key = key, _value = value, _entries = const [], _nestedMaps = const []; | 70 : _key = key, |
| 71 _value = value, |
| 72 _entries = const [], |
| 73 _nestedMaps = const []; |
68 | 74 |
69 /// Return the data corresponding to [key]. | 75 /// Return the data corresponding to [key]. |
70 V operator[](K key) { | 76 V operator [](K key) { |
71 var map = _flatMap[this]; | 77 var map = _flatMap[this]; |
72 if (map == null) { | 78 if (map == null) { |
73 map = {}; | 79 map = {}; |
74 _addEntriesTo(map); | 80 _addEntriesTo(map); |
75 _flatMap[this] = map; | 81 _flatMap[this] = map; |
76 } | 82 } |
77 return map[key]; | 83 return map[key]; |
78 } | 84 } |
79 | 85 |
80 /// Add to [map] entries from [nestedMaps] and from [entries] according to the | 86 /// Add to [map] entries from [nestedMaps] and from [entries] according to the |
(...skipping 10 matching lines...) Expand all Loading... |
91 /// An expando that stores a flatten version of a [LookupMap], this is | 97 /// An expando that stores a flatten version of a [LookupMap], this is |
92 /// computed and stored the first time the map is accessed. | 98 /// computed and stored the first time the map is accessed. |
93 final _flatMap = new Expando('_flat_map'); | 99 final _flatMap = new Expando('_flat_map'); |
94 | 100 |
95 /// Internal constant that matches the version in the pubspec. This is used by | 101 /// Internal constant that matches the version in the pubspec. This is used by |
96 /// dart2js to ensure that optimizations are only enabled on known versions of | 102 /// dart2js to ensure that optimizations are only enabled on known versions of |
97 /// this code. | 103 /// this code. |
98 // Note: this needs to be kept in sync with the pubspec, otherwise | 104 // Note: this needs to be kept in sync with the pubspec, otherwise |
99 // test/version_check_test would fail. | 105 // test/version_check_test would fail. |
100 final _version = '0.0.1+1'; | 106 final _version = '0.0.1+1'; |
OLD | NEW |