billyrieger / bimap-rs

Generic bijective maps in Rust
Apache License 2.0
129 stars 26 forks source link

Support range iterators in BiBTreeMap #17

Closed solarretrace closed 4 years ago

solarretrace commented 4 years ago

The std BTreeMap supports iteration over a subset of the map using a range (BTreeMap::range, BTreeMap::range_mut). If I understand correctly, the BiBTreeMap could support the same operation (in both directions) but not the _mut version.

I'm happy to submit a PR if these methods are desirable and I find some time to add them. I have implementations prepared with the following signatures:

pub fn left_range<'a, Q, A>(&'a self, range: A) -> LeftRange<'a, L, R>
    where
        Rc<L>: Borrow<Q>,
        A: std::ops::RangeBounds<Q>,
        Q: Ord + ?Sized;

pub fn right_range<'a, Q, A>(&'a self, range: A) -> RightRange<'a, L, R>
    where
        Rc<R>: Borrow<Q>,
        A: std::ops::RangeBounds<Q>,
        Q: Ord + ?Sized;
billyrieger commented 4 years ago

See https://github.com/billyrieger/bimap-rs/issues/16#issuecomment-657137595. This would be nice to have but it leaks implementation details.

solarretrace commented 4 years ago

You're probably right about Rc in the signature here, but I think these methods would still be usable with a more restricted signature:

pub fn left_range<'a, A>(&'a self, range: A) -> LeftRange<'a, L, R>
    where A: std::ops::RangeBounds<L>;

pub fn right_range<'a, A>(&'a self, range: A) -> RightRange<'a, L, R>
        where A: std::ops::RangeBounds<R>;
billyrieger commented 4 years ago

Yes, that looks like it should work. If you're up for making a PR I'd be happy to review it!

billyrieger commented 4 years ago

Closed by #18.