Implement index-sliced key-value iterators - #104
Conversation
These allow us to slice using RangeBounds<usize> which includes all the range types (a..b, c..=d) and some extras. The main infidelity of the interface is the need to handle Bound::Exclusive(std::usize::MAX) which does not have an equivalent in the i..j or i..=j and slices can't handle it natively. The scaffolding in src/util to create the .slice() and .slice_mut() methods is somewhat verbose, but it allows us to write neat code where it is used.
Add iterators that iterate a sliced key-value range (sliced in the order/index space).
cuviper
left a comment
There was a problem hiding this comment.
The scaffolding in src/util to create the .slice() and .slice_mut()
methods is somewhat verbose
It is, but dealing with both axes of &/&mut and Range/RangeFrom/RangeInc is a challenge. Maybe it would be simpler to map the RangeBounds to a simple Range, given the slice length? Just like you're using start Unbounded => 0. An inclusive end can use saturating_add(1), since inclusive MAX is always out of bounds anyway.
Not a big deal as-is though, since this is all internal.
For completeness, we should have methods slicing to map::Keys and set::Iter/IterMut too.
| let (iplus1, overflow) = i.overflowing_add(1); | ||
| if overflow { | ||
| panic!(concat!("Range out of bounds: exclusive from ", | ||
| stringify!(std::usize::MAX))); |
There was a problem hiding this comment.
That stringify! prints the tokens, not the constant value -- std :: usize :: MAX. You could use a runtime formatting string for panic! -- this is a cold case anyway. Or just say, "exclusive from usize::MAX".
There was a problem hiding this comment.
printing the tokens was intended, but the whitespace isn't pretty. I guess I can just use the simple option of writing usize::MAX :)
|
Converting to Range makes sense, but I have been stuck on this thought that this way would be the optimal way to do it for removing as many bounds checks as possible. However, (now looking) the std slicing code does not pay attention to making the tightest possible code for RangeInclusive's bounds checking in slicing, so it is rather a moot point right now. |
|
I believe this use-case is served by #177. |
Add iterators that iterate a sliced key-value range (sliced in the
order/index space).
Before merging, we'll look at if it's not better to express using views (from #47).
The main infidelity of the interface is the need to handle
Bound::Exclusive(std::usize::MAX) which does not have an equivalent in
the i..j or i..=j and slices can't handle it natively.
The scaffolding in src/util to create the .slice() and .slice_mut()
methods is somewhat verbose, but it allows us to write neat code where
it is used.
Fixes #103