Selaa lähdekoodia

Implemented 'enumerate' operation

master
Bergmann89 5 vuotta sitten
vanhempi
commit
3e7fab931d
3 muutettua tiedostoa jossa 197 lisäystä ja 2 poistoa
  1. +20
    -0
      asparit/src/core/iterator.rs
  2. +172
    -0
      asparit/src/iter/enumerate.rs
  3. +5
    -2
      asparit/src/iter/mod.rs

+ 20
- 0
asparit/src/core/iterator.rs Näytä tiedosto

@@ -14,6 +14,7 @@ use crate::{
collect::Collect,
copied::Copied,
count::Count,
enumerate::Enumerate,
filter::Filter,
filter_map::FilterMap,
find::{All, Any, Find, FindMap, FindMatch},
@@ -2041,6 +2042,25 @@ pub trait IndexedParallelIterator<'a>: ParallelIterator<'a> {
)
}

/// Yields an index along with each item.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let chars = vec!['a', 'b', 'c'];
/// let result: Vec<_> = chars
/// .into_par_iter()
/// .enumerate()
/// .collect();
///
/// assert_eq!(result, [(0, 'a'), (1, 'b'), (2, 'c')]);
/// ```
fn enumerate(self) -> Enumerate<Self> {
Enumerate::new(self)
}

/// Creates an iterator that yields the first `n` elements.
///
/// # Examples


+ 172
- 0
asparit/src/iter/enumerate.rs Näytä tiedosto

@@ -0,0 +1,172 @@
use std::iter::Zip;
use std::ops::Range;

use crate::{
Consumer, Executor, ExecutorCallback, IndexedParallelIterator, IndexedProducer,
IndexedProducerCallback, ParallelIterator, Producer, Reducer, Setup, WithIndexedProducer,
WithSetup,
};

pub struct Enumerate<X> {
base: X,
}

impl<X> Enumerate<X> {
pub fn new(base: X) -> Self {
Self { base }
}
}

impl<'a, X, I> ParallelIterator<'a> for Enumerate<X>
where
X: IndexedParallelIterator<'a, Item = I> + WithIndexedProducer<'a, Item = I>,
I: Send + 'a,
{
type Item = (usize, I);

fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
where
E: Executor<'a, D>,
C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
D: Send + 'a,
R: Reducer<D> + Send + 'a,
{
self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
}

fn len_hint_opt(&self) -> Option<usize> {
self.base.len_hint_opt()
}
}

impl<'a, X, I> IndexedParallelIterator<'a> for Enumerate<X>
where
X: IndexedParallelIterator<'a, Item = I> + WithIndexedProducer<'a, Item = I>,
I: Send + 'a,
{
fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
where
E: Executor<'a, D>,
C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
D: Send + 'a,
R: Reducer<D> + Send + 'a,
{
self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
}

fn len_hint(&self) -> usize {
self.base.len_hint()
}
}

impl<'a, X> WithIndexedProducer<'a> for Enumerate<X>
where
X: WithIndexedProducer<'a>,
{
type Item = (usize, X::Item);

fn with_indexed_producer<CB>(self, base: CB) -> CB::Output
where
CB: IndexedProducerCallback<'a, Self::Item>,
{
self.base.with_indexed_producer(EnumerateCallback { base })
}
}

/* EnumerateCallback */

struct EnumerateCallback<CB> {
base: CB,
}

impl<'a, CB, I> IndexedProducerCallback<'a, I> for EnumerateCallback<CB>
where
CB: IndexedProducerCallback<'a, (usize, I)>,
{
type Output = CB::Output;

fn callback<P>(self, base: P) -> Self::Output
where
P: IndexedProducer<Item = I> + 'a,
{
self.base.callback(EnumerateProducer { base, offset: 0 })
}
}

/* EnumerateProducer */

struct EnumerateProducer<P> {
base: P,
offset: usize,
}

impl<P> WithSetup for EnumerateProducer<P>
where
P: WithSetup,
{
fn setup(&self) -> Setup {
self.base.setup()
}
}

impl<P> Producer for EnumerateProducer<P>
where
P: IndexedProducer,
{
type Item = (usize, P::Item);
type IntoIter = Zip<Range<usize>, P::IntoIter>;

fn into_iter(self) -> Self::IntoIter {
let base = self.base.into_iter();
let start = self.offset;
let end = start + base.len();

(start..end).zip(base)
}

fn split(self) -> (Self, Option<Self>) {
let len = self.base.len();
if len < 2 {
return (self, None);
}

let (left, right) = self.split_at(len / 2);

(left, Some(right))
}
}

impl<P> IndexedProducer for EnumerateProducer<P>
where
P: IndexedProducer,
{
type Item = (usize, P::Item);
type IntoIter = Zip<Range<usize>, P::IntoIter>;

fn into_iter(self) -> Self::IntoIter {
let base = self.base.into_iter();
let start = self.offset;
let end = start + base.len();

(start..end).zip(base)
}

fn len(&self) -> usize {
self.base.len()
}

fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.base.split_at(index);

let left = Self {
base: left,
offset: self.offset,
};
let right = Self {
base: right,
offset: self.offset + index,
};

(left, right)
}
}

+ 5
- 2
asparit/src/iter/mod.rs Näytä tiedosto

@@ -5,6 +5,7 @@ pub mod cmp;
pub mod collect;
pub mod copied;
pub mod count;
pub mod enumerate;
pub mod filter;
pub mod filter_map;
pub mod find;
@@ -43,8 +44,10 @@ mod tests {
async fn test_for_each() {
let x = vec![0usize, 1, 2, 3, 4]
.into_par_iter()
.with_splits(1)
.lt(vec![0usize, 1, 2, 3, 4, 5])
.enumerate()
.for_each(|x| {
dbg!(x);
})
.exec()
.await;



Ladataan…
Peruuta
Tallenna