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

Select the longest matching key in matchMap #179

Merged
merged 2 commits into from
Jun 21, 2017

Conversation

barnardb
Copy link
Contributor

@barnardb barnardb commented Mar 3, 2017

Fixes #136

By checking keys from longest to shortest, we ensure that we always
select the longest matching key.

This fix requires sorting the keys each time the rule is invoked. If
#115 gets implemented, this could be improved to make use of it instead.

Fixes sirthias#136

By checking keys from longest to shortest, we ensure that we always
select the longest matching key.

This fix requires sorting the keys each time the rule is invoked. If
sirthias#115 gets implemented, this could be improved to make use of it instead.
@barnardb
Copy link
Contributor Author

barnardb commented Mar 3, 2017

Sorry, I wasn't thinking straight in writing that final comment. The map value isn't known until runtime, so of course the optimisation desired in #115 would be unhelpful.

@@ -449,7 +449,7 @@ abstract class Parser(initialValueStackSize: Int = 16,
* THIS IS NOT PUBLIC API and might become hidden in future. Use only if you know what you are doing!
*/
def __matchMap(m: Map[String, Any]): Boolean = {
val keys = m.keysIterator
val keys = m.keys.toSeq.sortBy(-_.length).iterator
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can actually make this more efficient.
We don't need to sort the whole map, only the prefix up until the match.
Check out this SO answer for an idea of how this could be done.

Only sort the keys until we find the one we're interested in, as suggested in
sirthias#179 (review)
@sirthias sirthias merged commit d7ad77a into sirthias:release-2.1 Jun 21, 2017
@sirthias
Copy link
Owner

Thank you, Ben, and sorry for taking so long to get this merged!

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.

valueMap does not backtrack correctly
2 participants