orx_parallel/parallelizable_collection.rs
1use crate::{Params, computational_variants::Par, runner::DefaultRunner};
2use orx_concurrent_iter::{ConcurrentCollection, ConcurrentIterable};
3
4/// A type implementing [`ParallelizableCollection`] is a collection owning the elements such that
5///
6/// * if the elements are of type `T`,
7/// * non-consuming [`par`] method can be called **multiple times** to create parallel
8/// iterators; i.e., [`ParIter`], yielding references to the elements `&T`; and
9/// * consuming [`into_par`] method can be called once to create a parallel iterator yielding
10/// owned elements `T`.
11///
12/// This trait can be considered as the *concurrent counterpart* of the [`Collection`] trait.
13///
14/// Note that every [`ConcurrentCollection`] type automatically implements [`ParallelizableCollection`].
15///
16/// [`con_iter`]: orx_concurrent_iter::ConcurrentCollection::con_iter
17/// [`Collection`]: orx_iterable::Collection
18/// [`ConcurrentCollection`]: orx_concurrent_iter::ConcurrentCollection
19/// [`par`]: crate::ParallelizableCollection::par
20/// [`into_par`]: crate::IntoParIter::into_par
21/// [`ParIter`]: crate::ParIter
22///
23/// # Examples
24///
25/// ```
26/// use orx_parallel::*;
27///
28/// // Vec<T>: ParallelizableCollection<Item = T>
29/// let vec = vec![1, 2, 3, 4];
30///
31/// // non-consuming iteration over references
32/// assert_eq!(vec.par().sum(), 10);
33/// assert_eq!(vec.par().min(), Some(&1));
34/// assert_eq!(vec.par().max(), Some(&4));
35///
36/// // consuming iteration over owned values
37/// assert_eq!(vec.into_par().max(), Some(4));
38/// ```
39pub trait ParallelizableCollection: ConcurrentCollection {
40 /// A type implementing [`ParallelizableCollection`] is a collection owning the elements such that
41 ///
42 /// * if the elements are of type `T`,
43 /// * non-consuming [`par`] method can be called **multiple times** to create parallel
44 /// iterators; i.e., [`ParIter`], yielding references to the elements `&T`; and
45 /// * consuming [`into_par`] method can be called once to create a parallel iterator yielding
46 /// owned elements `T`.
47 ///
48 /// This trait can be considered as the *concurrent counterpart* of the [`Collection`] trait.
49 ///
50 /// Note that every [`ConcurrentCollection`] type automatically implements [`ParallelizableCollection`].
51 ///
52 /// [`con_iter`]: orx_concurrent_iter::ConcurrentCollection::con_iter
53 /// [`Collection`]: orx_iterable::Collection
54 /// [`ConcurrentIter`]: orx_concurrent_iter::ConcurrentIter
55 /// [`par`]: crate::ParallelizableCollection::par
56 /// [`into_par`]: crate::IntoParIter::into_par
57 /// [`ParIter`]: crate::ParIter
58 ///
59 /// # Examples
60 ///
61 /// ```
62 /// use orx_parallel::*;
63 ///
64 /// // Vec<T>: ParallelizableCollection<Item = T>
65 /// let vec = vec![1, 2, 3, 4];
66 ///
67 /// // non-consuming iteration over references
68 /// assert_eq!(vec.par().sum(), 10);
69 /// assert_eq!(vec.par().min(), Some(&1));
70 /// assert_eq!(vec.par().max(), Some(&4));
71 ///
72 /// // consuming iteration over owned values
73 /// assert_eq!(vec.into_par().max(), Some(4));
74 /// ```
75 fn par(
76 &self,
77 ) -> Par<
78 <<Self as ConcurrentCollection>::Iterable<'_> as ConcurrentIterable>::Iter,
79 DefaultRunner,
80 > {
81 Par::new(Params::default(), self.con_iter())
82 }
83}
84
85impl<X> ParallelizableCollection for X where X: ConcurrentCollection {}