Refactor hash methods

This commit is contained in:
Peter Boyer 2017-03-12 14:53:58 -04:00
parent d8998ec00d
commit db447513e5
11 changed files with 121 additions and 122 deletions

View File

@ -20,7 +20,7 @@ type Comparable interface {
type ATNConfig interface {
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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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

View File

@ -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
}

View File

@ -23,7 +23,7 @@ type SemanticContext interface {
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 {

View File

@ -8,7 +8,6 @@ import (
"bytes"
"errors"
"fmt"
"hash/fnv"
"sort"
"strconv"
"strings"
@ -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