-
Notifications
You must be signed in to change notification settings - Fork 26
kaizen: Precomputed epsilon closures #482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sayrer
wants to merge
13
commits into
timbray:main
Choose a base branch
from
sayrer:pr/optimize-epsilon-closure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9576e08
Changes made:**
sayrer 6450d21
Merge origin/main into pr/optimize-epsilon-closure
sayrer 2252458
Merge branch 'main' into pr/optimize-epsilon-closure
sayrer d9687e1
Merge branch 'main' into pr/optimize-epsilon-closure
sayrer 8880e9b
Remove test path.
sayrer 0347d00
Merge branch 'pr/optimize-epsilon-closure' of https://github.com/sayr…
sayrer 68ab383
Consolidate precomputeEpsilonClosures into NFA creation functions
sayrer b82a6bf
Move new functions to epsi_closure.go, delete old ones.
sayrer 1615df3
Use the name computeClosureForNfa.
sayrer fc35cb4
Remove redundant 'compute' prefixes.
sayrer db66150
Rename precomputeEpsilonClosure to epsilonClosure.
sayrer 213360f
Adjust epsilonClosure.
sayrer bf3290a
Add some tests for epsilon closures.
sayrer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,62 @@ | ||
| package quamina | ||
|
|
||
| type epsilonClosure struct { | ||
| closures map[*faState][]*faState | ||
| // epsilonClosure walks the automaton starting from the given table | ||
| // and precomputes the epsilon closure for every reachable faState. | ||
| func epsilonClosure(table *smallTable) { | ||
| closureForNfa(table, make(map[*smallTable]bool)) | ||
| } | ||
|
|
||
| func newEpsilonClosure() *epsilonClosure { | ||
| return &epsilonClosure{make(map[*faState][]*faState)} | ||
| } | ||
| func closureForNfa(table *smallTable, visited map[*smallTable]bool) { | ||
| if visited[table] { | ||
| return | ||
| } | ||
| visited[table] = true | ||
|
|
||
| func (ec *epsilonClosure) getClosure(state *faState) []*faState { | ||
| var closure []*faState | ||
| var ok bool | ||
| if ec.closures != nil { | ||
| closure, ok = ec.closures[state] | ||
| if ok { | ||
| return closure | ||
| // Process each faState reachable via byte transitions | ||
| for _, state := range table.steps { | ||
| if state != nil { | ||
| closureForState(state) | ||
| closureForNfa(state.table, visited) | ||
| } | ||
| } | ||
| // Process each faState reachable via epsilon transitions | ||
| for _, eps := range table.epsilons { | ||
| closureForState(eps) | ||
| closureForNfa(eps.table, visited) | ||
| } | ||
| } | ||
|
|
||
| func closureForState(state *faState) { | ||
| if state.epsilonClosure != nil { | ||
| return // already computed | ||
| } | ||
|
|
||
| // not already known | ||
| if len(state.table.epsilons) == 0 { | ||
| justMe := []*faState{state} | ||
| if ec.closures != nil { | ||
| ec.closures[state] = justMe | ||
| } | ||
| return justMe | ||
| state.epsilonClosure = []*faState{state} | ||
| return | ||
| } | ||
|
|
||
| var closureStates = make(map[*faState]bool) | ||
| closureSet := make(map[*faState]bool) | ||
| if !state.table.isEpsilonOnly() { | ||
| closureStates[state] = true | ||
| closureSet[state] = true | ||
| } | ||
| traverseEpsilons(state, state.table.epsilons, closureStates) | ||
| for s := range closureStates { | ||
| traverseEpsilons(state, state.table.epsilons, closureSet) | ||
|
|
||
| closure := make([]*faState, 0, len(closureSet)) | ||
| for s := range closureSet { | ||
| closure = append(closure, s) | ||
| } | ||
| if ec.closures != nil { | ||
| ec.closures[state] = closure | ||
| } | ||
| return closure | ||
| state.epsilonClosure = closure | ||
| } | ||
|
|
||
| func traverseEpsilons(start *faState, epsilons []*faState, closureStates map[*faState]bool) { | ||
| func traverseEpsilons(start *faState, epsilons []*faState, closureSet map[*faState]bool) { | ||
| for _, eps := range epsilons { | ||
| if eps == start || closureStates[eps] { | ||
| if eps == start || closureSet[eps] { | ||
| continue | ||
| } | ||
| if !eps.table.isEpsilonOnly() { | ||
| closureStates[eps] = true | ||
| closureSet[eps] = true | ||
| } | ||
| traverseEpsilons(start, eps.table.epsilons, closureStates) | ||
| traverseEpsilons(start, eps.table.epsilons, closureSet) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a double-take here. I think these names are what it's supposed to be.