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

Iterate over all common prefixes #30

Open
alexkirsz opened this issue Sep 14, 2022 · 4 comments
Open

Iterate over all common prefixes #30

alexkirsz opened this issue Sep 14, 2022 · 4 comments

Comments

@alexkirsz
Copy link

Is there a way to iterate over all common prefixes with a particular key?

See https://docs.rs/patricia_tree/latest/patricia_tree/map/struct.PatriciaMap.html#method.common_prefixes

@erikbrinkman
Copy link
Contributor

do you mean longest_common_prefix?

@alexkirsz
Copy link
Author

longest_common_prefix returns the longest common prefix with a given key. I need to iterate over all common prefixes, not just the longest. Specifically, I need the same behavior as https://docs.rs/patricia_tree/latest/patricia_tree/map/struct.PatriciaMap.html#method.common_prefixes (see the example).

@erikbrinkman
Copy link
Contributor

it's not super elegant, but it seems like this can be implemented efficiently with the subtrie method. You just repeatedly call subtrie and get on progressively longer slices, and this has the effect of advancing the node in the tree. If you know you have strings, this might be a little faster by using the unicode representations to avoid unnecessary calls to get, although that's not obviously the case.

@hi-glenn
Copy link

hi-glenn commented Sep 19, 2023

Could add a function like longest_common_prefix_with_value(), it return the longest common prefix and the reference to the value associated with the longest common prefix at the same time ? If so, it can bring great convenience.

@erikbrinkman

ForsakenHarmony pushed a commit to vercel/next.js that referenced this issue Jul 25, 2024
This replaces the `PrefixTree` struct with an `AliasMap` struct. The underlying implementation is different: we no longer build a prefix tree by splitting paths into segments, but instead we have per-character (technically per u8) granularity.

I used the [patricia_tree](https://github.com/sile/patricia_tree) crate for the radix tree implementation. My first implementation was based on [qp_trie](https://github.com/sdleffler/qp-trie-rs) but they do not provide a way to iterate over [all common prefixes](sdleffler/qp-trie-rs#30) of a given key and the tree's entries.

Fixes #351.


Co-authored-by: Tobias Koppers <[email protected]>
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

No branches or pull requests

3 participants