Use HashMap instead of LinkedHashMap for DFA.states

This commit is contained in:
Sam Harwell 2013-04-22 16:41:27 -05:00
parent 7ebcc41b7b
commit 845bf53a51
2 changed files with 32 additions and 15 deletions

View File

@ -38,8 +38,10 @@ import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -49,7 +51,7 @@ public class DFA {
* ({@link Set} only allows you to see if it's there).
*/
@NotNull
public final Map<DFAState, DFAState> states = new LinkedHashMap<DFAState, DFAState>();
public final Map<DFAState, DFAState> states = new HashMap<DFAState, DFAState>();
@Nullable
public DFAState s0;
@ -73,6 +75,22 @@ public class DFA {
this.decision = decision;
}
/**
* Return a list of all states in this DFA, ordered by state number.
*/
@NotNull
public List<DFAState> getStates() {
List<DFAState> result = new ArrayList<DFAState>(states.keySet());
Collections.sort(result, new Comparator<DFAState>() {
@Override
public int compare(DFAState o1, DFAState o2) {
return o1.stateNumber - o2.stateNumber;
}
});
return result;
}
public List<Set<ATNState>> getATNStatesAlongPath(ParserATNSimulator atn,
List<DFAState> dfaStates,
TokenStream input, int start, int stop)

View File

@ -34,7 +34,7 @@ import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
/** A DFA walker that knows how to dump them to serialized strings. */
public class DFASerializer {
@ -52,9 +52,8 @@ public class DFASerializer {
public String toString() {
if ( dfa.s0==null ) return null;
StringBuilder buf = new StringBuilder();
Map<DFAState,DFAState> states = dfa.states;
if ( states!=null ) {
for (DFAState s : states.values()) {
List<DFAState> states = dfa.getStates();
for (DFAState s : states) {
int n = 0;
if ( s.edges!=null ) n = s.edges.length;
for (int i=0; i<n; i++) {
@ -66,7 +65,7 @@ public class DFASerializer {
}
}
}
}
String output = buf.toString();
//return Utils.sortLinesInString(output);
return output;