Modified Trie to add prefix support to boolean contains(), plus unit … #918
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.
…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.