-
Notifications
You must be signed in to change notification settings - Fork 319
Add .get(range)
#891
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
Merged
jswrenn
merged 10 commits into
rust-itertools:master
from
Philippe-Cholet:447-get-range
May 14, 2024
Merged
Add .get(range)
#891
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6766fd
Add `Itertools::get` with `IteratorIndex`
Philippe-Cholet b190bfc
Suggestions
Philippe-Cholet 6451862
Small fixes
Philippe-Cholet ed2feb7
Test `Itertools::get` laziness
Philippe-Cholet 6f48e32
Remove the unspecified check about `.get(exhausted_range_inclusive)`
Philippe-Cholet e5b8c12
`get(r: Range)` as `Skip<Take>`
Philippe-Cholet 70e3a1a
`get`: panics if the range includes `usize::MAX`
Philippe-Cholet d4d5be3
`get`: when is it ESI and/or DEI
Philippe-Cholet 0ac556c
`get(s..=usize::MAX)` should be fine when `s != 0`
Philippe-Cholet 3f49188
Remove free function `get`
Philippe-Cholet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use core::iter::{Skip, Take}; | ||
use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; | ||
|
||
#[cfg(doc)] | ||
use crate::Itertools; | ||
|
||
mod private_iter_index { | ||
use core::ops; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for ops::Range<usize> {} | ||
impl Sealed for ops::RangeInclusive<usize> {} | ||
impl Sealed for ops::RangeTo<usize> {} | ||
impl Sealed for ops::RangeToInclusive<usize> {} | ||
impl Sealed for ops::RangeFrom<usize> {} | ||
impl Sealed for ops::RangeFull {} | ||
} | ||
|
||
/// Used by [`Itertools::get`] to know which iterator | ||
/// to turn different ranges into. | ||
pub trait IteratorIndex<I>: private_iter_index::Sealed | ||
where | ||
I: Iterator, | ||
{ | ||
/// The type returned for this type of index. | ||
type Output: Iterator<Item = I::Item>; | ||
|
||
/// Returns an adapted iterator for the current index. | ||
/// | ||
/// Prefer calling [`Itertools::get`] instead | ||
/// of calling this directly. | ||
fn index(self, from: I) -> Self::Output; | ||
} | ||
|
||
impl<I> IteratorIndex<I> for Range<usize> | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = Skip<Take<I>>; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
iter.take(self.end).skip(self.start) | ||
} | ||
} | ||
|
||
impl<I> IteratorIndex<I> for RangeInclusive<usize> | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = Take<Skip<I>>; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
// end - start + 1 without overflowing if possible | ||
let length = if *self.end() == usize::MAX { | ||
assert_ne!(*self.start(), 0); | ||
self.end() - self.start() + 1 | ||
} else { | ||
(self.end() + 1).saturating_sub(*self.start()) | ||
}; | ||
iter.skip(*self.start()).take(length) | ||
} | ||
} | ||
|
||
impl<I> IteratorIndex<I> for RangeTo<usize> | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = Take<I>; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
iter.take(self.end) | ||
} | ||
} | ||
|
||
impl<I> IteratorIndex<I> for RangeToInclusive<usize> | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = Take<I>; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
assert_ne!(self.end, usize::MAX); | ||
iter.take(self.end + 1) | ||
} | ||
} | ||
|
||
impl<I> IteratorIndex<I> for RangeFrom<usize> | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = Skip<I>; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
iter.skip(self.start) | ||
} | ||
} | ||
|
||
impl<I> IteratorIndex<I> for RangeFull | ||
where | ||
I: Iterator, | ||
{ | ||
type Output = I; | ||
|
||
fn index(self, iter: I) -> Self::Output { | ||
iter | ||
} | ||
} | ||
|
||
pub fn get<I, R>(iter: I, index: R) -> R::Output | ||
where | ||
I: IntoIterator, | ||
R: IteratorIndex<I::IntoIter>, | ||
{ | ||
index.index(iter.into_iter()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.