Renamed variable to avoid conflicting with type name

master
Aadhavan Srinivasan 3 days ago
parent 198a2c12a7
commit 429d286439

@ -51,54 +51,54 @@ func cloneState(start *State) *State {
// Helper function for clone. The map is used to keep track of which states have // Helper function for clone. The map is used to keep track of which states have
// already been copied, and which ones haven't. // already been copied, and which ones haven't.
// This function was created using output from Llama3.1:405B. // This function was created using output from Llama3.1:405B.
func cloneStateHelper(state *State, cloneMap map[*State]*State) *State { func cloneStateHelper(stateToClone *State, cloneMap map[*State]*State) *State {
// Base case - if the clone exists in our map, return it. // Base case - if the clone exists in our map, return it.
if clone, exists := cloneMap[state]; exists { if clone, exists := cloneMap[stateToClone]; exists {
return clone return clone
} }
if state == nil { if stateToClone == nil {
return nil return nil
} }
// Recursive case - if the clone doesn't exist, create it, add it to the map, // Recursive case - if the clone doesn't exist, create it, add it to the map,
// and recursively call for each of the transition states. // and recursively call for each of the transition states.
clone := &State{ clone := &State{
content: append([]int{}, state.content...), content: append([]int{}, stateToClone.content...),
isEmpty: state.isEmpty, isEmpty: stateToClone.isEmpty,
isLast: state.isLast, isLast: stateToClone.isLast,
output: make([]*State, len(state.output)), output: make([]*State, len(stateToClone.output)),
transitions: make(map[int][]*State), transitions: make(map[int][]*State),
isKleene: state.isKleene, isKleene: stateToClone.isKleene,
assert: state.assert, assert: stateToClone.assert,
zeroMatchFound: state.zeroMatchFound, zeroMatchFound: stateToClone.zeroMatchFound,
allChars: state.allChars, allChars: stateToClone.allChars,
except: append([]rune{}, state.except...), except: append([]rune{}, stateToClone.except...),
lookaroundRegex: state.lookaroundRegex, lookaroundRegex: stateToClone.lookaroundRegex,
groupEnd: state.groupEnd, groupEnd: stateToClone.groupEnd,
groupBegin: state.groupBegin, groupBegin: stateToClone.groupBegin,
groupNum: state.groupNum, groupNum: stateToClone.groupNum,
} }
cloneMap[state] = clone cloneMap[stateToClone] = clone
for i, s := range state.output { for i, s := range stateToClone.output {
if s == state { if s == stateToClone {
clone.output[i] = clone clone.output[i] = clone
} else { } else {
clone.output[i] = cloneStateHelper(s, cloneMap) clone.output[i] = cloneStateHelper(s, cloneMap)
} }
} }
for k, v := range state.transitions { for k, v := range stateToClone.transitions {
clone.transitions[k] = make([]*State, len(v)) clone.transitions[k] = make([]*State, len(v))
for i, s := range v { for i, s := range v {
if s == state { if s == stateToClone {
clone.transitions[k][i] = clone clone.transitions[k][i] = clone
} else { } else {
clone.transitions[k][i] = cloneStateHelper(s, cloneMap) clone.transitions[k][i] = cloneStateHelper(s, cloneMap)
} }
} }
} }
if state.lookaroundNFA == state { if stateToClone.lookaroundNFA == stateToClone {
clone.lookaroundNFA = clone clone.lookaroundNFA = clone
} }
clone.lookaroundNFA = cloneStateHelper(state.lookaroundNFA, cloneMap) clone.lookaroundNFA = cloneStateHelper(stateToClone.lookaroundNFA, cloneMap)
return clone return clone
} }

Loading…
Cancel
Save