Lymia / enumset

A library for compact bit sets containing enums.
Apache License 2.0
91 stars 35 forks source link

Implement `DoubleEndedIterator` (or `Iterator::rev`) for `EnumSetIter` #30

Closed ahcodedthat closed 2 years ago

ahcodedthat commented 2 years ago

I ran into a situation where I needed to find the greatest item in an EnumSet. Although this can be done straightforwardly with Iterator::last, it would be more efficient to use DoubleEndedIterator::next_back…if EnumSetIter implemented DoubleEndedIterator, which it currently doesn't.

Lymia commented 2 years ago

I'll take a look at what I can do. Note that your use case relies on guarantees that EnumSet<T>::iter currently doesn't actually have, namely that the iteration returns values in a particular order. This should be safe to add to the Rustdoc too, but it is worth noting this isn't technically documented behavior currently.

ahcodedthat commented 2 years ago

Good point. I had assumed that any bit-set would have a predictable iteration order, but you're right that the documentation doesn't actually promise that. I'll use Iterator::max instead for now.

Thanks for making this crate, by the way. It's a pretty major improvement over the bespoke bit-set implementation I was using.

Lymia commented 2 years ago

I added the guarantees needed for this and implemented DoubleEndedIterator. I can't think of a good reason why I might want to not do lowest-to-highest bit order unless on some obscure platform, highest-to-lowest iteration is more efficient -- and the predictability of order seems more important.