Merge remote branch 'sharwell/array-opt' with master

This commit is contained in:
Sam Harwell 2012-12-12 09:28:55 -06:00
commit e5e4402ea9
4 changed files with 35 additions and 26 deletions

View File

@ -997,8 +997,8 @@ public class ParserATNSimulator extends ATNSimulator {
return altToPred;
}
public List<DFAState.PredPrediction> getPredicatePredictions(BitSet ambigAlts,
SemanticContext[] altToPred)
public DFAState.PredPrediction[] getPredicatePredictions(BitSet ambigAlts,
SemanticContext[] altToPred)
{
List<DFAState.PredPrediction> pairs = new ArrayList<DFAState.PredPrediction>();
boolean containsPredicate = false;
@ -1019,7 +1019,7 @@ public class ParserATNSimulator extends ATNSimulator {
}
// System.out.println(Arrays.toString(altToPred)+"->"+pairs);
return pairs;
return pairs.toArray(new DFAState.PredPrediction[pairs.size()]);
}
public int getAltThatFinishedDecisionEntryRule(ATNConfigSet configs) {
@ -1039,7 +1039,7 @@ public class ParserATNSimulator extends ATNSimulator {
* then we stop at the first predicate that evaluates to true. This
* includes pairs with null predicates.
*/
public BitSet evalSemanticContext(List<DFAState.PredPrediction> predPredictions,
public BitSet evalSemanticContext(@NotNull DFAState.PredPrediction[] predPredictions,
ParserRuleContext outerContext,
boolean complete)
{

View File

@ -35,6 +35,7 @@ import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Utils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@ -114,13 +115,16 @@ public abstract class SemanticContext {
}
public static class AND extends SemanticContext {
@NotNull public Set<SemanticContext> opnds = new HashSet<SemanticContext>();
public AND() { }
@NotNull public final SemanticContext[] opnds;
public AND(@NotNull SemanticContext a, @NotNull SemanticContext b) {
if ( a instanceof AND ) opnds.addAll(((AND)a).opnds);
else opnds.add(a);
if ( b instanceof AND ) opnds.addAll(((AND)b).opnds);
else opnds.add(b);
Set<SemanticContext> operands = new HashSet<SemanticContext>();
if ( a instanceof AND ) operands.addAll(Arrays.asList(((AND)a).opnds));
else operands.add(a);
if ( b instanceof AND ) operands.addAll(Arrays.asList(((AND)b).opnds));
else operands.add(b);
opnds = operands.toArray(new SemanticContext[operands.size()]);
}
@Override
@ -128,12 +132,12 @@ public abstract class SemanticContext {
if ( this==obj ) return true;
if ( !(obj instanceof AND) ) return false;
AND other = (AND)obj;
return this.opnds.equals(other.opnds);
return Arrays.equals(this.opnds, other.opnds);
}
@Override
public int hashCode() {
return opnds.hashCode();
return Arrays.hashCode(opnds);
}
@Override
@ -146,18 +150,21 @@ public abstract class SemanticContext {
@Override
public String toString() {
return Utils.join(opnds.iterator(), "&&");
return Utils.join(Arrays.asList(opnds).iterator(), "&&");
}
}
public static class OR extends SemanticContext {
@NotNull public Set<SemanticContext> opnds = new HashSet<SemanticContext>();
public OR() { }
public OR(@NotNull SemanticContext a, @NotNull SemanticContext b) {
if ( a instanceof OR ) opnds.addAll(((OR)a).opnds);
else opnds.add(a);
if ( b instanceof OR ) opnds.addAll(((OR)b).opnds);
else opnds.add(b);
@NotNull public final SemanticContext[] opnds;
public OR(@NotNull SemanticContext a, @NotNull SemanticContext b) {
Set<SemanticContext> operands = new HashSet<SemanticContext>();
if ( a instanceof OR ) operands.addAll(Arrays.asList(((OR)a).opnds));
else operands.add(a);
if ( b instanceof OR ) operands.addAll(Arrays.asList(((OR)b).opnds));
else operands.add(b);
this.opnds = operands.toArray(new SemanticContext[operands.size()]);
}
@Override
@ -165,12 +172,12 @@ public abstract class SemanticContext {
if ( this==obj ) return true;
if ( !(obj instanceof OR) ) return false;
OR other = (OR)obj;
return this.opnds.equals(other.opnds);
return Arrays.equals(this.opnds, other.opnds);
}
@Override
public int hashCode() {
return opnds.hashCode() + 1; // differ from AND slightly
return Arrays.hashCode(opnds) + 1; // differ from AND slightly
}
@Override
@ -183,7 +190,7 @@ public abstract class SemanticContext {
@Override
public String toString() {
return Utils.join(opnds.iterator(), "||");
return Utils.join(Arrays.asList(opnds).iterator(), "||");
}
}

View File

@ -33,6 +33,7 @@ package org.antlr.v4.runtime.dfa;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.Arrays;
import java.util.Map;
/** A DFA walker that knows how to dump them to serialized strings. */
@ -84,7 +85,7 @@ public class DFASerializer {
String stateStr = "s"+n;
if ( s.isAcceptState ) {
if ( s.predicates!=null ) {
stateStr = ":s"+n+"=>"+s.predicates;
stateStr = ":s"+n+"=>"+Arrays.toString(s.predicates);
}
else {
stateStr = ":s"+n+"=>"+s.prediction;

View File

@ -36,6 +36,7 @@ import org.antlr.v4.runtime.atn.SemanticContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -107,7 +108,7 @@ public class DFAState {
* This list is computed by predicateDFAState() in ATN simulator.
*/
@Nullable
public List<PredPrediction> predicates;
public PredPrediction[] predicates;
/** Map a predicate to a predicted alternative */
public static class PredPrediction {
@ -190,7 +191,7 @@ public class DFAState {
if ( isAcceptState ) {
buf.append("=>");
if ( predicates!=null ) {
buf.append(predicates);
buf.append(Arrays.toString(predicates));
}
else {
buf.append(prediction);