-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement parallel sources over ranges.
- Loading branch information
Showing
5 changed files
with
257 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,151 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use super::{IntoParallelSource, ParallelSource, SourceDescriptor}; | ||
#[cfg(feature = "nightly")] | ||
use std::iter::Step; | ||
use std::ops::{Range, RangeInclusive}; | ||
|
||
/// A parallel source over a [`Range`]. This struct is created by the | ||
/// [`into_par_iter()`](IntoParallelSource::into_par_iter) method on | ||
/// [`IntoParallelSource`]. | ||
#[must_use = "iterator adaptors are lazy"] | ||
pub struct RangeParallelSource<T> { | ||
range: Range<T>, | ||
} | ||
|
||
#[cfg(feature = "nightly")] | ||
impl<T: Step + Copy + Send + Sync> IntoParallelSource for Range<T> { | ||
type Item = T; | ||
type Source = RangeParallelSource<T>; | ||
|
||
fn into_par_iter(self) -> Self::Source { | ||
RangeParallelSource { range: self } | ||
} | ||
} | ||
|
||
#[cfg(feature = "nightly")] | ||
impl<T: Step + Copy + Send + Sync> ParallelSource for RangeParallelSource<T> { | ||
type Item = T; | ||
|
||
fn descriptor(self) -> SourceDescriptor<Self::Item, impl Fn(usize) -> Self::Item + Sync> { | ||
let range = self.range; | ||
let len = T::steps_between(&range.start, &range.end).unwrap_or_else(|| { | ||
panic!( | ||
"cannot iterate over a range with more than usize::MAX ({}) items", | ||
usize::MAX | ||
); | ||
}); | ||
SourceDescriptor { | ||
len, | ||
fetch_item: move |index| T::forward(range.start, index), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "nightly"))] | ||
impl IntoParallelSource for Range<usize> { | ||
type Item = usize; | ||
type Source = RangeParallelSource<usize>; | ||
|
||
fn into_par_iter(self) -> Self::Source { | ||
RangeParallelSource { range: self } | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "nightly"))] | ||
impl ParallelSource for RangeParallelSource<usize> { | ||
type Item = usize; | ||
|
||
fn descriptor(self) -> SourceDescriptor<Self::Item, impl Fn(usize) -> Self::Item + Sync> { | ||
let range = self.range; | ||
let len = range | ||
.end | ||
.checked_sub(range.start) | ||
.expect("cannot iterate over a backward range"); | ||
SourceDescriptor { | ||
len, | ||
fetch_item: move |index| range.start + index, | ||
} | ||
} | ||
} | ||
|
||
/// A parallel source over a [`RangeInclusive`]. This struct is created by the | ||
/// [`into_par_iter()`](IntoParallelSource::into_par_iter) method on | ||
/// [`IntoParallelSource`]. | ||
#[must_use = "iterator adaptors are lazy"] | ||
pub struct RangeInclusiveParallelSource<T> { | ||
range: RangeInclusive<T>, | ||
} | ||
|
||
#[cfg(feature = "nightly")] | ||
impl<T: Step + Copy + Send + Sync> IntoParallelSource for RangeInclusive<T> { | ||
type Item = T; | ||
type Source = RangeInclusiveParallelSource<T>; | ||
|
||
fn into_par_iter(self) -> Self::Source { | ||
RangeInclusiveParallelSource { range: self } | ||
} | ||
} | ||
|
||
#[cfg(feature = "nightly")] | ||
impl<T: Step + Copy + Send + Sync> ParallelSource for RangeInclusiveParallelSource<T> { | ||
type Item = T; | ||
|
||
fn descriptor(self) -> SourceDescriptor<Self::Item, impl Fn(usize) -> Self::Item + Sync> { | ||
let (start, end) = self.range.into_inner(); | ||
let len = T::steps_between(&start, &end).unwrap_or_else(|| { | ||
panic!( | ||
"cannot iterate over a range with more than usize::MAX ({}) items", | ||
usize::MAX | ||
); | ||
}); | ||
let len = len.checked_add(1).unwrap_or_else(|| { | ||
panic!( | ||
"cannot iterate over a range with more than usize::MAX ({}) items", | ||
usize::MAX | ||
); | ||
}); | ||
SourceDescriptor { | ||
len, | ||
fetch_item: move |index| T::forward(start, index), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "nightly"))] | ||
impl IntoParallelSource for RangeInclusive<usize> { | ||
type Item = usize; | ||
type Source = RangeInclusiveParallelSource<usize>; | ||
|
||
fn into_par_iter(self) -> Self::Source { | ||
RangeInclusiveParallelSource { range: self } | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "nightly"))] | ||
impl ParallelSource for RangeInclusiveParallelSource<usize> { | ||
type Item = usize; | ||
|
||
fn descriptor(self) -> SourceDescriptor<Self::Item, impl Fn(usize) -> Self::Item + Sync> { | ||
let (start, end) = self.range.into_inner(); | ||
let len = end | ||
.checked_sub(start) | ||
.expect("cannot iterate over a backward range"); | ||
let len = len.checked_add(1).unwrap_or_else(|| { | ||
panic!( | ||
"cannot iterate over a range with more than usize::MAX ({}) items", | ||
usize::MAX | ||
); | ||
}); | ||
SourceDescriptor { | ||
len, | ||
fetch_item: move |index| start + index, | ||
} | ||
} | ||
} |
This file contains 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