forrestthewoods / lib_fts

single-file public domain libraries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does bestLetter match work?

gaurav5430 opened this issue · comments

Can you explain what purpose does bestLetter serve ?

After running/debugging the code, this is my understanding:

bestLetter is only relevant when we encounter the last matched letter again, in which case we check whether the current match has a better score than the last match (which is stored in bestLetterScore) .
After we "advance" to the next letter from the pattern, which means that we haven't encountered the last match again, but have found the next letter of the pattern, we forget about the previous last match, add its score, and start the same process for the current match.

for example if the pattern is "abc" and the string is "rabndom bc",

the algorithm finds "a" , it keeps "a" in memory as bestLetter.
going forward if the algorithm finds another "a" , then it would compare the score with the previous "a" and the better score will be the bestLetter. (This does not happen here.)

but as soon as we find "b" , which is the next char from the pattern, the algorithm would add the score of the best found "a" and forget all about it, and start focusing on "b".

it will remember "b" as bestLetter until it finds "c" or another "b" before "c" .

here it finds another "b" , it will compare the score for this newly found "b" with the previously found "b" and keep the one with higher score ( in this case the latter "b" as it has higher score as it occurs after a separator)

then it will find "c".

so, rematch keeps track of whether we have encountered the same character again (which we have in memory using bestLetter)

advance keeps track of when we have moved to the next character in the pattern, which means it is time to commit the scores for the previous letter.

I am yet to understand patternRepeat