some refactor and expanded tables functionality

This commit is contained in:
2026-03-24 13:42:15 +00:00
parent 9170104a12
commit 47a077bffc
15 changed files with 322 additions and 166 deletions
@@ -25,7 +25,7 @@ public class BuildingIssueSink extends ReadOnlyCollection<BuildingIssue> {
return this;
}
public BuildingIssueSink report(final BuildingIssueSink issues) {
public BuildingIssueSink merge(final BuildingIssueSink issues) {
hasErrors |= issues.hasErrors();
collection.addAll(issues.collection);
return this;
@@ -3,11 +3,13 @@ package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.SourceIdentifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
public abstract class DenseTable<IDENTIFIER extends SourceIdentifier, VALUE> {
public abstract class DenseTable<IDENTIFIER extends SourceIdentifier, VALUE>
implements DenseTableReader<IDENTIFIER, VALUE>, DenseTableWriter<IDENTIFIER, VALUE> {
private final List<VALUE> values = new ArrayList<>();
private final Function<Integer, IDENTIFIER> identifierGenerator;
@@ -15,19 +17,65 @@ public abstract class DenseTable<IDENTIFIER extends SourceIdentifier, VALUE> {
this.identifierGenerator = Objects.requireNonNull(identifierGenerator);
}
@Override
public final int size() { return values.size(); }
public final VALUE get(IDENTIFIER id) {
return values.get(id.getIndex());
@Override
public final VALUE get(final IDENTIFIER identifier) {
return values.get(identifier.getIndex());
}
public IDENTIFIER register(VALUE value) {
@Override
public IDENTIFIER register(final VALUE value) {
final int idx = values.size();
values.add(value);
return identifierGenerator.apply(idx);
}
protected void clear() {
@Override
public void clear() {
values.clear();
}
@Override
public Iterable<IDENTIFIER> identifiers() {
return () -> new Iterator<>() {
private int current = 0;
@Override
public boolean hasNext() {
return current < values.size();
}
@Override
public IDENTIFIER next() {
return identifierGenerator.apply(current++);
}
};
}
@Override
public Iterable<VALUE> values() {
return () -> new Iterator<>() {
private int current = 0;
@Override
public boolean hasNext() {
return current < values.size();
}
@Override
public VALUE next() {
final var identifier = identifierGenerator.apply(current++);
return get(identifier);
}
};
}
@Override
public Iterator<IDENTIFIER> iterator() {
return identifiers().iterator();
}
}
@@ -0,0 +1,10 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.SourceIdentifier;
public interface DenseTableReader<IDENTIFIER extends SourceIdentifier, VALUE> extends Iterable<IDENTIFIER> {
int size();
VALUE get(IDENTIFIER identifier);
Iterable<IDENTIFIER> identifiers();
Iterable<VALUE> values();
}
@@ -0,0 +1,8 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.SourceIdentifier;
public interface DenseTableWriter<IDENTIFIER extends SourceIdentifier, VALUE> {
IDENTIFIER register(VALUE value);
void clear();
}
@@ -8,7 +8,7 @@ import p.studio.utilities.structures.ReadOnlyList;
import java.util.*;
public class FileTable extends InternTable<FileId, SourceHandle> {
public class FileTable extends InternTable<FileId, SourceHandle> implements FileTableReader {
private final Map<ProjectId, Set<FileId>> projectFiles = new HashMap<>();
@@ -23,6 +23,7 @@ public class FileTable extends InternTable<FileId, SourceHandle> {
return fileId;
}
@Override
public ReadOnlyList<FileId> getFiles(final ProjectId projectId) {
final var fileIds = projectFiles.get(projectId);
if (CollectionUtils.isEmpty(fileIds)) {
@@ -0,0 +1,10 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.ProjectId;
import p.studio.utilities.structures.ReadOnlyList;
public interface FileTableReader extends DenseTableReader<FileId, SourceHandle> {
ReadOnlyList<FileId> getFiles(ProjectId projectId);
}
@@ -8,11 +8,12 @@ import java.util.Optional;
import java.util.function.Function;
public abstract class InternTable<IDENTIFIER extends SourceIdentifier, VALUE>
extends DenseTable<IDENTIFIER, VALUE> {
extends DenseTable<IDENTIFIER, VALUE>
implements InternTableReader<IDENTIFIER, VALUE> {
private final Map<VALUE, IDENTIFIER> identifierByValue = new HashMap<>();
public InternTable(Function<Integer, IDENTIFIER> identifierGenerator) {
public InternTable(final Function<Integer, IDENTIFIER> identifierGenerator) {
super(identifierGenerator);
}
@@ -21,17 +22,19 @@ public abstract class InternTable<IDENTIFIER extends SourceIdentifier, VALUE>
return identifierByValue.computeIfAbsent(value, super::register);
}
public Optional<IDENTIFIER> optional(final VALUE value) {
return Optional.ofNullable(identifierByValue.get(value));
}
@Override
public void clear() {
super.clear();
identifierByValue.clear();
}
public boolean containsKey(VALUE value) {
@Override
public Optional<IDENTIFIER> optional(final VALUE value) {
return Optional.ofNullable(identifierByValue.get(value));
}
@Override
public boolean containsKey(final VALUE value) {
return identifierByValue.containsKey(value);
}
}
@@ -0,0 +1,10 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.source.identifiers.SourceIdentifier;
import java.util.Optional;
public interface InternTableReader<IDENTIFIER extends SourceIdentifier, VALUE> extends DenseTableReader<IDENTIFIER, VALUE> {
Optional<IDENTIFIER> optional(VALUE value);
boolean containsKey(VALUE value);
}
@@ -8,7 +8,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class ProjectTable extends DenseTable<ProjectId, ProjectDescriptor> {
public class ProjectTable extends DenseTable<ProjectId, ProjectDescriptor> implements ProjectTableReader {
private final Map<Path, ProjectId> projectIdByPath = new HashMap<>();
@@ -27,11 +27,13 @@ public class ProjectTable extends DenseTable<ProjectId, ProjectDescriptor> {
projectIdByPath.clear();
}
public Optional<ProjectDescriptor> optional(Path pathCanon) {
@Override
public Optional<ProjectDescriptor> optional(final Path pathCanon) {
return optionalId(pathCanon).map(this::get);
}
public Optional<ProjectId> optionalId(Path pathCanon) {
@Override
public Optional<ProjectId> optionalId(final Path pathCanon) {
return Optional.ofNullable(projectIdByPath.get(pathCanon));
}
}
@@ -0,0 +1,12 @@
package p.studio.compiler.source.tables;
import p.studio.compiler.models.ProjectDescriptor;
import p.studio.compiler.source.identifiers.ProjectId;
import java.nio.file.Path;
import java.util.Optional;
public interface ProjectTableReader extends DenseTableReader<ProjectId, ProjectDescriptor> {
Optional<ProjectDescriptor> optional(Path pathCanon);
Optional<ProjectId> optionalId(Path pathCanon);
}