Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement index-sliced key-value iterators #104

Closed
wants to merge 2 commits into from
Closed

Conversation

bluss
Copy link
Member

@bluss bluss commented Sep 8, 2019

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

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).
Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printing the tokens was intended, but the whitespace isn't pretty. I guess I can just use the simple option of writing usize::MAX :)

@bluss
Copy link
Member Author

bluss commented Sep 10, 2019

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.

@cuviper
Copy link
Member

cuviper commented Jul 15, 2022

I believe this use-case is served by #177.

@cuviper cuviper closed this Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: .values_mut_range(idx..)
2 participants