Просмотр исходного кода

Implemented 'max' and 'max_by' operation

master
Bergmann89 5 лет назад
Родитель
Сommit
de837c0ec5
3 измененных файлов: 121 добавлений и 0 удалений
  1. +55
    -0
      asparit/src/core/iterator.rs
  2. +65
    -0
      asparit/src/inner/max.rs
  3. +1
    -0
      asparit/src/inner/mod.rs

+ 55
- 0
asparit/src/core/iterator.rs Просмотреть файл

@@ -20,6 +20,7 @@ use crate::{
map::Map,
map_init::MapInit,
map_with::MapWith,
max::{Max, MaxBy},
min::{Min, MinBy},
product::Product,
reduce::{Reduce, ReduceWith},
@@ -1139,6 +1140,60 @@ pub trait ParallelIterator<'a>: Sized + Send {
MinBy::new(self, operation)
}

/// Computes the maximum of all the items in the iterator. If the
/// iterator is empty, `None` is returned; otherwise, `Some(max)`
/// 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::max(a, b))`.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
///
/// let a = [45, 74, 32];
///
/// assert_eq!(a.par_iter().max(), Some(&74));
///
/// let b: [i32; 0] = [];
///
/// assert_eq!(b.par_iter().max(), None);
/// ```
fn max(self) -> Max<Self>
where
Self::Item: Ord,
{
Max::new(self)
}

/// Computes the maximum 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().max_by(|x, y| x.abs().cmp(&y.abs())), Some(&240));
/// ```
fn max_by<O>(self, operation: O) -> MaxBy<Self, O>
where
O: Fn(&Self::Item, &Self::Item) -> Ordering + Clone + Send + Sync + 'a,
{
MaxBy::new(self, operation)
}

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


+ 65
- 0
asparit/src/inner/max.rs Просмотреть файл

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

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

/* Max */

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

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

impl<'a, X, T> Driver<'a, Option<T>> for Max<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(max).exec_with(executor)
}
}

/* MaxBy */

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

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

impl<'a, X, O, T> Driver<'a, Option<T>> for MaxBy<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 max;
pub mod min;
pub mod noop;
pub mod product;


Загрузка…
Отмена
Сохранить