Used 'unique append' to ensure that a transition can only contain a given state once

master
Aadhavan Srinivasan 2 months ago
parent 50e86b5db4
commit bf3060b672

@ -147,8 +147,13 @@ func thompson(re string) *State {
s3.transitions = make(map[int][]*State) s3.transitions = make(map[int][]*State)
s3.output = append(s3.output, s1.output...) s3.output = append(s3.output, s1.output...)
s3.output = append(s3.output, s2.output...) s3.output = append(s3.output, s2.output...)
s3.transitions[s1.content] = append(s3.transitions[s1.content], s1) // Unique append is used here (and elsewhere) to ensure that,
s3.transitions[s2.content] = append(s3.transitions[s2.content], s2) // for any given transition, a state can only be mentioned once.
// For example, given the transition 'a', the state 's1' can only be mentioned once.
// This would lead to multiple instances of the same set of match indices, since both
// 's1' states would be considered to match.
s3.transitions[s1.content] = unique_append(s3.transitions[s1.content], s1)
s3.transitions[s2.content] = unique_append(s3.transitions[s2.content], s2)
s3.content = EPSILON s3.content = EPSILON
s3.isEmpty = true s3.isEmpty = true

@ -55,7 +55,7 @@ func verifyLastStates(start []*State) {
func concatenate(s1 *State, s2 *State) *State { func concatenate(s1 *State, s2 *State) *State {
for i := range s1.output { for i := range s1.output {
s1.output[i].transitions[s2.content] = append(s1.output[i].transitions[s2.content], s2) s1.output[i].transitions[s2.content] = unique_append(s1.output[i].transitions[s2.content], s2)
} }
s1.output = s2.output s1.output = s2.output
return s1 return s1
@ -69,8 +69,8 @@ func kleene(s1 State) *State {
toReturn.isKleene = true toReturn.isKleene = true
toReturn.output = append(toReturn.output, toReturn) toReturn.output = append(toReturn.output, toReturn)
for i := range s1.output { for i := range s1.output {
s1.output[i].transitions[toReturn.content] = append(s1.output[i].transitions[toReturn.content], toReturn) s1.output[i].transitions[toReturn.content] = unique_append(s1.output[i].transitions[toReturn.content], toReturn)
} }
toReturn.transitions[s1.content] = append(toReturn.transitions[s1.content], &s1) toReturn.transitions[s1.content] = unique_append(toReturn.transitions[s1.content], &s1)
return toReturn return toReturn
} }

Loading…
Cancel
Save