OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library services.src.index.store.codec; | 5 library services.src.index.store.codec; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
11 import 'package:analysis_services/src/index/store/collection.dart'; | 11 import 'package:analysis_services/src/index/store/collection.dart'; |
12 import 'package:analysis_services/index/index.dart'; | 12 import 'package:analysis_services/index/index.dart'; |
13 | 13 |
14 | 14 |
15 /** | 15 /** |
16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. | 16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. |
17 */ | 17 */ |
18 class ContextCodec { | 18 class ContextCodec { |
19 /** | 19 /** |
20 * A table mapping contexts to their unique indices. | 20 * A table mapping contexts to their unique indices. |
21 */ | 21 */ |
22 Map<AnalysisContext, int> _contextToIndex = new HashMap<AnalysisContext, int>( | 22 Map<AnalysisContext, int> _contextToIndex = |
23 ); | 23 new HashMap<AnalysisContext, int>(); |
24 | 24 |
25 /** | 25 /** |
26 * A table mapping indices to the corresponding contexts. | 26 * A table mapping indices to the corresponding contexts. |
27 */ | 27 */ |
28 Map<int, AnalysisContext> _indexToContext = new HashMap<int, AnalysisContext>( | 28 Map<int, AnalysisContext> _indexToContext = |
29 ); | 29 new HashMap<int, AnalysisContext>(); |
30 | 30 |
31 /** | 31 /** |
32 * The next id to assign. | 32 * The next id to assign. |
33 */ | 33 */ |
34 int _nextId = 0; | 34 int _nextId = 0; |
35 | 35 |
36 /** | 36 /** |
37 * Returns the [AnalysisContext] that corresponds to the given index. | 37 * Returns the [AnalysisContext] that corresponds to the given index. |
38 */ | 38 */ |
39 AnalysisContext decode(int index) => _indexToContext[index]; | 39 AnalysisContext decode(int index) => _indexToContext[index]; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 * Returns an [Element] that corresponds to the given location. | 85 * Returns an [Element] that corresponds to the given location. |
86 * | 86 * |
87 * @param context the [AnalysisContext] to find [Element] in | 87 * @param context the [AnalysisContext] to find [Element] in |
88 * @param index an integer corresponding to the [Element] | 88 * @param index an integer corresponding to the [Element] |
89 * @return the [Element] or `null` | 89 * @return the [Element] or `null` |
90 */ | 90 */ |
91 Element decode(AnalysisContext context, int index) { | 91 Element decode(AnalysisContext context, int index) { |
92 List<int> path = _indexToPath[index]; | 92 List<int> path = _indexToPath[index]; |
93 List<String> components = _getLocationComponents(path); | 93 List<String> components = _getLocationComponents(path); |
94 ElementLocation location = new ElementLocationImpl.con3(components); | 94 ElementLocation location = new ElementLocationImpl.con3(components); |
95 return context.getElement(location); | 95 Element element = context.getElement(location); |
| 96 return element; |
96 } | 97 } |
97 | 98 |
98 /** | 99 /** |
99 * Returns a unique integer that corresponds to the given [Element]. | 100 * Returns a unique integer that corresponds to the given [Element]. |
100 */ | 101 */ |
101 int encode(Element element) { | 102 int encode(Element element) { |
102 List<int> path = _getLocationPath(element); | 103 List<int> path = _getLocationPath(element); |
103 int index = _pathToIndex[path]; | 104 int index = _pathToIndex[path]; |
104 if (index == null) { | 105 if (index == null) { |
105 index = _indexToPath.length; | 106 index = _indexToPath.length; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 int encode(String name) { | 208 int encode(String name) { |
208 int index = nameToIndex[name]; | 209 int index = nameToIndex[name]; |
209 if (index == null) { | 210 if (index == null) { |
210 index = _indexToName.length; | 211 index = _indexToName.length; |
211 nameToIndex[name] = index; | 212 nameToIndex[name] = index; |
212 _indexToName.add(name); | 213 _indexToName.add(name); |
213 } | 214 } |
214 return index; | 215 return index; |
215 } | 216 } |
216 } | 217 } |
OLD | NEW |