Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Unified Diff: pkg/compiler/lib/src/kernel/env.dart

Issue 3004433002: Encapsulate the index based maps for entities. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/kernel/env.dart
diff --git a/pkg/compiler/lib/src/kernel/env.dart b/pkg/compiler/lib/src/kernel/env.dart
index 845feeec36a793d77bbc7996ca85053d0506bb97..17f3d91cfb3e20678b25f47081313981671bdf61 100644
--- a/pkg/compiler/lib/src/kernel/env.dart
+++ b/pkg/compiler/lib/src/kernel/env.dart
@@ -19,6 +19,106 @@ import 'element_map.dart';
import 'element_map_impl.dart';
import 'element_map_mixins.dart';
+/// Base implementation for an index based map of entities of type [E].
+abstract class EntityMapBase<E> {
+ List<E> _list = <E>[];
+
+ /// Returns the [index]th entity in the map.
+ E getEntity(int index) => _list[index];
+
+ /// Returns the number entities in the map.
+ int get length => _list.length;
+}
+
+/// Index based map of entities of type [E].
+class EntityMap<E> extends EntityMapBase<E> {
+ /// Registers a new entity.
+ ///
+ /// [createEntity] is called to create the entity with the given index.
+ E0 register<E0 extends E>(E0 createEntity(int index)) {
+ E0 entity = createEntity(_list.length);
+ assert(entity != null);
+ _list.add(entity);
+ return entity;
+ }
+}
+
+/// Base implementation of an index based map of entities of type [E] with a
+/// corresponding data object of type [D].
+abstract class EntityDataMapBase<E, D> extends EntityMapBase<E> {
+ List<D> _data = <D>[];
+
+ /// Returns the data object stored for the [index]th entity.
+ D getData(int index) {
Siggi Cherem (dart-lang) 2017/08/22 19:12:22 what about making a generic Indexable interface an
+ if (index < length && index >= _data.length) {
+ throw new StateError(
+ 'Data is in the process of being created for ${_list[index]}.');
+ }
+ return _data[index];
+ }
+}
+
+/// Index based map of entities of type [E] with a corresponding data object
+/// of type [D].
+class EntityDataMap<E, D> extends EntityDataMapBase<E, D> {
+ /// Registers a new entity with an associated data object.
+ ///
+ /// Firstly, [createEntity] is called to create the entity with the given
+ /// index. Secondly, [createData] is called with the newly created entity to
+ /// create the associated data object.
+ E0 register<E0 extends E, D0 extends D>(
+ E0 createEntity(int index), D0 createData(E0 entity)) {
+ E0 entity = createEntity(_list.length);
+ assert(entity != null);
+ _list.add(entity);
+ D0 data = createData(entity);
+ assert(data != null);
+ _data.add(data);
+ return entity;
+ }
+}
+
+/// Base implementation for an index based of entities of type [E] with a
+/// corresponding data object of type [D] and an environment of type [V].
+abstract class EntityDataEnvMapBase<E, D, V> extends EntityDataMapBase<E, D> {
+ List<V> _env = <V>[];
+
+ /// Returns the environment object stored for the [index]th entity.
+ V getEnv(int index) {
+ if (index < length && index >= _env.length) {
+ throw new StateError(
+ 'Env is in the process of being created for ${_list[index]}.');
+ }
+ return _env[index];
+ }
+}
+
+/// Index based of entities of type [E] with a corresponding data object of
+/// type [D] and an environment of type [V].
+class EntityDataEnvMap<E, D, V> extends EntityDataEnvMapBase<E, D, V> {
+ /// Registers a new entity with an associated data object and environment.
+ ///
+ /// Firstly, [createEntity] is called to create the entity with the given
+ /// index. Secondly, [createData] is called with the newly created entity to
+ /// create the associated data object. Thirdly, [createEnv] is called with
+ /// the newly created entity to create the associated environment object.
+ E0 register<E0 extends E, D0 extends D, V0 extends V>(
+ E0 createEntity(int index),
Siggi Cherem (dart-lang) 2017/08/22 19:12:22 can we switch to take the actual values instead of
+ D0 createData(E0 entity),
+ V0 createEnv(E0 entity)) {
+ E0 entity = createEntity(_list.length);
+ assert(entity != null);
+ _list.add(entity);
+ D0 data = createData(entity);
+ assert(data != null);
+ _data.add(data);
+ V0 env = createEnv(entity);
+ assert(env != null);
+ _env.add(env);
+ return entity;
+ }
+}
+
/// Environment for fast lookup of program libraries.
class ProgramEnv {
final Set<ir.Program> _programs = new Set<ir.Program>();

Powered by Google App Engine
This is Rietveld 408576698