@@ -7,18 +7,17 @@ package antlr
7
7
import "fmt"
8
8
9
9
type ATNConfigSet interface {
10
- hash () int
10
+ Hash () int
11
+ Equals (o Collectable [ATNConfig ]) bool
11
12
Add (ATNConfig , * DoubleDict ) bool
12
13
AddAll ([]ATNConfig ) bool
13
14
14
- GetStates () Set
15
+ GetStates () * JStore [ ATNState , Comparator [ ATNState ]]
15
16
GetPredicates () []SemanticContext
16
17
GetItems () []ATNConfig
17
18
18
19
OptimizeConfigs (interpreter * BaseATNSimulator )
19
20
20
- Equals (other interface {}) bool
21
-
22
21
Length () int
23
22
IsEmpty () bool
24
23
Contains (ATNConfig ) bool
@@ -57,7 +56,7 @@ type BaseATNConfigSet struct {
57
56
// effectively doubles the number of objects associated with ATNConfigs. All
58
57
// keys are hashed by (s, i, _, pi), not including the context. Wiped out when
59
58
// read-only because a set becomes a DFA state.
60
- configLookup Set
59
+ configLookup * JStore [ ATNConfig , Comparator [ ATNConfig ]]
61
60
62
61
// configs is the added elements.
63
62
configs []ATNConfig
@@ -83,7 +82,7 @@ type BaseATNConfigSet struct {
83
82
84
83
// readOnly is whether it is read-only. Do not
85
84
// allow any code to manipulate the set if true because DFA states will point at
86
- // sets and those must not change. It not protect other fields; conflictingAlts
85
+ // sets and those must not change. It not, protect other fields; conflictingAlts
87
86
// in particular, which is assigned after readOnly.
88
87
readOnly bool
89
88
@@ -104,7 +103,7 @@ func (b *BaseATNConfigSet) Alts() *BitSet {
104
103
func NewBaseATNConfigSet (fullCtx bool ) * BaseATNConfigSet {
105
104
return & BaseATNConfigSet {
106
105
cachedHash : - 1 ,
107
- configLookup : newArray2DHashSetWithCap ( hashATNConfig , equalATNConfigs , 16 , 2 ),
106
+ configLookup : NewJStore [ ATNConfig , Comparator [ ATNConfig ]]( & ATNConfigComparator [ ATNConfig ]{} ),
108
107
fullCtx : fullCtx ,
109
108
}
110
109
}
@@ -126,9 +125,11 @@ func (b *BaseATNConfigSet) Add(config ATNConfig, mergeCache *DoubleDict) bool {
126
125
b .dipsIntoOuterContext = true
127
126
}
128
127
129
- existing := b .configLookup .Add (config ).( ATNConfig )
128
+ existing , present := b .configLookup .Put (config )
130
129
131
- if existing == config {
130
+ // The config was not already in the set
131
+ //
132
+ if ! present {
132
133
b .cachedHash = - 1
133
134
b .configs = append (b .configs , config ) // Track order here
134
135
return true
@@ -154,11 +155,14 @@ func (b *BaseATNConfigSet) Add(config ATNConfig, mergeCache *DoubleDict) bool {
154
155
return true
155
156
}
156
157
157
- func (b * BaseATNConfigSet ) GetStates () Set {
158
- states := newArray2DHashSet (nil , nil )
158
+ func (b * BaseATNConfigSet ) GetStates () * JStore [ATNState , Comparator [ATNState ]] {
159
+
160
+ // states uses the standard comparator provided by the ATNState instance
161
+ //
162
+ states := NewJStore [ATNState , Comparator [ATNState ]](& ObjEqComparator [ATNState ]{})
159
163
160
164
for i := 0 ; i < len (b .configs ); i ++ {
161
- states .Add (b .configs [i ].GetState ())
165
+ states .Put (b .configs [i ].GetState ())
162
166
}
163
167
164
168
return states
@@ -227,7 +231,7 @@ func (b *BaseATNConfigSet) Compare(bs *BaseATNConfigSet) bool {
227
231
for _ , c := range b .configs {
228
232
found := false
229
233
for _ , c2 := range bs .configs {
230
- if c .equals (c2 ) {
234
+ if c .Equals (c2 ) {
231
235
found = true
232
236
break
233
237
}
@@ -241,7 +245,7 @@ func (b *BaseATNConfigSet) Compare(bs *BaseATNConfigSet) bool {
241
245
return true
242
246
}
243
247
244
- func (b * BaseATNConfigSet ) Equals (other interface {} ) bool {
248
+ func (b * BaseATNConfigSet ) Equals (other Collectable [ ATNConfig ] ) bool {
245
249
if b == other {
246
250
return true
247
251
} else if _ , ok := other .(* BaseATNConfigSet ); ! ok {
@@ -259,7 +263,7 @@ func (b *BaseATNConfigSet) Equals(other interface{}) bool {
259
263
b .Compare (other2 )
260
264
}
261
265
262
- func (b * BaseATNConfigSet ) hash () int {
266
+ func (b * BaseATNConfigSet ) Hash () int {
263
267
if b .readOnly {
264
268
if b .cachedHash == - 1 {
265
269
b .cachedHash = b .hashCodeConfigs ()
@@ -274,7 +278,7 @@ func (b *BaseATNConfigSet) hash() int {
274
278
func (b * BaseATNConfigSet ) hashCodeConfigs () int {
275
279
h := 1
276
280
for _ , config := range b .configs {
277
- h = 31 * h + config .hash ()
281
+ h = 31 * h + config .Hash ()
278
282
}
279
283
return h
280
284
}
@@ -310,7 +314,7 @@ func (b *BaseATNConfigSet) Clear() {
310
314
311
315
b .configs = make ([]ATNConfig , 0 )
312
316
b .cachedHash = - 1
313
- b .configLookup = newArray2DHashSet ( nil , equalATNConfigs )
317
+ b .configLookup = NewJStore [ ATNConfig , Comparator [ ATNConfig ]]( & BaseATNConfigComparator [ ATNConfig ]{} )
314
318
}
315
319
316
320
func (b * BaseATNConfigSet ) FullContext () bool {
@@ -392,7 +396,8 @@ type OrderedATNConfigSet struct {
392
396
func NewOrderedATNConfigSet () * OrderedATNConfigSet {
393
397
b := NewBaseATNConfigSet (false )
394
398
395
- b .configLookup = newArray2DHashSet (nil , nil )
399
+ // This set uses the standard Hash() and Equals() from ATNConfig
400
+ b .configLookup = NewJStore [ATNConfig , Comparator [ATNConfig ]](& ObjEqComparator [ATNConfig ]{})
396
401
397
402
return & OrderedATNConfigSet {BaseATNConfigSet : b }
398
403
}
@@ -402,7 +407,7 @@ func hashATNConfig(i interface{}) int {
402
407
hash := 7
403
408
hash = 31 * hash + o .GetState ().GetStateNumber ()
404
409
hash = 31 * hash + o .GetAlt ()
405
- hash = 31 * hash + o .GetSemanticContext ().hash ()
410
+ hash = 31 * hash + o .GetSemanticContext ().Hash ()
406
411
return hash
407
412
}
408
413
@@ -430,5 +435,5 @@ func equalATNConfigs(a, b interface{}) bool {
430
435
return false
431
436
}
432
437
433
- return ai .GetSemanticContext ().equals (bi .GetSemanticContext ())
438
+ return ai .GetSemanticContext ().Equals (bi .GetSemanticContext ())
434
439
}
0 commit comments