Skip to content
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

add optimized state traversal in Reader #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions fst.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ func (f *FST) Accept(addr int, b byte) int {
// IsMatchWithVal returns if this state is a matching state in this Automaton
// and also returns the final output value for this state
func (f *FST) IsMatchWithVal(addr int) (bool, uint64) {
s, err := f.decoder.stateAt(addr, nil)
return f.isMatchWithVal(addr, nil)
}

func (f *FST) isMatchWithVal(addr int, prealloc fstState) (bool, uint64) {
s, err := f.decoder.stateAt(addr, prealloc)
if err != nil {
return false, 0
}
Expand All @@ -170,7 +174,11 @@ func (f *FST) IsMatchWithVal(addr int) (bool, uint64) {
// AcceptWithVal returns the next state for this Automaton on input of byte b
// and also returns the output value for the transition
func (f *FST) AcceptWithVal(addr int, b byte) (int, uint64) {
s, err := f.decoder.stateAt(addr, nil)
return f.acceptWithVal(addr, b, nil)
}

func (f *FST) acceptWithVal(addr int, b byte, prealloc fstState) (int, uint64) {
s, err := f.decoder.stateAt(addr, prealloc)
if err != nil {
return noneAddr, 0
}
Expand Down Expand Up @@ -289,7 +297,10 @@ func (f *FST) GetMaxKey() ([]byte, error) {
return rv, nil
}

// A Reader is meant for a single threaded use
// A Reader can access states from the FST in a more efficient way. It is
// useful for repeated lookups.
//
// A Reader may only be used by a single thread at a time.
type Reader struct {
f *FST
prealloc fstStateV1
Expand All @@ -298,3 +309,21 @@ type Reader struct {
func (r *Reader) Get(input []byte) (uint64, bool, error) {
return r.f.get(input, &r.prealloc)
}

func (r *Reader) IsMatch(addr int) bool {
match, _ := r.IsMatchWithVal(addr)
return match
}

func (r *Reader) Accept(addr int, b byte) int {
next, _ := r.AcceptWithVal(addr, b)
return next
}

func (r *Reader) IsMatchWithVal(addr int) (bool, uint64) {
return r.f.isMatchWithVal(addr, &r.prealloc)
}

func (r *Reader) AcceptWithVal(addr int, b byte) (int, uint64) {
return r.f.acceptWithVal(addr, b, &r.prealloc)
}