diff --git a/runtime/Go/antlr/atn_config.go b/runtime/Go/antlr/atn_config.go index f49d7f4c46..5ba537f556 100644 --- a/runtime/Go/antlr/atn_config.go +++ b/runtime/Go/antlr/atn_config.go @@ -8,7 +8,7 @@ import ( "fmt" ) -type Comparable interface { +type comparable interface { equals(other interface{}) bool } @@ -18,9 +18,9 @@ type Comparable interface { // state. The semantic context is the tree of semantic predicates encountered // before reaching an ATN state. type ATNConfig interface { - Comparable + comparable - HashCode() int + hash() int GetState() ATNState GetAlt() int @@ -166,18 +166,18 @@ func (b *BaseATNConfig) equals(o interface{}) bool { return nums && alts && cons && sups && equal } -func (b *BaseATNConfig) HashCode() int { +func (b *BaseATNConfig) hash() int { var c int if b.context != nil { - c = b.context.HashCode() + c = b.context.hash() } - h := initHash(7) - h = update(h, b.state.GetStateNumber()) - h = update(h, b.alt) - h = update(h, c) - h = update(h, b.semanticContext.HashCode()) - return finish(h, 4) + h := murmurInit(7) + h = murmurUpdate(h, b.state.GetStateNumber()) + h = murmurUpdate(h, b.alt) + h = murmurUpdate(h, c) + h = murmurUpdate(h, b.semanticContext.hash()) + return murmurFinish(h, 4) } func (b *BaseATNConfig) String() string { @@ -243,21 +243,21 @@ func NewLexerATNConfig1(state ATNState, alt int, context PredictionContext) *Lex return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)} } -func (l *LexerATNConfig) HashCode() int { +func (l *LexerATNConfig) hash() int { var f int if l.passedThroughNonGreedyDecision { f = 1 } else { f = 0 } - h := initHash(7) - h = update(h, l.state.HashCode()) - h = update(h, l.alt) - h = update(h, l.context.HashCode()) - h = update(h, l.semanticContext.HashCode()) - h = update(h, f) - h = update(h, l.lexerActionExecutor.HashCode()) - h = finish(h, 6) + h := murmurInit(7) + h = murmurUpdate(h, l.state.hash()) + h = murmurUpdate(h, l.alt) + h = murmurUpdate(h, l.context.hash()) + h = murmurUpdate(h, l.semanticContext.hash()) + h = murmurUpdate(h, f) + h = murmurUpdate(h, l.lexerActionExecutor.hash()) + h = murmurFinish(h, 6) return h } diff --git a/runtime/Go/antlr/atn_config_set.go b/runtime/Go/antlr/atn_config_set.go index 80fa459e48..66bdf3fb84 100644 --- a/runtime/Go/antlr/atn_config_set.go +++ b/runtime/Go/antlr/atn_config_set.go @@ -7,7 +7,7 @@ package antlr import "fmt" type ATNConfigSet interface { - HashCode() int + hash() int Add(ATNConfig, *DoubleDict) bool AddAll([]ATNConfig) bool @@ -223,7 +223,7 @@ func (b *BaseATNConfigSet) Equals(other interface{}) bool { b.dipsIntoOuterContext == other2.dipsIntoOuterContext } -func (b *BaseATNConfigSet) HashCode() int { +func (b *BaseATNConfigSet) hash() int { if b.readOnly { if b.cachedHash == -1 { b.cachedHash = b.hashCodeConfigs() @@ -236,13 +236,13 @@ func (b *BaseATNConfigSet) HashCode() int { } func (b *BaseATNConfigSet) hashCodeConfigs() int { - h := initHash(1) + h := murmurInit(1) for _, c := range b.configs { if c != nil { - h = update(h, c.HashCode()) + h = murmurUpdate(h, c.hash()) } } - return finish(h, len(b.configs)) + return murmurFinish(h, len(b.configs)) } func (b *BaseATNConfigSet) Length() int { diff --git a/runtime/Go/antlr/atn_state.go b/runtime/Go/antlr/atn_state.go index 657cb979a0..2c5ed43a05 100644 --- a/runtime/Go/antlr/atn_state.go +++ b/runtime/Go/antlr/atn_state.go @@ -49,7 +49,7 @@ type ATNState interface { AddTransition(Transition, int) String() string - HashCode() int + hash() int } type BaseATNState struct { @@ -123,7 +123,7 @@ func (as *BaseATNState) SetNextTokenWithinRule(v *IntervalSet) { as.NextTokenWithinRule = v } -func (as *BaseATNState) HashCode() int { +func (as *BaseATNState) hash() int { return as.stateNumber } diff --git a/runtime/Go/antlr/dfa_state.go b/runtime/Go/antlr/dfa_state.go index 71067eecfb..1c40ddf289 100644 --- a/runtime/Go/antlr/dfa_state.go +++ b/runtime/Go/antlr/dfa_state.go @@ -144,23 +144,23 @@ func (d *DFAState) String() string { return fmt.Sprintf("%d:%s%s", fmt.Sprint(d.configs), s) } -func (d *DFAState) HashCode() int { - h := initHash(11) +func (d *DFAState) hash() int { + h := murmurInit(11) c := 1 if d.isAcceptState { if d.predicates != nil { for _, p := range d.predicates { - h = update(h, p.alt) - h = update(h, p.pred.HashCode()) + h = murmurUpdate(h, p.alt) + h = murmurUpdate(h, p.pred.hash()) c += 2 } } else { - h = update(h, d.prediction) + h = murmurUpdate(h, d.prediction) c += 1 } } - h = update(h, d.configs.HashCode()) - return finish(h, c) + h = murmurUpdate(h, d.configs.hash()) + return murmurFinish(h, c) } \ No newline at end of file diff --git a/runtime/Go/antlr/lexer_action.go b/runtime/Go/antlr/lexer_action.go index 8e32e9d397..0cb5f92f46 100644 --- a/runtime/Go/antlr/lexer_action.go +++ b/runtime/Go/antlr/lexer_action.go @@ -21,7 +21,7 @@ type LexerAction interface { getActionType() int getIsPositionDependent() bool execute(lexer Lexer) - HashCode() int + hash() int equals(other LexerAction) bool } @@ -51,7 +51,7 @@ func (b *BaseLexerAction) getIsPositionDependent() bool { return b.isPositionDependent } -func (b *BaseLexerAction) HashCode() int { +func (b *BaseLexerAction) hash() int { return b.actionType } @@ -104,11 +104,11 @@ func (l *LexerTypeAction) execute(lexer Lexer) { lexer.setType(l.thetype) } -func (l *LexerTypeAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.thetype) - return finish(h, 2) +func (l *LexerTypeAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.thetype) + return murmurFinish(h, 2) } func (l *LexerTypeAction) equals(other LexerAction) bool { @@ -148,11 +148,11 @@ func (l *LexerPushModeAction) execute(lexer Lexer) { lexer.pushMode(l.mode) } -func (l *LexerPushModeAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.mode) - return finish(h, 2) +func (l *LexerPushModeAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.mode) + return murmurFinish(h, 2) } func (l *LexerPushModeAction) equals(other LexerAction) bool { @@ -245,11 +245,11 @@ func (l *LexerModeAction) execute(lexer Lexer) { lexer.setMode(l.mode) } -func (l *LexerModeAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.mode) - return finish(h, 2) +func (l *LexerModeAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.mode) + return murmurFinish(h, 2) } func (l *LexerModeAction) equals(other LexerAction) bool { @@ -303,12 +303,12 @@ func (l *LexerCustomAction) execute(lexer Lexer) { lexer.Action(nil, l.ruleIndex, l.actionIndex) } -func (l *LexerCustomAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.ruleIndex) - h = update(h, l.actionIndex) - return finish(h, 3) +func (l *LexerCustomAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.ruleIndex) + h = murmurUpdate(h, l.actionIndex) + return murmurFinish(h, 3) } func (l *LexerCustomAction) equals(other LexerAction) bool { @@ -344,11 +344,11 @@ func (l *LexerChannelAction) execute(lexer Lexer) { lexer.setChannel(l.channel) } -func (l *LexerChannelAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.channel) - return finish(h, 2) +func (l *LexerChannelAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.channel) + return murmurFinish(h, 2) } func (l *LexerChannelAction) equals(other LexerAction) bool { @@ -412,12 +412,12 @@ func (l *LexerIndexedCustomAction) execute(lexer Lexer) { l.lexerAction.execute(lexer) } -func (l *LexerIndexedCustomAction) HashCode() int { - h := initHash(0) - h = update(h, l.actionType) - h = update(h, l.offset) - h = update(h, l.lexerAction.HashCode()) - return finish(h, 3) +func (l *LexerIndexedCustomAction) hash() int { + h := murmurInit(0) + h = murmurUpdate(h, l.actionType) + h = murmurUpdate(h, l.offset) + h = murmurUpdate(h, l.lexerAction.hash()) + return murmurFinish(h, 3) } func (l *LexerIndexedCustomAction) equals(other LexerAction) bool { diff --git a/runtime/Go/antlr/lexer_action_executor.go b/runtime/Go/antlr/lexer_action_executor.go index 62f32609c7..b1699fb08c 100644 --- a/runtime/Go/antlr/lexer_action_executor.go +++ b/runtime/Go/antlr/lexer_action_executor.go @@ -28,9 +28,9 @@ func NewLexerActionExecutor(lexerActions []LexerAction) *LexerActionExecutor { // Caches the result of {@link //hashCode} since the hash code is an element // of the performance-critical {@link LexerATNConfig//hashCode} operation. - l.cachedHash = initHash(57) + l.cachedHash = murmurInit(57) for _, a := range lexerActions { - l.cachedHash = update(l.cachedHash, a.HashCode()) + l.cachedHash = murmurUpdate(l.cachedHash, a.hash()) } return l @@ -151,7 +151,7 @@ func (l *LexerActionExecutor) execute(lexer Lexer, input CharStream, startIndex } } -func (l *LexerActionExecutor) HashCode() int { +func (l *LexerActionExecutor) hash() int { if l == nil { return 61 } diff --git a/runtime/Go/antlr/lexer_atn_simulator.go b/runtime/Go/antlr/lexer_atn_simulator.go index 5f7e9ecb46..48f2564886 100644 --- a/runtime/Go/antlr/lexer_atn_simulator.go +++ b/runtime/Go/antlr/lexer_atn_simulator.go @@ -582,7 +582,7 @@ func (l *LexerATNSimulator) addDFAState(configs ATNConfigSet) *DFAState { proposed.lexerActionExecutor = firstConfigWithRuleStopState.(*LexerATNConfig).lexerActionExecutor proposed.setPrediction(l.atn.ruleToTokenType[firstConfigWithRuleStopState.GetState().GetRuleIndex()]) } - hash := proposed.HashCode() + hash := proposed.hash() dfa := l.decisionToDFA[l.mode] existing := dfa.GetStates()[hash] if existing != nil { diff --git a/runtime/Go/antlr/parser_atn_simulator.go b/runtime/Go/antlr/parser_atn_simulator.go index d02e040f7e..f581c307af 100644 --- a/runtime/Go/antlr/parser_atn_simulator.go +++ b/runtime/Go/antlr/parser_atn_simulator.go @@ -1420,7 +1420,7 @@ func (p *ParserATNSimulator) addDFAState(dfa *DFA, D *DFAState) *DFAState { if D == ATNSimulatorError { return D } - hash := D.HashCode() + hash := D.hash() var existing, ok = dfa.GetStates()[hash] if ok { return existing diff --git a/runtime/Go/antlr/prediction_context.go b/runtime/Go/antlr/prediction_context.go index b1efe2f2c2..2991566eae 100644 --- a/runtime/Go/antlr/prediction_context.go +++ b/runtime/Go/antlr/prediction_context.go @@ -26,7 +26,7 @@ var ( ) type PredictionContext interface { - HashCode() int + hash() int GetParent(int) PredictionContext getReturnState(int) int equals(PredictionContext) bool @@ -52,15 +52,15 @@ func (b *BasePredictionContext) isEmpty() bool { } func calculateHash(parent PredictionContext, returnState int) int { - h := initHash(1) - h = update(h, parent.HashCode()) - h = update(h, returnState) - return finish(h, 2) + h := murmurInit(1) + h = murmurUpdate(h, parent.hash()) + h = murmurUpdate(h, returnState) + return murmurFinish(h, 2) } func calculateEmptyHash() int { - h := initHash(1) - return finish(h, 0) + h := murmurInit(1) + return murmurFinish(h, 0) } // Used to cache {@link BasePredictionContext} objects. Its used for the shared @@ -159,7 +159,7 @@ func (b *BaseSingletonPredictionContext) equals(other PredictionContext) bool { return true } else if _, ok := other.(*BaseSingletonPredictionContext); !ok { return false - } else if b.HashCode() != other.HashCode() { + } else if b.hash() != other.hash() { return false // can't be same if hash is different } @@ -174,16 +174,16 @@ func (b *BaseSingletonPredictionContext) equals(other PredictionContext) bool { return b.parentCtx.equals(otherP.parentCtx) } -func (b *BaseSingletonPredictionContext) HashCode() int { - h := initHash(1) +func (b *BaseSingletonPredictionContext) hash() int { + h := murmurInit(1) if b.parentCtx == nil { - return finish(h, 0) + return murmurFinish(h, 0) } - h = update(h, b.parentCtx.HashCode()) - h = update(h, b.returnState) - return finish(h, 2) + h = murmurUpdate(h, b.parentCtx.hash()) + h = murmurUpdate(h, b.returnState) + return murmurFinish(h, 2) } func (b *BaseSingletonPredictionContext) String() string { @@ -296,7 +296,7 @@ func (a *ArrayPredictionContext) getReturnState(index int) int { func (a *ArrayPredictionContext) equals(other PredictionContext) bool { if _, ok := other.(*ArrayPredictionContext); !ok { return false - } else if a.cachedHash != other.HashCode() { + } else if a.cachedHash != other.hash() { return false // can't be same if hash is different } else { otherP := other.(*ArrayPredictionContext) @@ -304,18 +304,18 @@ func (a *ArrayPredictionContext) equals(other PredictionContext) bool { } } -func (a *ArrayPredictionContext) HashCode() int { - h := initHash(1) +func (a *ArrayPredictionContext) hash() int { + h := murmurInit(1) for _, p := range a.parents { - h = update(h, p.HashCode()) + h = murmurUpdate(h, p.hash()) } for _, r := range a.returnStates { - h = update(h, r) + h = murmurUpdate(h, r) } - return finish(h, 2 * len(a.parents)) + return murmurFinish(h, 2 * len(a.parents)) } func (a *ArrayPredictionContext) String() string { @@ -428,11 +428,11 @@ func merge(a, b PredictionContext, rootIsWildcard bool, mergeCache *DoubleDict) // / func mergeSingletons(a, b *BaseSingletonPredictionContext, rootIsWildcard bool, mergeCache *DoubleDict) PredictionContext { if mergeCache != nil { - previous := mergeCache.Get(a.HashCode(), b.HashCode()) + previous := mergeCache.Get(a.hash(), b.hash()) if previous != nil { return previous.(PredictionContext) } - previous = mergeCache.Get(b.HashCode(), a.HashCode()) + previous = mergeCache.Get(b.hash(), a.hash()) if previous != nil { return previous.(PredictionContext) } @@ -441,7 +441,7 @@ func mergeSingletons(a, b *BaseSingletonPredictionContext, rootIsWildcard bool, rootMerge := mergeRoot(a, b, rootIsWildcard) if rootMerge != nil { if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), rootMerge) + mergeCache.set(a.hash(), b.hash(), rootMerge) } return rootMerge } @@ -461,7 +461,7 @@ func mergeSingletons(a, b *BaseSingletonPredictionContext, rootIsWildcard bool, // Newjoined parent so create Newsingleton pointing to it, a' spc := SingletonBasePredictionContextCreate(parent, a.returnState) if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), spc) + mergeCache.set(a.hash(), b.hash(), spc) } return spc } @@ -483,7 +483,7 @@ func mergeSingletons(a, b *BaseSingletonPredictionContext, rootIsWildcard bool, parents := []PredictionContext{singleParent, singleParent} apc := NewArrayPredictionContext(parents, payloads) if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), apc) + mergeCache.set(a.hash(), b.hash(), apc) } return apc } @@ -499,7 +499,7 @@ func mergeSingletons(a, b *BaseSingletonPredictionContext, rootIsWildcard bool, } apc := NewArrayPredictionContext(parents, payloads) if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), apc) + mergeCache.set(a.hash(), b.hash(), apc) } return apc } @@ -588,11 +588,11 @@ func mergeRoot(a, b SingletonPredictionContext, rootIsWildcard bool) PredictionC // / func mergeArrays(a, b *ArrayPredictionContext, rootIsWildcard bool, mergeCache *DoubleDict) PredictionContext { if mergeCache != nil { - previous := mergeCache.Get(a.HashCode(), b.HashCode()) + previous := mergeCache.Get(a.hash(), b.hash()) if previous != nil { return previous.(PredictionContext) } - previous = mergeCache.Get(b.HashCode(), a.HashCode()) + previous = mergeCache.Get(b.hash(), a.hash()) if previous != nil { return previous.(PredictionContext) } @@ -656,7 +656,7 @@ func mergeArrays(a, b *ArrayPredictionContext, rootIsWildcard bool, mergeCache * if k == 1 { // for just one merged element, return singleton top pc := SingletonBasePredictionContextCreate(mergedParents[0], mergedReturnStates[0]) if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), pc) + mergeCache.set(a.hash(), b.hash(), pc) } return pc } @@ -670,20 +670,20 @@ func mergeArrays(a, b *ArrayPredictionContext, rootIsWildcard bool, mergeCache * // TODO: track whether this is possible above during merge sort for speed if M == a { if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), a) + mergeCache.set(a.hash(), b.hash(), a) } return a } if M == b { if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), b) + mergeCache.set(a.hash(), b.hash(), b) } return b } combineCommonParents(mergedParents) if mergeCache != nil { - mergeCache.set(a.HashCode(), b.HashCode(), M) + mergeCache.set(a.hash(), b.hash(), M) } return M } diff --git a/runtime/Go/antlr/prediction_mode.go b/runtime/Go/antlr/prediction_mode.go index c2e55afc7d..96c9c30f22 100644 --- a/runtime/Go/antlr/prediction_mode.go +++ b/runtime/Go/antlr/prediction_mode.go @@ -4,12 +4,6 @@ package antlr -import ( - "strconv" - "strings" -) - -// // This enumeration defines the prediction modes available in ANTLR 4 along with // utility methods for analyzing configuration sets for conflicts and/or // ambiguities. @@ -491,25 +485,22 @@ func PredictionModeGetAlts(altsets []*BitSet) *BitSet { // // func PredictionModegetConflictingAltSubsets(configs ATNConfigSet) []*BitSet { - configToAlts := make(map[string]*BitSet) + configToAlts := make(map[int]*BitSet) for _, c := range configs.GetItems() { - key := "key_" + strconv.Itoa(c.GetState().GetStateNumber()) + "/" + c.GetContext().String() - alts := configToAlts[key] - if alts == nil { + key := 31 * c.GetState().GetStateNumber() + c.GetContext().hash() + + alts, ok := configToAlts[key] + if !ok { alts = NewBitSet() configToAlts[key] = alts } alts.add(c.GetAlt()) } - values := make([]*BitSet, 0) - - for k := range configToAlts { - if strings.Index(k, "key_") != 0 { - continue - } - values = append(values, configToAlts[k]) + values := make([]*BitSet, 0, 10) + for _, v := range configToAlts { + values = append(values, v) } return values } diff --git a/runtime/Go/antlr/semantic_context.go b/runtime/Go/antlr/semantic_context.go index 44c99751ea..54f17476a4 100644 --- a/runtime/Go/antlr/semantic_context.go +++ b/runtime/Go/antlr/semantic_context.go @@ -18,12 +18,12 @@ import ( // type SemanticContext interface { - Comparable + comparable evaluate(parser Recognizer, outerContext RuleContext) bool evalPrecedence(parser Recognizer, outerContext RuleContext) SemanticContext - HashCode() int + hash() int String() string } @@ -107,7 +107,7 @@ func (p *Predicate) equals(other interface{}) bool { } } -func (p *Predicate) HashCode() int { +func (p *Predicate) hash() int { return p.ruleIndex*43 + p.predIndex*47 } @@ -153,7 +153,7 @@ func (p *PrecedencePredicate) equals(other interface{}) bool { } } -func (p *PrecedencePredicate) HashCode() int { +func (p *PrecedencePredicate) hash() int { return p.precedence * 51 } @@ -293,20 +293,20 @@ func (a *AND) evalPrecedence(parser Recognizer, outerContext RuleContext) Semant return result } -func (a *AND) HashCode() int { - h := initHash(37) // Init with a value different from OR +func (a *AND) hash() int { + h := murmurInit(37) // Init with a value different from OR for _, op := range a.opnds { - h = update(h, op.HashCode()) + h = murmurUpdate(h, op.hash()) } - return finish(h, len(a.opnds)) + return murmurFinish(h, len(a.opnds)) } -func (a *OR) HashCode() int { - h := initHash(41) // Init with a value different from AND +func (a *OR) hash() int { + h := murmurInit(41) // Init with a value different from AND for _, op := range a.opnds { - h = update(h, op.HashCode()) + h = murmurUpdate(h, op.hash()) } - return finish(h, len(a.opnds)) + return murmurFinish(h, len(a.opnds)) } func (a *AND) String() string { diff --git a/runtime/Go/antlr/utils.go b/runtime/Go/antlr/utils.go index ea072751e6..706a679746 100644 --- a/runtime/Go/antlr/utils.go +++ b/runtime/Go/antlr/utils.go @@ -8,7 +8,6 @@ import ( "bytes" "errors" "fmt" - "hash/fnv" "sort" "strconv" "strings" @@ -79,8 +78,8 @@ func NewSet( func standardEqualsFunction(a interface{}, b interface{}) bool { - ac, oka := a.(Comparable) - bc, okb := b.(Comparable) + ac, oka := a.(comparable) + bc, okb := b.(comparable) if !oka || !okb { panic("Not Comparable") @@ -90,15 +89,15 @@ func standardEqualsFunction(a interface{}, b interface{}) bool { } func standardHashFunction(a interface{}) int { - if h, ok := a.(HashCoder); ok { - return h.HashCode() + if h, ok := a.(hasher); ok { + return h.hash() } panic("Not Hasher") } -type HashCoder interface { - HashCode() int +type hasher interface { + hash() int } func (s *Set) length() int { @@ -362,11 +361,11 @@ const ( n1_32 = 0xE6546B64 ) -func initHash(seed int) int { +func murmurInit(seed int) int { return seed } -func update(h1 int, k1 int) int { +func murmurUpdate(h1 int, k1 int) int { k1 *= c1_32 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15) k1 *= c2_32 @@ -377,7 +376,7 @@ func update(h1 int, k1 int) int { return h1 } -func finish(h1 int, numberOfWords int) int { +func murmurFinish(h1 int, numberOfWords int) int { h1 ^= (numberOfWords * 4) h1 ^= h1 >> 16 h1 *= 0x85ebca6b