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