improve hashCode

This commit is contained in:
Terence Parr 2012-08-04 14:38:57 -07:00
parent 1bec176eaa
commit 0d92c25056
1 changed files with 10 additions and 7 deletions

View File

@ -130,10 +130,11 @@ public class DFAState {
* DFA state.
*/
public Set<Integer> getAltSet() {
// TODO (sam): what to do when configs==null?
Set<Integer> alts = new HashSet<Integer>();
for (ATNConfig c : configs) {
alts.add(c.alt);
if ( configs!=null ) {
for (ATNConfig c : configs) {
alts.add(c.alt);
}
}
if ( alts.isEmpty() ) return null;
return alts;
@ -142,10 +143,12 @@ public class DFAState {
/** A decent hash for a DFA state is the sum of the ATN state/alt pairs. */
@Override
public int hashCode() {
// TODO (sam): what to do when configs==null?
int h = 0;
for (ATNConfig c : configs) {
h += c.alt;
int h = 7;
if ( configs!=null ) {
for (ATNConfig c : configs) {
h = h * 31 ^ c.alt;
h = h * 31 ^ c.state.stateNumber;
}
}
return h;
}