sdleffler / qp-trie-rs

An idiomatic and fast QP-trie implementation in pure Rust.
Mozilla Public License 2.0
94 stars 24 forks source link

Iterate over all common prefixes #30

Open alexkirsz opened 2 years ago

alexkirsz commented 2 years ago

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 commented 1 year ago

do you mean longest_common_prefix?

alexkirsz commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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