Переглянути джерело

Implemented 'min' and 'min_by' operation

master
Bergmann89 5 роки тому
джерело
коміт
4bca6ced21
3 змінених файлів з 122 додано та 0 видалено
  1. +56
    -0
      asparit/src/core/iterator.rs
  2. +65
    -0
      asparit/src/inner/min.rs
  3. +1
    -0
      asparit/src/inner/mod.rs

+ 56
- 0
asparit/src/core/iterator.rs Переглянути файл

@@ -1,3 +1,4 @@
use std::cmp::{Ord, Ordering};
use std::iter::IntoIterator;

use super::{
@@ -19,6 +20,7 @@ use crate::{
map::Map,
map_init::MapInit,
map_with::MapWith,
min::{Min, MinBy},
product::Product,
reduce::{Reduce, ReduceWith},
sum::Sum,
@@ -1083,6 +1085,60 @@ pub trait ParallelIterator<'a>: Sized + Send {
Product::new(self)
}

/// Computes the minimum of all the items in the iterator. If the
/// iterator is empty, `None` is returned; otherwise, `Some(min)`
/// is returned.
///
/// Note that the order in which the items will be reduced is not
/// specified, so if the `Ord` impl is not truly associative, then
/// the results are not deterministic.
///
/// Basically equivalent to `self.reduce_with(|a, b| cmp::min(a, b))`.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let a = [45, 74, 32];
///
/// assert_eq!(a.par_iter().min(), Some(&32));
///
/// let b: [i32; 0] = [];
///
/// assert_eq!(b.par_iter().min(), None);
/// ```
fn min(self) -> Min<Self>
where
Self::Item: Ord,
{
Min::new(self)
}

/// Computes the minimum of all the items in the iterator with respect to
/// the given comparison function. If the iterator is empty, `None` is
/// returned; otherwise, `Some(min)` is returned.
///
/// Note that the order in which the items will be reduced is not
/// specified, so if the comparison function is not associative, then
/// the results are not deterministic.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let a = [-3_i32, 77, 53, 240, -1];
///
/// assert_eq!(a.par_iter().min_by(|x, y| x.cmp(y)), Some(&-3));
/// ```
fn min_by<O>(self, operation: O) -> MinBy<Self, O>
where
O: Fn(&Self::Item, &Self::Item) -> Ordering + Clone + Send + Sync + 'a,
{
MinBy::new(self, operation)
}

/// Creates a fresh collection containing all the elements produced
/// by this parallel iterator.
///


+ 65
- 0
asparit/src/inner/min.rs Переглянути файл

@@ -0,0 +1,65 @@
use std::cmp::{min, Ord, Ordering};

use crate::{Driver, Executor, ParallelIterator};

/* Min */

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

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

impl<'a, X, T> Driver<'a, Option<T>> for Min<X>
where
X: ParallelIterator<'a, Item = T>,
T: Send + Ord + 'a,
{
fn exec_with<E>(self, executor: E) -> E::Result
where
E: Executor<'a, Option<T>>,
{
self.iterator.reduce_with(min).exec_with(executor)
}
}

/* MinBy */

pub struct MinBy<X, O> {
iterator: X,
operation: O,
}

impl<X, O> MinBy<X, O> {
pub fn new(iterator: X, operation: O) -> Self {
Self {
iterator,
operation,
}
}
}

impl<'a, X, O, T> Driver<'a, Option<T>> for MinBy<X, O>
where
X: ParallelIterator<'a, Item = T>,
O: Fn(&T, &T) -> Ordering + Clone + Send + Sync + 'a,
T: Send + Ord + 'a,
{
fn exec_with<E>(self, executor: E) -> E::Result
where
E: Executor<'a, Option<T>>,
{
let operation = self.operation;

self.iterator
.reduce_with(move |a, b| match operation(&a, &b) {
Ordering::Greater => b,
_ => a,
})
.exec_with(executor)
}
}

+ 1
- 0
asparit/src/inner/mod.rs Переглянути файл

@@ -11,6 +11,7 @@ pub mod inspect;
pub mod map;
pub mod map_init;
pub mod map_with;
pub mod min;
pub mod noop;
pub mod product;
pub mod reduce;


Завантаження…
Відмінити
Зберегти