fst icon indicating copy to clipboard operation
fst copied to clipboard

find the last element in a `Stream`?

Open droundy opened this issue 2 years ago • 5 comments

Would it be possible to make it fast to either reverse a Stream or to find the last element in it? I just want to know the first and last element matching my query, and would like to do so without iterating through everything in between.

droundy avatar Jul 30 '22 13:07 droundy

I think #129 is related? If you didn't know, the raw API let's you walk the automaton. I think it exposes every capability. So if you can't do it with that, then I don't think it's possible.

With that said, if you just want the maxkey, I think that can be done by traversing the last transition in each state? There is no specific API for it, but I think coding it up shouldn't be too bad?

BurntSushi avatar Jul 30 '22 14:07 BurntSushi

Thanks for the rapid response! Maybe I can take a look tomorrow. I'll have to take a look at the lower level API. Do you think this could be added as a last method in Stream?

droundy avatar Jul 30 '22 14:07 droundy

I don't have this crate paged into context at the moment and I probably won't have time to page it in soon.

My suspicion is that you cannot add last to a generic interface. But you might be able to add it to the automaton types themselves. You might be able to write a last method that accepts anything that imple Automaton though?

Don't take my word for gospel though.

BurntSushi avatar Jul 30 '22 14:07 BurntSushi

Great, thanks!

droundy avatar Jul 30 '22 14:07 droundy

Somewhat related but maybe distinct enough for its own issue, there's no way to iterate over fst::map::Values or fst::set::Stream in a reversed order, nor any way I can see to make Automation do something like that (without allocating a vec and collecting all elements before iterating over that in a reversed order)

Kixiron avatar Apr 30 '23 15:04 Kixiron