changed TokenAnalysisPaths to lists

because the hash code of TokenAnalysisPath objects changes on
add-operations; instead of sets we use lists and manually check for
collisions when merging
This commit is contained in:
overflowerror 2022-02-03 19:12:30 +01:00
parent 145b55c187
commit 742b1dbfbc
2 changed files with 27 additions and 21 deletions

View file

@ -23,11 +23,11 @@ public class TokenAnalysisPath {
private List<Integer> remainingIndexes;
private int position = 1;
public TokenAnalysisPath(List<Integer> indexes) {
TokenAnalysisPath(List<Integer> indexes) {
this.remainingIndexes = new LinkedList<>(indexes);
}
public TokenAnalysisPath(TokenAnalysisPath prefix) {
TokenAnalysisPath(TokenAnalysisPath prefix) {
this(prefix.remainingIndexes);
path = new LinkedList<>(prefix.path);
position = prefix.position;
@ -37,7 +37,7 @@ public class TokenAnalysisPath {
return position - 1;
}
public boolean isDone() {
boolean isDone() {
return remainingIndexes.isEmpty();
}
@ -49,7 +49,7 @@ public class TokenAnalysisPath {
}
}
public boolean add(AbstractElement element) {
boolean add(AbstractElement element) {
if (isDone())
return false;
@ -93,10 +93,7 @@ public class TokenAnalysisPath {
if (getClass() != obj.getClass())
return false;
TokenAnalysisPath other = (TokenAnalysisPath) obj;
if (path == null) {
if (other.path != null)
return false;
} else if (!path.equals(other.path))
if (!path.equals(other.path))
return false;
if (position != other.position)
return false;

View file

@ -8,30 +8,21 @@
*******************************************************************************/
package org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.pathAnalysis;
import java.util.LinkedHashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.token.Token;
import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.utils.StreamUtils;
/**
* @author overflow - Initial contribution and APILinkedHashSet
*/
public class TokenAnalysisPaths {
private Set<TokenAnalysisPath> tokenPaths = new LinkedHashSet<>();
private List<TokenAnalysisPath> tokenPaths = new ArrayList<>(10);
private boolean isEmpty = false;
private boolean hasProgress = false;
public List<List<Token>> getTokenPaths() {
return tokenPaths.stream()
.map(TokenAnalysisPath::getTokenPath)
.distinct()
.collect(Collectors.toList());
}
public TokenAnalysisPaths(List<Integer> indexes) {
tokenPaths.add(new TokenAnalysisPath(indexes));
}
@ -39,10 +30,17 @@ public class TokenAnalysisPaths {
public TokenAnalysisPaths(TokenAnalysisPaths prefix) {
this.tokenPaths = prefix.tokenPaths.stream()
.map(TokenAnalysisPath::new)
.collect(StreamUtils.collectToLinkedHashSet());
.collect(Collectors.toList());
this.hasProgress = prefix.hasProgress;
}
public List<List<Token>> getTokenPaths() {
return tokenPaths.stream()
.map(TokenAnalysisPath::getTokenPath)
.distinct()
.collect(Collectors.toList());
}
public boolean isDone() {
return !isEmpty && tokenPaths.stream().allMatch(TokenAnalysisPath::isDone);
}
@ -59,6 +57,17 @@ public class TokenAnalysisPaths {
tokenPaths.forEach(p -> hasProgress = p.add(element) || hasProgress);
}
private boolean addAllDistinct(TokenAnalysisPaths other) {
boolean changes = false;
for(TokenAnalysisPath path : other.tokenPaths) {
if (!tokenPaths.contains(path)) {
changes = true;
tokenPaths.add(path);
}
}
return changes;
}
public TokenAnalysisPaths merge(TokenAnalysisPaths other) {
if (isEmpty) {
return other.clone();
@ -66,7 +75,7 @@ public class TokenAnalysisPaths {
return this.clone();
} else {
// set hasProgress if other has progress and progress is merged
if (this.tokenPaths.addAll(other.tokenPaths)) {
if (addAllDistinct(other)) {
this.hasProgress |= other.hasProgress;
}
return this;