import java.util.Map;
import java.util.function.Consumer;
+import org.simantics.scl.runtime.tuple.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.THashMap;
+import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.procedure.TObjectObjectProcedure;
import gnu.trove.procedure.TObjectProcedure;
import gnu.trove.set.hash.THashSet;
*/
transient protected Map<String, T> configurationBySolverName;
+ /**
+ * Set of all mapped components indexed by their solver component id.
+ */
+ transient protected TIntObjectMap<T> configurationByComponentId;
+
/**
* Set of components whose removal is delayed because they might
* have been moved somewhere else.
}
}
+ @SuppressWarnings("unchecked")
public Map<String, T> getConfigurationBySolverName() {
Map<String, T> result = configurationBySolverName;
if (result == null) {
T configuration = getConfiguration();
- if (configuration != null)
- result = configurationBySolverName = createConfigurationBySolverName(configuration);
- else
+ if (configuration != null) {
+ Tuple2 t = createConfigurationCacheMaps(configuration);
+ return (Map<String, T>) t.get(0);
+ } else {
result = Collections.emptyMap();
+ }
+ }
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ public TIntObjectMap<T> getConfigurationByComponentId() {
+ TIntObjectMap<T> result = configurationByComponentId;
+ if (result == null) {
+ T configuration = getConfiguration();
+ if (configuration != null) {
+ Tuple2 t = createConfigurationCacheMaps(configuration);
+ return (TIntObjectMap<T>) t.get(1);
+ } else {
+ result = new TIntObjectHashMap<>(1);
+ }
}
return result;
}
- protected Map<String, T> createConfigurationBySolverName(T configuration) {
+ protected Tuple2 createConfigurationCacheMaps(T configuration) {
THashMap<String, T> configurationBySolverName = new THashMap<>();
- browseConfigurationBySolverName(configurationBySolverName, configuration);
- return configurationBySolverName;
+ TIntObjectMap<T> configurationByComponentId = new TIntObjectHashMap<>();
+ browseConfigurationCacheMaps(configurationBySolverName, configurationByComponentId, configuration);
+ this.configurationBySolverName = configurationBySolverName;
+ this.configurationByComponentId = configurationByComponentId;
+ return new Tuple2(configurationBySolverName, configurationByComponentId);
}
- private void browseConfigurationBySolverName(
+ private void browseConfigurationCacheMaps(
THashMap<String, T> configurationBySolverName,
- T configuration) {
+ TIntObjectMap<T> configurationByComponentId,
+ T configuration)
+ {
if (configuration.solverComponentName != null) {
configurationBySolverName.put(configuration.solverComponentName, configuration);
+ configurationByComponentId.put(configuration.componentId, configuration);
} else if (configuration.componentId != 0) {
LOGGER.warn("configuration.solverComponentName is null! configuration uid is {} and component id {}", configuration.getUid(), configuration.componentId);
}
for(T child : configuration.getChildren()) {
- browseConfigurationBySolverName(configurationBySolverName, child);
- child.parent = configuration;
+ browseConfigurationCacheMaps(configurationBySolverName, configurationByComponentId, child);
}
}