ソースを参照

Implemented 'count' operation

master
Bergmann89 5年前
コミット
8cd0a76438
3個のファイルの変更40行の追加0行の削除
  1. +16
    -0
      asparit/src/core/iterator.rs
  2. +23
    -0
      asparit/src/inner/count.rs
  3. +1
    -0
      asparit/src/inner/mod.rs

+ 16
- 0
asparit/src/core/iterator.rs ファイルの表示

@@ -9,6 +9,7 @@ use crate::{
cloned::Cloned,
collect::Collect,
copied::Copied,
count::Count,
filter::Filter,
filter_map::FilterMap,
flatten::{FlatMapIter, FlattenIter},
@@ -290,6 +291,21 @@ pub trait ParallelIterator<'a>: Sized + Send {
TryForEachInit::new(self, init, operation)
}

/// Counts the number of items in this parallel iterator.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let count = (0..100).into_par_iter().count();
///
/// assert_eq!(count, 100);
/// ```
fn count(self) -> Count<Self> {
Count::new(self)
}

/// Applies `operation` to each item of this iterator, producing a new
/// iterator with the results.
///


+ 23
- 0
asparit/src/inner/count.rs ファイルの表示

@@ -0,0 +1,23 @@
use crate::{Driver, Executor, ParallelIterator};

pub struct Count<X> {
iterator: X,
}

impl<X> Count<X> {
pub fn new(iterator: X) -> Self {
Self { iterator }
}
}

impl<'a, X> Driver<'a, usize> for Count<X>
where
X: ParallelIterator<'a>,
{
fn exec_with<E>(self, executor: E) -> E::Result
where
E: Executor<'a, usize>,
{
self.iterator.map(|_| 1).sum().exec_with(executor)
}
}

+ 1
- 0
asparit/src/inner/mod.rs ファイルの表示

@@ -1,6 +1,7 @@
pub mod cloned;
pub mod collect;
pub mod copied;
pub mod count;
pub mod filter;
pub mod filter_map;
pub mod flatten;


読み込み中…
キャンセル
保存