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

Modified Trie to add prefix support to boolean contains(), plus unit … #918

Merged

Conversation

gentlesystems
Copy link

…tests.

Checklist

Description

There was already a function for a rapid search to see if the trie contains a word, but there was not an equivalent function that returned in the same (O) time to check for prefix matches. The existing function findWordsWithPrefix() takes significantly longer because it tracks down all terminating nodes in the subtrie, and is a waste if you're just looking to know if the prefix matches. A classic use case of this type of boolean request is to determine whether to return a document that matches a find-as-you-type prefix search on a pre-constructed trie of its tokenized contents.

I modified the contains() function to support this functionality for a project a couple years ago, and as I was updating that project it occurred to me that I ought to contribute this minor change upstream. I've built similar classes in other languages, but your swift version was so clear and elegant that I just used it instead of porting another or rewriting it.

Unit tests were simply adapted from the existing findWordsWithPrefix() tests.

@richard-ash
Copy link
Contributor

richard-ash commented Aug 29, 2020

Thanks for the PR!

@richard-ash richard-ash merged commit aeafa95 into kodecocodes:master Aug 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants