Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1773 lines
60 KiB

  1. use std::cmp::{Ord, Ordering};
  2. use std::iter::IntoIterator;
  3. use super::{Consumer, Executor, FromParallelIterator, IntoParallelIterator, Reducer};
  4. use crate::{
  5. iter::{
  6. chain::Chain,
  7. cloned::Cloned,
  8. collect::Collect,
  9. copied::Copied,
  10. count::Count,
  11. filter::Filter,
  12. filter_map::FilterMap,
  13. find::{All, Any, Find, FindMap, FindMatch},
  14. flatten::{FlatMapIter, FlattenIter},
  15. fold::{Fold, FoldWith},
  16. for_each::ForEach,
  17. inspect::Inspect,
  18. intersperse::Intersperse,
  19. map::Map,
  20. map_init::MapInit,
  21. map_with::MapWith,
  22. max::{Max, MaxBy, MaxByKey},
  23. min::{Min, MinBy, MinByKey},
  24. panic_fuse::PanicFuse,
  25. partition::{Partition, PartitionMap},
  26. product::Product,
  27. reduce::{Reduce, ReduceWith},
  28. splits::Splits,
  29. sum::Sum,
  30. try_fold::{TryFold, TryFoldWith},
  31. try_for_each::{TryForEach, TryForEachInit, TryForEachWith},
  32. try_reduce::{TryReduce, TryReduceWith},
  33. unzip::Unzip,
  34. update::Update,
  35. while_some::WhileSome,
  36. },
  37. misc::Try,
  38. };
  39. /// Parallel version of the standard iterator trait.
  40. ///
  41. /// The combinators on this trait are available on **all** parallel
  42. /// iterators. Additional methods can be found on the
  43. /// [`IndexedParallelIterator`] trait: those methods are only
  44. /// available for parallel iterators where the number of items is
  45. /// known in advance (so, e.g., after invoking `filter`, those methods
  46. /// become unavailable).
  47. ///
  48. /// For examples of using parallel iterators, see [the docs on the
  49. /// `iter` module][iter].
  50. ///
  51. /// [iter]: index.html
  52. /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
  53. pub trait ParallelIterator<'a>: Sized + Send {
  54. /// The type of item that this parallel iterator produces.
  55. /// For example, if you use the [`for_each`] method, this is the type of
  56. /// item that your closure will be invoked with.
  57. ///
  58. /// [`for_each`]: #method.for_each
  59. type Item: Send + 'a;
  60. /// Internal method used to define the behavior of this parallel
  61. /// iterator. You should not need to call this directly.
  62. ///
  63. /// This method causes the iterator `self` to start producing
  64. /// items and to feed them to the consumer `consumer` one by one.
  65. /// It may split the consumer before doing so to create the
  66. /// opportunity to produce in parallel.
  67. ///
  68. /// See the [README] for more details on the internals of parallel
  69. /// iterators.
  70. ///
  71. /// [README]: README.md
  72. fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  73. where
  74. E: Executor<'a, D>,
  75. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  76. D: Send + 'a,
  77. R: Reducer<D> + Send + 'a;
  78. /// Internal method used to define the behavior of this parallel
  79. /// iterator. You should not need to call this directly.
  80. ///
  81. /// Returns the number of items produced by this iterator, if known
  82. /// statically. This can be used by consumers to trigger special fast
  83. /// paths. Therefore, if `Some(_)` is returned, this iterator must only
  84. /// use the (indexed) `Consumer` methods when driving a consumer, such
  85. /// as `split_at()`. Calling `UnindexedConsumer::split_off_left()` or
  86. /// other `UnindexedConsumer` methods -- or returning an inaccurate
  87. /// value -- may result in panics.
  88. ///
  89. /// This method is currently used to optimize `collect` for want
  90. /// of true Rust specialization; it may be removed when
  91. /// specialization is stable.
  92. fn len_hint_opt(&self) -> Option<usize> {
  93. None
  94. }
  95. /// Executes `operation` on each item produced by the iterator, in parallel.
  96. ///
  97. /// # Examples
  98. ///
  99. /// ```
  100. /// use asparit::*;
  101. ///
  102. /// (0..100).into_par_iter().for_each(|x| println!("{:?}", x));
  103. /// ```
  104. fn for_each<O>(self, operation: O) -> ForEach<Self, O>
  105. where
  106. O: Fn(Self::Item),
  107. {
  108. ForEach::new(self, operation)
  109. }
  110. /// Executes `operation` on the given `init` value with each item produced by
  111. /// the iterator, in parallel.
  112. ///
  113. /// The `init` value will be cloned only as needed to be paired with
  114. /// the group of items in each rayon job. It does not require the type
  115. /// to be `Sync`.
  116. ///
  117. /// # Examples
  118. ///
  119. /// ```
  120. /// use std::sync::mpsc::channel;
  121. /// use rayon::prelude::*;
  122. ///
  123. /// let (sender, receiver) = channel();
  124. ///
  125. /// (0..5).into_par_iter().for_each_with(sender, |s, x| s.send(x).unwrap());
  126. ///
  127. /// let mut res: Vec<_> = receiver.iter().collect();
  128. ///
  129. /// res.sort();
  130. ///
  131. /// assert_eq!(&res[..], &[0, 1, 2, 3, 4])
  132. /// ```
  133. fn for_each_with<O, T>(self, init: T, operation: O) -> Collect<MapWith<Self, T, O>, ()>
  134. where
  135. O: Fn(&mut T, Self::Item) + Clone + Send + 'a,
  136. T: Clone + Send + 'a,
  137. {
  138. self.map_with(init, operation).collect()
  139. }
  140. /// Executes `operation` on a value returned by `init` with each item produced by
  141. /// the iterator, in parallel.
  142. ///
  143. /// The `init` function will be called only as needed for a value to be
  144. /// paired with the group of items in each rayon job. There is no
  145. /// constraint on that returned type at all!
  146. ///
  147. /// # Examples
  148. ///
  149. /// ```
  150. /// use rand::Rng;
  151. /// use rayon::prelude::*;
  152. ///
  153. /// let mut v = vec![0u8; 1_000_000];
  154. ///
  155. /// v.par_chunks_mut(1000)
  156. /// .for_each_init(
  157. /// || rand::thread_rng(),
  158. /// |rng, chunk| rng.fill(chunk),
  159. /// );
  160. ///
  161. /// // There's a remote chance that this will fail...
  162. /// for i in 0u8..=255 {
  163. /// assert!(v.contains(&i));
  164. /// }
  165. /// ```
  166. fn for_each_init<O, S, T>(self, init: S, operation: O) -> Collect<MapInit<Self, S, O>, ()>
  167. where
  168. O: Fn(&mut T, Self::Item) + Clone + Send + 'a,
  169. S: Fn() -> T + Clone + Send + 'a,
  170. {
  171. self.map_init(init, operation).collect()
  172. }
  173. /// Executes a fallible `operation` on each item produced by the iterator, in parallel.
  174. ///
  175. /// If the `operation` returns `Result::Err` or `Option::None`, we will attempt to
  176. /// stop processing the rest of the items in the iterator as soon as
  177. /// possible, and we will return that terminating value. Otherwise, we will
  178. /// return an empty `Result::Ok(())` or `Option::Some(())`. If there are
  179. /// multiple errors in parallel, it is not specified which will be returned.
  180. ///
  181. /// # Examples
  182. ///
  183. /// ```
  184. /// use rayon::prelude::*;
  185. /// use std::io::{self, Write};
  186. ///
  187. /// // This will stop iteration early if there's any write error, like
  188. /// // having piped output get closed on the other end.
  189. /// (0..100).into_par_iter()
  190. /// .try_for_each(|x| writeln!(io::stdout(), "{:?}", x))
  191. /// .expect("expected no write errors");
  192. /// ```
  193. fn try_for_each<O, T>(self, operation: O) -> TryForEach<Self, O>
  194. where
  195. O: Fn(Self::Item) -> T + Clone + Send,
  196. T: Try<Ok = ()> + Send,
  197. {
  198. TryForEach::new(self, operation)
  199. }
  200. /// Executes a fallible `operation` on the given `init` value with each item
  201. /// produced by the iterator, in parallel.
  202. ///
  203. /// This combines the `init` semantics of [`for_each_with()`] and the
  204. /// failure semantics of [`try_for_each()`].
  205. ///
  206. /// [`for_each_with()`]: #method.for_each_with
  207. /// [`try_for_each()`]: #method.try_for_each
  208. ///
  209. /// # Examples
  210. ///
  211. /// ```
  212. /// use std::sync::mpsc::channel;
  213. /// use rayon::prelude::*;
  214. ///
  215. /// let (sender, receiver) = channel();
  216. ///
  217. /// (0..5).into_par_iter()
  218. /// .try_for_each_with(sender, |s, x| s.send(x))
  219. /// .expect("expected no send errors");
  220. ///
  221. /// let mut res: Vec<_> = receiver.iter().collect();
  222. ///
  223. /// res.sort();
  224. ///
  225. /// assert_eq!(&res[..], &[0, 1, 2, 3, 4])
  226. /// ```
  227. fn try_for_each_with<O, S, T>(self, init: S, operation: O) -> TryForEachWith<Self, S, O>
  228. where
  229. S: Clone + Send + 'a,
  230. O: Fn(&mut S, Self::Item) -> T + Clone + Send + 'a,
  231. T: Try<Ok = ()> + Send + 'a,
  232. {
  233. TryForEachWith::new(self, init, operation)
  234. }
  235. /// Executes a fallible `operation` on a value returned by `init` with each item
  236. /// produced by the iterator, in parallel.
  237. ///
  238. /// This combines the `init` semantics of [`for_each_init()`] and the
  239. /// failure semantics of [`try_for_each()`].
  240. ///
  241. /// [`for_each_init()`]: #method.for_each_init
  242. /// [`try_for_each()`]: #method.try_for_each
  243. ///
  244. /// # Examples
  245. ///
  246. /// ```
  247. /// use rand::Rng;
  248. /// use rayon::prelude::*;
  249. ///
  250. /// let mut v = vec![0u8; 1_000_000];
  251. ///
  252. /// v.par_chunks_mut(1000)
  253. /// .try_for_each_init(
  254. /// || rand::thread_rng(),
  255. /// |rng, chunk| rng.try_fill(chunk),
  256. /// )
  257. /// .expect("expected no rand errors");
  258. ///
  259. /// // There's a remote chance that this will fail...
  260. /// for i in 0u8..=255 {
  261. /// assert!(v.contains(&i));
  262. /// }
  263. /// ```
  264. fn try_for_each_init<O, S, T, U>(self, init: S, operation: O) -> TryForEachInit<Self, S, O>
  265. where
  266. O: Fn(&mut U, Self::Item) -> T + Clone + Send + 'a,
  267. S: Fn() -> U + Clone + Send + 'a,
  268. T: Try<Ok = ()> + Send + 'a,
  269. {
  270. TryForEachInit::new(self, init, operation)
  271. }
  272. /// Counts the number of items in this parallel iterator.
  273. ///
  274. /// # Examples
  275. ///
  276. /// ```
  277. /// use rayon::prelude::*;
  278. ///
  279. /// let count = (0..100).into_par_iter().count();
  280. ///
  281. /// assert_eq!(count, 100);
  282. /// ```
  283. fn count(self) -> Count<Self> {
  284. Count::new(self)
  285. }
  286. /// Applies `operation` to each item of this iterator, producing a new
  287. /// iterator with the results.
  288. ///
  289. /// # Examples
  290. ///
  291. /// ```
  292. /// use rayon::prelude::*;
  293. ///
  294. /// let mut par_iter = (0..5).into_par_iter().map(|x| x * 2);
  295. ///
  296. /// let doubles: Vec<_> = par_iter.collect();
  297. ///
  298. /// assert_eq!(&doubles[..], &[0, 2, 4, 6, 8]);
  299. /// ```
  300. fn map<O, T>(self, operation: O) -> Map<Self, O>
  301. where
  302. O: Fn(Self::Item) -> T + Clone + Send,
  303. T: Send,
  304. {
  305. Map::new(self, operation)
  306. }
  307. /// Applies `operation` to the given `init` value with each item of this
  308. /// iterator, producing a new iterator with the results.
  309. ///
  310. /// The `init` value will be cloned only as needed to be paired with
  311. /// the group of items in each rayon job. It does not require the type
  312. /// to be `Sync`.
  313. ///
  314. /// # Examples
  315. ///
  316. /// ```
  317. /// use std::sync::mpsc::channel;
  318. /// use rayon::prelude::*;
  319. ///
  320. /// let (sender, receiver) = channel();
  321. ///
  322. /// let a: Vec<_> = (0..5)
  323. /// .into_par_iter() // iterating over i32
  324. /// .map_with(sender, |s, x| {
  325. /// s.send(x).unwrap(); // sending i32 values through the channel
  326. /// x // returning i32
  327. /// })
  328. /// .collect(); // collecting the returned values into a vector
  329. ///
  330. /// let mut b: Vec<_> = receiver
  331. /// .iter() // iterating over the values in the channel
  332. /// .collect(); // and collecting them
  333. /// b.sort();
  334. ///
  335. /// assert_eq!(a, b);
  336. /// ```
  337. fn map_with<O, T, S>(self, init: S, operation: O) -> MapWith<Self, S, O>
  338. where
  339. O: Fn(&mut S, Self::Item) -> T + Clone + Send,
  340. S: Send + Clone,
  341. T: Send,
  342. {
  343. MapWith::new(self, init, operation)
  344. }
  345. /// Applies `operation` to a value returned by `init` with each item of this
  346. /// iterator, producing a new iterator with the results.
  347. ///
  348. /// The `init` function will be called only as needed for a value to be
  349. /// paired with the group of items in each rayon job. There is no
  350. /// constraint on that returned type at all!
  351. ///
  352. /// # Examples
  353. ///
  354. /// ```
  355. /// use rand::Rng;
  356. /// use rayon::prelude::*;
  357. ///
  358. /// let a: Vec<_> = (1i32..1_000_000)
  359. /// .into_par_iter()
  360. /// .map_init(
  361. /// || rand::thread_rng(), // get the thread-local RNG
  362. /// |rng, x| if rng.gen() { // randomly negate items
  363. /// -x
  364. /// } else {
  365. /// x
  366. /// },
  367. /// ).collect();
  368. ///
  369. /// // There's a remote chance that this will fail...
  370. /// assert!(a.iter().any(|&x| x < 0));
  371. /// assert!(a.iter().any(|&x| x > 0));
  372. /// ```
  373. fn map_init<O, T, S, U>(self, init: S, operation: O) -> MapInit<Self, S, O>
  374. where
  375. O: Fn(&mut U, Self::Item) -> T + Send,
  376. S: Fn() -> U + Send,
  377. T: Send,
  378. {
  379. MapInit::new(self, init, operation)
  380. }
  381. /// Creates an iterator which clones all of its elements. This may be
  382. /// useful when you have an iterator over `&T`, but you need `T`, and
  383. /// that type implements `Clone`. See also [`copied()`].
  384. ///
  385. /// [`copied()`]: #method.copied
  386. ///
  387. /// # Examples
  388. ///
  389. /// ```
  390. /// use rayon::prelude::*;
  391. ///
  392. /// let a = [1, 2, 3];
  393. ///
  394. /// let v_cloned: Vec<_> = a.par_iter().cloned().collect();
  395. ///
  396. /// // cloned is the same as .map(|&x| x), for integers
  397. /// let v_map: Vec<_> = a.par_iter().map(|&x| x).collect();
  398. ///
  399. /// assert_eq!(v_cloned, vec![1, 2, 3]);
  400. /// assert_eq!(v_map, vec![1, 2, 3]);
  401. /// ```
  402. fn cloned<T>(self) -> Cloned<Self>
  403. where
  404. T: Clone + Send + 'a,
  405. Self: ParallelIterator<'a, Item = &'a T>,
  406. {
  407. Cloned::new(self)
  408. }
  409. /// Creates an iterator which copies all of its elements. This may be
  410. /// useful when you have an iterator over `&T`, but you need `T`, and
  411. /// that type implements `Copy`. See also [`cloned()`].
  412. ///
  413. /// [`cloned()`]: #method.cloned
  414. ///
  415. /// # Examples
  416. ///
  417. /// ```
  418. /// use rayon::prelude::*;
  419. ///
  420. /// let a = [1, 2, 3];
  421. ///
  422. /// let v_copied: Vec<_> = a.par_iter().copied().collect();
  423. ///
  424. /// // copied is the same as .map(|&x| x), for integers
  425. /// let v_map: Vec<_> = a.par_iter().map(|&x| x).collect();
  426. ///
  427. /// assert_eq!(v_copied, vec![1, 2, 3]);
  428. /// assert_eq!(v_map, vec![1, 2, 3]);
  429. /// ```
  430. fn copied<T>(self) -> Copied<Self>
  431. where
  432. T: Copy + Send + 'a,
  433. Self: ParallelIterator<'a, Item = &'a T>,
  434. {
  435. Copied::new(self)
  436. }
  437. /// Applies `operation` to a reference to each item of this iterator,
  438. /// producing a new iterator passing through the original items. This is
  439. /// often useful for debugging to see what's happening in iterator stages.
  440. ///
  441. /// # Examples
  442. ///
  443. /// ```
  444. /// use rayon::prelude::*;
  445. ///
  446. /// let a = [1, 4, 2, 3];
  447. ///
  448. /// // this iterator sequence is complex.
  449. /// let sum = a.par_iter()
  450. /// .cloned()
  451. /// .filter(|&x| x % 2 == 0)
  452. /// .reduce(|| 0, |sum, i| sum + i);
  453. ///
  454. /// println!("{}", sum);
  455. ///
  456. /// // let's add some inspect() calls to investigate what's happening
  457. /// let sum = a.par_iter()
  458. /// .cloned()
  459. /// .inspect(|x| println!("about to filter: {}", x))
  460. /// .filter(|&x| x % 2 == 0)
  461. /// .inspect(|x| println!("made it through filter: {}", x))
  462. /// .reduce(|| 0, |sum, i| sum + i);
  463. ///
  464. /// println!("{}", sum);
  465. /// ```
  466. fn inspect<O>(self, operation: O) -> Inspect<Self, O>
  467. where
  468. O: Fn(&Self::Item) + Clone + Send + 'a,
  469. {
  470. Inspect::new(self, operation)
  471. }
  472. /// Mutates each item of this iterator before yielding it.
  473. ///
  474. /// # Examples
  475. ///
  476. /// ```
  477. /// use rayon::prelude::*;
  478. ///
  479. /// let par_iter = (0..5).into_par_iter().update(|x| {*x *= 2;});
  480. ///
  481. /// let doubles: Vec<_> = par_iter.collect();
  482. ///
  483. /// assert_eq!(&doubles[..], &[0, 2, 4, 6, 8]);
  484. /// ```
  485. fn update<O>(self, operation: O) -> Update<Self, O>
  486. where
  487. O: Fn(&mut Self::Item) + Clone + Send + 'a,
  488. {
  489. Update::new(self, operation)
  490. }
  491. /// Applies `operation` to each item of this iterator, producing a new
  492. /// iterator with only the items that gave `true` results.
  493. ///
  494. /// # Examples
  495. ///
  496. /// ```
  497. /// use rayon::prelude::*;
  498. ///
  499. /// let mut par_iter = (0..10).into_par_iter().filter(|x| x % 2 == 0);
  500. ///
  501. /// let even_numbers: Vec<_> = par_iter.collect();
  502. ///
  503. /// assert_eq!(&even_numbers[..], &[0, 2, 4, 6, 8]);
  504. /// ```
  505. fn filter<O>(self, operation: O) -> Filter<Self, O>
  506. where
  507. O: Fn(&Self::Item) -> bool + Clone + Send + 'a,
  508. {
  509. Filter::new(self, operation)
  510. }
  511. /// Applies `operation` to each item of this iterator to get an `Option`,
  512. /// producing a new iterator with only the items from `Some` results.
  513. ///
  514. /// # Examples
  515. ///
  516. /// ```
  517. /// use rayon::prelude::*;
  518. ///
  519. /// let mut par_iter = (0..10).into_par_iter()
  520. /// .filter_map(|x| {
  521. /// if x % 2 == 0 { Some(x * 3) }
  522. /// else { None }
  523. /// });
  524. ///
  525. /// let even_numbers: Vec<_> = par_iter.collect();
  526. ///
  527. /// assert_eq!(&even_numbers[..], &[0, 6, 12, 18, 24]);
  528. /// ```
  529. fn filter_map<O, S>(self, operation: O) -> FilterMap<Self, O>
  530. where
  531. O: Fn(Self::Item) -> Option<S> + Clone + Send + 'a,
  532. {
  533. FilterMap::new(self, operation)
  534. }
  535. /// Applies `operation` to each item of this iterator to get nested serial iterators,
  536. /// producing a new parallel iterator that flattens these back into one.
  537. ///
  538. /// # `flat_map_iter` versus `flat_map`
  539. ///
  540. /// These two methods are similar but behave slightly differently. With [`flat_map`],
  541. /// each of the nested iterators must be a parallel iterator, and they will be further
  542. /// split up with nested parallelism. With `flat_map_iter`, each nested iterator is a
  543. /// sequential `Iterator`, and we only parallelize _between_ them, while the items
  544. /// produced by each nested iterator are processed sequentially.
  545. ///
  546. /// When choosing between these methods, consider whether nested parallelism suits the
  547. /// potential iterators at hand. If there's little computation involved, or its length
  548. /// is much less than the outer parallel iterator, then it may perform better to avoid
  549. /// the overhead of parallelism, just flattening sequentially with `flat_map_iter`.
  550. /// If there is a lot of computation, potentially outweighing the outer parallel
  551. /// iterator, then the nested parallelism of `flat_map` may be worthwhile.
  552. ///
  553. /// [`flat_map`]: #method.flat_map
  554. ///
  555. /// # Examples
  556. ///
  557. /// ```
  558. /// use rayon::prelude::*;
  559. /// use std::cell::RefCell;
  560. ///
  561. /// let a = [[1, 2], [3, 4], [5, 6], [7, 8]];
  562. ///
  563. /// let par_iter = a.par_iter().flat_map_iter(|a| {
  564. /// // The serial iterator doesn't have to be thread-safe, just its items.
  565. /// let cell_iter = RefCell::new(a.iter().cloned());
  566. /// std::iter::from_fn(move || cell_iter.borrow_mut().next())
  567. /// });
  568. ///
  569. /// let vec: Vec<_> = par_iter.collect();
  570. ///
  571. /// assert_eq!(&vec[..], &[1, 2, 3, 4, 5, 6, 7, 8]);
  572. /// ```
  573. fn flat_map_iter<O, SI>(self, operation: O) -> FlatMapIter<Self, O>
  574. where
  575. O: Fn(Self::Item) -> SI + Clone + Send + 'a,
  576. SI: IntoIterator,
  577. SI::Item: Send,
  578. {
  579. FlatMapIter::new(self, operation)
  580. }
  581. /// An adaptor that flattens serial-iterable `Item`s into one large iterator.
  582. ///
  583. /// See also [`flatten`](#method.flatten) and the analagous comparison of
  584. /// [`flat_map_iter` versus `flat_map`](#flat_map_iter-versus-flat_map).
  585. ///
  586. /// # Examples
  587. ///
  588. /// ```
  589. /// use rayon::prelude::*;
  590. ///
  591. /// let x: Vec<Vec<_>> = vec![vec![1, 2], vec![3, 4]];
  592. /// let iters: Vec<_> = x.into_iter().map(Vec::into_iter).collect();
  593. /// let y: Vec<_> = iters.into_par_iter().flatten_iter().collect();
  594. ///
  595. /// assert_eq!(y, vec![1, 2, 3, 4]);
  596. /// ```
  597. fn flatten_iter(self) -> FlattenIter<Self>
  598. where
  599. Self::Item: IntoIterator + Send,
  600. <Self::Item as IntoIterator>::Item: Send,
  601. {
  602. FlattenIter::new(self)
  603. }
  604. /// Reduces the items in the iterator into one item using `operation`.
  605. /// The argument `identity` should be a closure that can produce
  606. /// "identity" value which may be inserted into the sequence as
  607. /// needed to create opportunities for parallel execution. So, for
  608. /// example, if you are doing a summation, then `identity()` ought
  609. /// to produce something that represents the zero for your type
  610. /// (but consider just calling `sum()` in that case).
  611. ///
  612. /// # Examples
  613. ///
  614. /// ```
  615. /// // Iterate over a sequence of pairs `(x0, y0), ..., (xN, yN)`
  616. /// // and use reduce to compute one pair `(x0 + ... + xN, y0 + ... + yN)`
  617. /// // where the first/second elements are summed separately.
  618. /// use rayon::prelude::*;
  619. /// let sums = [(0, 1), (5, 6), (16, 2), (8, 9)]
  620. /// .par_iter() // iterating over &(i32, i32)
  621. /// .cloned() // iterating over (i32, i32)
  622. /// .reduce(|| (0, 0), // the "identity" is 0 in both columns
  623. /// |a, b| (a.0 + b.0, a.1 + b.1));
  624. /// assert_eq!(sums, (0 + 5 + 16 + 8, 1 + 6 + 2 + 9));
  625. /// ```
  626. ///
  627. /// **Note:** unlike a sequential `fold` operation, the order in
  628. /// which `operation` will be applied to reduce the result is not fully
  629. /// specified. So `operation` should be [associative] or else the results
  630. /// will be non-deterministic. And of course `identity()` should
  631. /// produce a true identity.
  632. ///
  633. /// [associative]: https://en.wikipedia.org/wiki/Associative_property
  634. fn reduce<S, O>(self, identity: S, operation: O) -> Reduce<Self, S, O>
  635. where
  636. S: Fn() -> Self::Item + Clone + Send + 'a,
  637. O: Fn(Self::Item, Self::Item) -> Self::Item + Clone + Send + 'a,
  638. {
  639. Reduce::new(self, identity, operation)
  640. }
  641. /// Reduces the items in the iterator into one item using `operation`.
  642. /// If the iterator is empty, `None` is returned; otherwise,
  643. /// `Some` is returned.
  644. ///
  645. /// This version of `reduce` is simple but somewhat less
  646. /// efficient. If possible, it is better to call `reduce()`, which
  647. /// requires an identity element.
  648. ///
  649. /// # Examples
  650. ///
  651. /// ```
  652. /// use rayon::prelude::*;
  653. /// let sums = [(0, 1), (5, 6), (16, 2), (8, 9)]
  654. /// .par_iter() // iterating over &(i32, i32)
  655. /// .cloned() // iterating over (i32, i32)
  656. /// .reduce_with(|a, b| (a.0 + b.0, a.1 + b.1))
  657. /// .unwrap();
  658. /// assert_eq!(sums, (0 + 5 + 16 + 8, 1 + 6 + 2 + 9));
  659. /// ```
  660. ///
  661. /// **Note:** unlike a sequential `fold` operation, the order in
  662. /// which `operation` will be applied to reduce the result is not fully
  663. /// specified. So `operation` should be [associative] or else the results
  664. /// will be non-deterministic.
  665. ///
  666. /// [associative]: https://en.wikipedia.org/wiki/Associative_property
  667. fn reduce_with<O>(self, operation: O) -> ReduceWith<Self, O>
  668. where
  669. O: Fn(Self::Item, Self::Item) -> Self::Item + Clone + Send + 'a,
  670. {
  671. ReduceWith::new(self, operation)
  672. }
  673. /// Reduces the items in the iterator into one item using a fallible `operation`.
  674. /// The `identity` argument is used the same way as in [`reduce()`].
  675. ///
  676. /// [`reduce()`]: #method.reduce
  677. ///
  678. /// If a `Result::Err` or `Option::None` item is found, or if `operation` reduces
  679. /// to one, we will attempt to stop processing the rest of the items in the
  680. /// iterator as soon as possible, and we will return that terminating value.
  681. /// Otherwise, we will return the final reduced `Result::Ok(T)` or
  682. /// `Option::Some(T)`. If there are multiple errors in parallel, it is not
  683. /// specified which will be returned.
  684. ///
  685. /// # Examples
  686. ///
  687. /// ```
  688. /// use rayon::prelude::*;
  689. ///
  690. /// // Compute the sum of squares, being careful about overflow.
  691. /// fn sum_squares<I: IntoParallelIterator<Item = i32>>(iter: I) -> Option<i32> {
  692. /// iter.into_par_iter()
  693. /// .map(|i| i.checked_mul(i)) // square each item,
  694. /// .try_reduce(|| 0, i32::checked_add) // and add them up!
  695. /// }
  696. /// assert_eq!(sum_squares(0..5), Some(0 + 1 + 4 + 9 + 16));
  697. ///
  698. /// // The sum might overflow
  699. /// assert_eq!(sum_squares(0..10_000), None);
  700. ///
  701. /// // Or the squares might overflow before it even reaches `try_reduce`
  702. /// assert_eq!(sum_squares(1_000_000..1_000_001), None);
  703. /// ```
  704. fn try_reduce<S, O, T>(self, identity: S, operation: O) -> TryReduce<Self, S, O>
  705. where
  706. Self::Item: Try<Ok = T>,
  707. S: Fn() -> T + Clone + Send + 'a,
  708. O: Fn(T, T) -> Self::Item + Clone + Send + 'a,
  709. {
  710. TryReduce::new(self, identity, operation)
  711. }
  712. /// Reduces the items in the iterator into one item using a fallible `operation`.
  713. ///
  714. /// Like [`reduce_with()`], if the iterator is empty, `None` is returned;
  715. /// otherwise, `Some` is returned. Beyond that, it behaves like
  716. /// [`try_reduce()`] for handling `Err`/`None`.
  717. ///
  718. /// [`reduce_with()`]: #method.reduce_with
  719. /// [`try_reduce()`]: #method.try_reduce
  720. ///
  721. /// For instance, with `Option` items, the return value may be:
  722. /// - `None`, the iterator was empty
  723. /// - `Some(None)`, we stopped after encountering `None`.
  724. /// - `Some(Some(x))`, the entire iterator reduced to `x`.
  725. ///
  726. /// With `Result` items, the nesting is more obvious:
  727. /// - `None`, the iterator was empty
  728. /// - `Some(Err(e))`, we stopped after encountering an error `e`.
  729. /// - `Some(Ok(x))`, the entire iterator reduced to `x`.
  730. ///
  731. /// # Examples
  732. ///
  733. /// ```
  734. /// use rayon::prelude::*;
  735. ///
  736. /// let files = ["/dev/null", "/does/not/exist"];
  737. ///
  738. /// // Find the biggest file
  739. /// files.into_par_iter()
  740. /// .map(|path| std::fs::metadata(path).map(|m| (path, m.len())))
  741. /// .try_reduce_with(|a, b| {
  742. /// Ok(if a.1 >= b.1 { a } else { b })
  743. /// })
  744. /// .expect("Some value, since the iterator is not empty")
  745. /// .expect_err("not found");
  746. /// ```
  747. fn try_reduce_with<O, T>(self, operation: O) -> TryReduceWith<Self, O>
  748. where
  749. Self::Item: Try<Ok = T>,
  750. O: Fn(T, T) -> Self::Item + Clone + Send + 'a,
  751. {
  752. TryReduceWith::new(self, operation)
  753. }
  754. /// Parallel fold is similar to sequential fold except that the
  755. /// sequence of items may be subdivided before it is
  756. /// folded. Consider a list of numbers like `22 3 77 89 46`. If
  757. /// you used sequential fold to add them (`fold(0, |a,b| a+b)`,
  758. /// you would wind up first adding 0 + 22, then 22 + 3, then 25 +
  759. /// 77, and so forth. The **parallel fold** works similarly except
  760. /// that it first breaks up your list into sublists, and hence
  761. /// instead of yielding up a single sum at the end, it yields up
  762. /// multiple sums. The number of results is nondeterministic, as
  763. /// is the point where the breaks occur.
  764. ///
  765. /// So if did the same parallel fold (`fold(0, |a,b| a+b)`) on
  766. /// our example list, we might wind up with a sequence of two numbers,
  767. /// like so:
  768. ///
  769. /// ```notrust
  770. /// 22 3 77 89 46
  771. /// | |
  772. /// 102 135
  773. /// ```
  774. ///
  775. /// Or perhaps these three numbers:
  776. ///
  777. /// ```notrust
  778. /// 22 3 77 89 46
  779. /// | | |
  780. /// 102 89 46
  781. /// ```
  782. ///
  783. /// In general, Rayon will attempt to find good breaking points
  784. /// that keep all of your cores busy.
  785. ///
  786. /// ### Fold versus reduce
  787. ///
  788. /// The `fold()` and `reduce()` methods each take an identity element
  789. /// and a combining function, but they operate rather differently.
  790. ///
  791. /// `reduce()` requires that the identity function has the same
  792. /// type as the things you are iterating over, and it fully
  793. /// reduces the list of items into a single item. So, for example,
  794. /// imagine we are iterating over a list of bytes `bytes: [128_u8,
  795. /// 64_u8, 64_u8]`. If we used `bytes.reduce(|| 0_u8, |a: u8, b:
  796. /// u8| a + b)`, we would get an overflow. This is because `0`,
  797. /// `a`, and `b` here are all bytes, just like the numbers in the
  798. /// list (I wrote the types explicitly above, but those are the
  799. /// only types you can use). To avoid the overflow, we would need
  800. /// to do something like `bytes.map(|b| b as u32).reduce(|| 0, |a,
  801. /// b| a + b)`, in which case our result would be `256`.
  802. ///
  803. /// In contrast, with `fold()`, the identity function does not
  804. /// have to have the same type as the things you are iterating
  805. /// over, and you potentially get back many results. So, if we
  806. /// continue with the `bytes` example from the previous paragraph,
  807. /// we could do `bytes.fold(|| 0_u32, |a, b| a + (b as u32))` to
  808. /// convert our bytes into `u32`. And of course we might not get
  809. /// back a single sum.
  810. ///
  811. /// There is a more subtle distinction as well, though it's
  812. /// actually implied by the above points. When you use `reduce()`,
  813. /// your reduction function is sometimes called with values that
  814. /// were never part of your original parallel iterator (for
  815. /// example, both the left and right might be a partial sum). With
  816. /// `fold()`, in contrast, the left value in the fold function is
  817. /// always the accumulator, and the right value is always from
  818. /// your original sequence.
  819. ///
  820. /// ### Fold vs Map/Reduce
  821. ///
  822. /// Fold makes sense if you have some operation where it is
  823. /// cheaper to create groups of elements at a time. For example,
  824. /// imagine collecting characters into a string. If you were going
  825. /// to use map/reduce, you might try this:
  826. ///
  827. /// ```
  828. /// use rayon::prelude::*;
  829. ///
  830. /// let s =
  831. /// ['a', 'b', 'c', 'd', 'e']
  832. /// .par_iter()
  833. /// .map(|c: &char| format!("{}", c))
  834. /// .reduce(|| String::new(),
  835. /// |mut a: String, b: String| { a.push_str(&b); a });
  836. ///
  837. /// assert_eq!(s, "abcde");
  838. /// ```
  839. ///
  840. /// Because reduce produces the same type of element as its input,
  841. /// you have to first map each character into a string, and then
  842. /// you can reduce them. This means we create one string per
  843. /// element in our iterator -- not so great. Using `fold`, we can
  844. /// do this instead:
  845. ///
  846. /// ```
  847. /// use rayon::prelude::*;
  848. ///
  849. /// let s =
  850. /// ['a', 'b', 'c', 'd', 'e']
  851. /// .par_iter()
  852. /// .fold(|| String::new(),
  853. /// |mut s: String, c: &char| { s.push(*c); s })
  854. /// .reduce(|| String::new(),
  855. /// |mut a: String, b: String| { a.push_str(&b); a });
  856. ///
  857. /// assert_eq!(s, "abcde");
  858. /// ```
  859. ///
  860. /// Now `fold` will process groups of our characters at a time,
  861. /// and we only make one string per group. We should wind up with
  862. /// some small-ish number of strings roughly proportional to the
  863. /// number of CPUs you have (it will ultimately depend on how busy
  864. /// your processors are). Note that we still need to do a reduce
  865. /// afterwards to combine those groups of strings into a single
  866. /// string.
  867. ///
  868. /// You could use a similar trick to save partial results (e.g., a
  869. /// cache) or something similar.
  870. ///
  871. /// ### Combining fold with other operations
  872. ///
  873. /// You can combine `fold` with `reduce` if you want to produce a
  874. /// single value. This is then roughly equivalent to a map/reduce
  875. /// combination in effect:
  876. ///
  877. /// ```
  878. /// use rayon::prelude::*;
  879. ///
  880. /// let bytes = 0..22_u8;
  881. /// let sum = bytes.into_par_iter()
  882. /// .fold(|| 0_u32, |a: u32, b: u8| a + (b as u32))
  883. /// .sum::<u32>();
  884. ///
  885. /// assert_eq!(sum, (0..22).sum()); // compare to sequential
  886. /// ```
  887. fn fold<S, O, U>(self, init: S, operation: O) -> Fold<Self, S, O>
  888. where
  889. S: Fn() -> U + Clone + Send + 'a,
  890. O: Fn(U, Self::Item) -> U + Clone + Send + 'a,
  891. U: Send,
  892. {
  893. Fold::new(self, init, operation)
  894. }
  895. /// Applies `operation` to the given `init` value with each item of this
  896. /// iterator, finally producing the value for further use.
  897. ///
  898. /// This works essentially like `fold(|| init.clone(), operation)`, except
  899. /// it doesn't require the `init` type to be `Sync`, nor any other form
  900. /// of added synchronization.
  901. ///
  902. /// # Examples
  903. ///
  904. /// ```
  905. /// use rayon::prelude::*;
  906. ///
  907. /// let bytes = 0..22_u8;
  908. /// let sum = bytes.into_par_iter()
  909. /// .fold_with(0_u32, |a: u32, b: u8| a + (b as u32))
  910. /// .sum::<u32>();
  911. ///
  912. /// assert_eq!(sum, (0..22).sum()); // compare to sequential
  913. /// ```
  914. fn fold_with<U, O>(self, init: U, operation: O) -> FoldWith<Self, U, O>
  915. where
  916. U: Clone + Send + 'a,
  917. O: Fn(U, Self::Item) -> U + Clone + Send + 'a,
  918. {
  919. FoldWith::new(self, init, operation)
  920. }
  921. /// Performs a fallible parallel fold.
  922. ///
  923. /// This is a variation of [`fold()`] for operations which can fail with
  924. /// `Option::None` or `Result::Err`. The first such failure stops
  925. /// processing the local set of items, without affecting other folds in the
  926. /// iterator's subdivisions.
  927. ///
  928. /// Often, `try_fold()` will be followed by [`try_reduce()`]
  929. /// for a final reduction and global short-circuiting effect.
  930. ///
  931. /// [`fold()`]: #method.fold
  932. /// [`try_reduce()`]: #method.try_reduce
  933. ///
  934. /// # Examples
  935. ///
  936. /// ```
  937. /// use rayon::prelude::*;
  938. ///
  939. /// let bytes = 0..22_u8;
  940. /// let sum = bytes.into_par_iter()
  941. /// .try_fold(|| 0_u32, |a: u32, b: u8| a.checked_add(b as u32))
  942. /// .try_reduce(|| 0, u32::checked_add);
  943. ///
  944. /// assert_eq!(sum, Some((0..22).sum())); // compare to sequential
  945. /// ```
  946. fn try_fold<S, O, U, T>(self, init: S, operation: O) -> TryFold<Self, S, O, T>
  947. where
  948. S: Fn() -> U + Clone + Send + 'a,
  949. O: Fn(U, Self::Item) -> T + Clone + Send + 'a,
  950. T: Try<Ok = U> + Send,
  951. {
  952. TryFold::new(self, init, operation)
  953. }
  954. /// Performs a fallible parallel fold with a cloneable `init` value.
  955. ///
  956. /// This combines the `init` semantics of [`fold_with()`] and the failure
  957. /// semantics of [`try_fold()`].
  958. ///
  959. /// [`fold_with()`]: #method.fold_with
  960. /// [`try_fold()`]: #method.try_fold
  961. ///
  962. /// ```
  963. /// use rayon::prelude::*;
  964. ///
  965. /// let bytes = 0..22_u8;
  966. /// let sum = bytes.into_par_iter()
  967. /// .try_fold_with(0_u32, |a: u32, b: u8| a.checked_add(b as u32))
  968. /// .try_reduce(|| 0, u32::checked_add);
  969. ///
  970. /// assert_eq!(sum, Some((0..22).sum())); // compare to sequential
  971. /// ```
  972. fn try_fold_with<U, O, T>(self, init: U, operation: O) -> TryFoldWith<Self, U, O, T>
  973. where
  974. U: Clone + Send + 'a,
  975. O: Fn(U, Self::Item) -> T + Clone + Send + 'a,
  976. T: Try<Ok = U>,
  977. {
  978. TryFoldWith::new(self, init, operation)
  979. }
  980. /// Sums up the items in the iterator.
  981. ///
  982. /// Note that the order in items will be reduced is not specified,
  983. /// so if the `+` operator is not truly [associative] \(as is the
  984. /// case for floating point numbers), then the results are not
  985. /// fully deterministic.
  986. ///
  987. /// [associative]: https://en.wikipedia.org/wiki/Associative_property
  988. ///
  989. /// Basically equivalent to `self.reduce(|| 0, |a, b| a + b)`,
  990. /// except that the type of `0` and the `+` operation may vary
  991. /// depending on the type of value being produced.
  992. ///
  993. /// # Examples
  994. ///
  995. /// ```
  996. /// use rayon::prelude::*;
  997. ///
  998. /// let a = [1, 5, 7];
  999. ///
  1000. /// let sum: i32 = a.par_iter().sum();
  1001. ///
  1002. /// assert_eq!(sum, 13);
  1003. /// ```
  1004. fn sum<S>(self) -> Sum<Self, S>
  1005. where
  1006. S: std::iter::Sum<Self::Item> + std::iter::Sum<S> + Send,
  1007. {
  1008. Sum::new(self)
  1009. }
  1010. /// Multiplies all the items in the iterator.
  1011. ///
  1012. /// Note that the order in items will be reduced is not specified,
  1013. /// so if the `*` operator is not truly [associative] \(as is the
  1014. /// case for floating point numbers), then the results are not
  1015. /// fully deterministic.
  1016. ///
  1017. /// [associative]: https://en.wikipedia.org/wiki/Associative_property
  1018. ///
  1019. /// Basically equivalent to `self.reduce(|| 1, |a, b| a * b)`,
  1020. /// except that the type of `1` and the `*` operation may vary
  1021. /// depending on the type of value being produced.
  1022. ///
  1023. /// # Examples
  1024. ///
  1025. /// ```
  1026. /// use rayon::prelude::*;
  1027. ///
  1028. /// fn factorial(n: u32) -> u32 {
  1029. /// (1..n+1).into_par_iter().product()
  1030. /// }
  1031. ///
  1032. /// assert_eq!(factorial(0), 1);
  1033. /// assert_eq!(factorial(1), 1);
  1034. /// assert_eq!(factorial(5), 120);
  1035. /// ```
  1036. fn product<P>(self) -> Product<Self, P>
  1037. where
  1038. P: std::iter::Product<Self::Item> + std::iter::Product<P> + Send,
  1039. {
  1040. Product::new(self)
  1041. }
  1042. /// Computes the minimum of all the items in the iterator. If the
  1043. /// iterator is empty, `None` is returned; otherwise, `Some(min)`
  1044. /// is returned.
  1045. ///
  1046. /// Note that the order in which the items will be reduced is not
  1047. /// specified, so if the `Ord` impl is not truly associative, then
  1048. /// the results are not deterministic.
  1049. ///
  1050. /// Basically equivalent to `self.reduce_with(|a, b| cmp::min(a, b))`.
  1051. ///
  1052. /// # Examples
  1053. ///
  1054. /// ```
  1055. /// use rayon::prelude::*;
  1056. ///
  1057. /// let a = [45, 74, 32];
  1058. ///
  1059. /// assert_eq!(a.par_iter().min(), Some(&32));
  1060. ///
  1061. /// let b: [i32; 0] = [];
  1062. ///
  1063. /// assert_eq!(b.par_iter().min(), None);
  1064. /// ```
  1065. fn min(self) -> Min<Self>
  1066. where
  1067. Self::Item: Ord,
  1068. {
  1069. Min::new(self)
  1070. }
  1071. /// Computes the minimum of all the items in the iterator with respect to
  1072. /// the given comparison function. If the iterator is empty, `None` is
  1073. /// returned; otherwise, `Some(min)` is returned.
  1074. ///
  1075. /// Note that the order in which the items will be reduced is not
  1076. /// specified, so if the comparison function is not associative, then
  1077. /// the results are not deterministic.
  1078. ///
  1079. /// # Examples
  1080. ///
  1081. /// ```
  1082. /// use rayon::prelude::*;
  1083. ///
  1084. /// let a = [-3_i32, 77, 53, 240, -1];
  1085. ///
  1086. /// assert_eq!(a.par_iter().min_by(|x, y| x.cmp(y)), Some(&-3));
  1087. /// ```
  1088. fn min_by<O>(self, operation: O) -> MinBy<Self, O>
  1089. where
  1090. O: Fn(&Self::Item, &Self::Item) -> Ordering + Clone + Send + Sync + 'a,
  1091. {
  1092. MinBy::new(self, operation)
  1093. }
  1094. /// Computes the item that yields the minimum value for the given
  1095. /// function. If the iterator is empty, `None` is returned;
  1096. /// otherwise, `Some(item)` is returned.
  1097. ///
  1098. /// Note that the order in which the items will be reduced is not
  1099. /// specified, so if the `Ord` impl is not truly associative, then
  1100. /// the results are not deterministic.
  1101. ///
  1102. /// # Examples
  1103. ///
  1104. /// ```
  1105. /// use rayon::prelude::*;
  1106. ///
  1107. /// let a = [-3_i32, 34, 2, 5, -10, -3, -23];
  1108. ///
  1109. /// assert_eq!(a.par_iter().min_by_key(|x| x.abs()), Some(&2));
  1110. /// ```
  1111. fn min_by_key<O, K>(self, operation: O) -> MinByKey<Self, O>
  1112. where
  1113. O: Fn(&Self::Item) -> K + Clone + Send + 'a,
  1114. K: Ord + Send,
  1115. {
  1116. MinByKey::new(self, operation)
  1117. }
  1118. /// Computes the maximum of all the items in the iterator. If the
  1119. /// iterator is empty, `None` is returned; otherwise, `Some(max)`
  1120. /// is returned.
  1121. ///
  1122. /// Note that the order in which the items will be reduced is not
  1123. /// specified, so if the `Ord` impl is not truly associative, then
  1124. /// the results are not deterministic.
  1125. ///
  1126. /// Basically equivalent to `self.reduce_with(|a, b| cmp::max(a, b))`.
  1127. ///
  1128. /// # Examples
  1129. ///
  1130. /// ```
  1131. /// use rayon::prelude::*;
  1132. ///
  1133. /// let a = [45, 74, 32];
  1134. ///
  1135. /// assert_eq!(a.par_iter().max(), Some(&74));
  1136. ///
  1137. /// let b: [i32; 0] = [];
  1138. ///
  1139. /// assert_eq!(b.par_iter().max(), None);
  1140. /// ```
  1141. fn max(self) -> Max<Self>
  1142. where
  1143. Self::Item: Ord,
  1144. {
  1145. Max::new(self)
  1146. }
  1147. /// Computes the maximum of all the items in the iterator with respect to
  1148. /// the given comparison function. If the iterator is empty, `None` is
  1149. /// returned; otherwise, `Some(min)` is returned.
  1150. ///
  1151. /// Note that the order in which the items will be reduced is not
  1152. /// specified, so if the comparison function is not associative, then
  1153. /// the results are not deterministic.
  1154. ///
  1155. /// # Examples
  1156. ///
  1157. /// ```
  1158. /// use rayon::prelude::*;
  1159. ///
  1160. /// let a = [-3_i32, 77, 53, 240, -1];
  1161. ///
  1162. /// assert_eq!(a.par_iter().max_by(|x, y| x.abs().cmp(&y.abs())), Some(&240));
  1163. /// ```
  1164. fn max_by<O>(self, operation: O) -> MaxBy<Self, O>
  1165. where
  1166. O: Fn(&Self::Item, &Self::Item) -> Ordering + Clone + Send + Sync + 'a,
  1167. {
  1168. MaxBy::new(self, operation)
  1169. }
  1170. /// Computes the item that yields the maximum value for the given
  1171. /// function. If the iterator is empty, `None` is returned;
  1172. /// otherwise, `Some(item)` is returned.
  1173. ///
  1174. /// Note that the order in which the items will be reduced is not
  1175. /// specified, so if the `Ord` impl is not truly associative, then
  1176. /// the results are not deterministic.
  1177. ///
  1178. /// # Examples
  1179. ///
  1180. /// ```
  1181. /// use rayon::prelude::*;
  1182. ///
  1183. /// let a = [-3_i32, 34, 2, 5, -10, -3, -23];
  1184. ///
  1185. /// assert_eq!(a.par_iter().max_by_key(|x| x.abs()), Some(&34));
  1186. /// ```
  1187. fn max_by_key<O, K>(self, operation: O) -> MaxByKey<Self, O>
  1188. where
  1189. O: Fn(&Self::Item) -> K + Clone + Send + 'a,
  1190. K: Ord + Send,
  1191. {
  1192. MaxByKey::new(self, operation)
  1193. }
  1194. /// Takes two iterators and creates a new iterator over both.
  1195. ///
  1196. /// # Examples
  1197. ///
  1198. /// ```
  1199. /// use rayon::prelude::*;
  1200. ///
  1201. /// let a = [0, 1, 2];
  1202. /// let b = [9, 8, 7];
  1203. ///
  1204. /// let par_iter = a.par_iter().chain(b.par_iter());
  1205. ///
  1206. /// let chained: Vec<_> = par_iter.cloned().collect();
  1207. ///
  1208. /// assert_eq!(&chained[..], &[0, 1, 2, 9, 8, 7]);
  1209. /// ```
  1210. fn chain<C>(self, chain: C) -> Chain<Self, C::Iter>
  1211. where
  1212. C: IntoParallelIterator<'a, Item = Self::Item>,
  1213. {
  1214. Chain::new(self, chain.into_par_iter())
  1215. }
  1216. /// Searches for **some** item in the parallel iterator that
  1217. /// matches the given operation and returns it. This operation
  1218. /// is similar to [`find` on sequential iterators][find] but
  1219. /// the item returned may not be the **first** one in the parallel
  1220. /// sequence which matches, since we search the entire sequence in parallel.
  1221. ///
  1222. /// Once a match is found, we will attempt to stop processing
  1223. /// the rest of the items in the iterator as soon as possible
  1224. /// (just as `find` stops iterating once a match is found).
  1225. ///
  1226. /// [find]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find
  1227. ///
  1228. /// # Examples
  1229. ///
  1230. /// ```
  1231. /// use rayon::prelude::*;
  1232. ///
  1233. /// let a = [1, 2, 3, 3];
  1234. ///
  1235. /// assert_eq!(a.par_iter().find_any(|&&x| x == 3), Some(&3));
  1236. ///
  1237. /// assert_eq!(a.par_iter().find_any(|&&x| x == 100), None);
  1238. /// ```
  1239. fn find_any<O>(self, operation: O) -> Find<Self, O>
  1240. where
  1241. O: Fn(&Self::Item) -> bool + Clone + Send + 'a,
  1242. {
  1243. Find::new(self, operation, FindMatch::Any)
  1244. }
  1245. /// Searches for the sequentially **first** item in the parallel iterator
  1246. /// that matches the given operation and returns it.
  1247. ///
  1248. /// Once a match is found, all attempts to the right of the match
  1249. /// will be stopped, while attempts to the left must continue in case
  1250. /// an earlier match is found.
  1251. ///
  1252. /// Note that not all parallel iterators have a useful order, much like
  1253. /// sequential `HashMap` iteration, so "first" may be nebulous. If you
  1254. /// just want the first match that discovered anywhere in the iterator,
  1255. /// `find_any` is a better choice.
  1256. ///
  1257. /// # Examples
  1258. ///
  1259. /// ```
  1260. /// use rayon::prelude::*;
  1261. ///
  1262. /// let a = [1, 2, 3, 3];
  1263. ///
  1264. /// assert_eq!(a.par_iter().find_first(|&&x| x == 3), Some(&3));
  1265. ///
  1266. /// assert_eq!(a.par_iter().find_first(|&&x| x == 100), None);
  1267. /// ```
  1268. fn find_first<O>(self, operation: O) -> Find<Self, O>
  1269. where
  1270. O: Fn(&Self::Item) -> bool + Clone + Send + 'a,
  1271. {
  1272. Find::new(self, operation, FindMatch::First)
  1273. }
  1274. /// Searches for the sequentially **last** item in the parallel iterator
  1275. /// that matches the given operation and returns it.
  1276. ///
  1277. /// Once a match is found, all attempts to the left of the match
  1278. /// will be stopped, while attempts to the right must continue in case
  1279. /// a later match is found.
  1280. ///
  1281. /// Note that not all parallel iterators have a useful order, much like
  1282. /// sequential `HashMap` iteration, so "last" may be nebulous. When the
  1283. /// order doesn't actually matter to you, `find_any` is a better choice.
  1284. ///
  1285. /// # Examples
  1286. ///
  1287. /// ```
  1288. /// use rayon::prelude::*;
  1289. ///
  1290. /// let a = [1, 2, 3, 3];
  1291. ///
  1292. /// assert_eq!(a.par_iter().find_last(|&&x| x == 3), Some(&3));
  1293. ///
  1294. /// assert_eq!(a.par_iter().find_last(|&&x| x == 100), None);
  1295. /// ```
  1296. fn find_last<O>(self, operation: O) -> Find<Self, O>
  1297. where
  1298. O: Fn(&Self::Item) -> bool + Clone + Send + 'a,
  1299. {
  1300. Find::new(self, operation, FindMatch::Last)
  1301. }
  1302. /// Applies the given operation to the items in the parallel iterator
  1303. /// and returns **any** non-None result of the map operation.
  1304. ///
  1305. /// Once a non-None value is produced from the map operation, we will
  1306. /// attempt to stop processing the rest of the items in the iterator
  1307. /// as soon as possible.
  1308. ///
  1309. /// Note that this method only returns **some** item in the parallel
  1310. /// iterator that is not None from the map operation. The item returned
  1311. /// may not be the **first** non-None value produced in the parallel
  1312. /// sequence, since the entire sequence is mapped over in parallel.
  1313. ///
  1314. /// # Examples
  1315. ///
  1316. /// ```
  1317. /// use rayon::prelude::*;
  1318. ///
  1319. /// let c = ["lol", "NaN", "5", "5"];
  1320. ///
  1321. /// let found_number = c.par_iter().find_map_any(|s| s.parse().ok());
  1322. ///
  1323. /// assert_eq!(found_number, Some(5));
  1324. /// ```
  1325. fn find_map_any<O, T>(self, operation: O) -> FindMap<Self, O>
  1326. where
  1327. O: Fn(Self::Item) -> Option<T> + Clone + Send + 'a,
  1328. T: Send,
  1329. {
  1330. FindMap::new(self, operation, FindMatch::Any)
  1331. }
  1332. /// Applies the given operation to the items in the parallel iterator and
  1333. /// returns the sequentially **first** non-None result of the map operation.
  1334. ///
  1335. /// Once a non-None value is produced from the map operation, all attempts
  1336. /// to the right of the match will be stopped, while attempts to the left
  1337. /// must continue in case an earlier match is found.
  1338. ///
  1339. /// Note that not all parallel iterators have a useful order, much like
  1340. /// sequential `HashMap` iteration, so "first" may be nebulous. If you
  1341. /// just want the first non-None value discovered anywhere in the iterator,
  1342. /// `find_map_any` is a better choice.
  1343. ///
  1344. /// # Examples
  1345. ///
  1346. /// ```
  1347. /// use rayon::prelude::*;
  1348. ///
  1349. /// let c = ["lol", "NaN", "2", "5"];
  1350. ///
  1351. /// let first_number = c.par_iter().find_map_first(|s| s.parse().ok());
  1352. ///
  1353. /// assert_eq!(first_number, Some(2));
  1354. /// ```
  1355. fn find_map_first<O, T>(self, operation: O) -> FindMap<Self, O>
  1356. where
  1357. O: Fn(Self::Item) -> Option<T> + Clone + Send + 'a,
  1358. T: Send,
  1359. {
  1360. FindMap::new(self, operation, FindMatch::First)
  1361. }
  1362. /// Applies the given operation to the items in the parallel iterator and
  1363. /// returns the sequentially **last** non-None result of the map operation.
  1364. ///
  1365. /// Once a non-None value is produced from the map operation, all attempts
  1366. /// to the left of the match will be stopped, while attempts to the right
  1367. /// must continue in case a later match is found.
  1368. ///
  1369. /// Note that not all parallel iterators have a useful order, much like
  1370. /// sequential `HashMap` iteration, so "first" may be nebulous. If you
  1371. /// just want the first non-None value discovered anywhere in the iterator,
  1372. /// `find_map_any` is a better choice.
  1373. ///
  1374. /// # Examples
  1375. ///
  1376. /// ```
  1377. /// use rayon::prelude::*;
  1378. ///
  1379. /// let c = ["lol", "NaN", "2", "5"];
  1380. ///
  1381. /// let last_number = c.par_iter().find_map_last(|s| s.parse().ok());
  1382. ///
  1383. /// assert_eq!(last_number, Some(5));
  1384. /// ```
  1385. fn find_map_last<O, T>(self, operation: O) -> FindMap<Self, O>
  1386. where
  1387. O: Fn(Self::Item) -> Option<T> + Clone + Send + 'a,
  1388. T: Send,
  1389. {
  1390. FindMap::new(self, operation, FindMatch::Last)
  1391. }
  1392. /// Searches for **some** item in the parallel iterator that
  1393. /// matches the given operation, and if so returns true. Once
  1394. /// a match is found, we'll attempt to stop process the rest
  1395. /// of the items. Proving that there's no match, returning false,
  1396. /// does require visiting every item.
  1397. ///
  1398. /// # Examples
  1399. ///
  1400. /// ```
  1401. /// use rayon::prelude::*;
  1402. ///
  1403. /// let a = [0, 12, 3, 4, 0, 23, 0];
  1404. ///
  1405. /// let is_valid = a.par_iter().any(|&x| x > 10);
  1406. ///
  1407. /// assert!(is_valid);
  1408. /// ```
  1409. fn any<O>(self, operation: O) -> Any<Self, O>
  1410. where
  1411. O: Fn(Self::Item) -> bool + Clone + Send + 'a,
  1412. {
  1413. Any::new(self, operation)
  1414. }
  1415. /// Tests that every item in the parallel iterator matches the given
  1416. /// operation, and if so returns true. If a counter-example is found,
  1417. /// we'll attempt to stop processing more items, then return false.
  1418. ///
  1419. /// # Examples
  1420. ///
  1421. /// ```
  1422. /// use rayon::prelude::*;
  1423. ///
  1424. /// let a = [0, 12, 3, 4, 0, 23, 0];
  1425. ///
  1426. /// let is_valid = a.par_iter().all(|&x| x > 10);
  1427. ///
  1428. /// assert!(!is_valid);
  1429. /// ```
  1430. fn all<O>(self, operation: O) -> All<Self, O>
  1431. where
  1432. O: Fn(Self::Item) -> bool + Clone + Send + 'a,
  1433. {
  1434. All::new(self, operation)
  1435. }
  1436. /// Creates an iterator over the `Some` items of this iterator, halting
  1437. /// as soon as any `None` is found.
  1438. ///
  1439. /// # Examples
  1440. ///
  1441. /// ```
  1442. /// use rayon::prelude::*;
  1443. /// use std::sync::atomic::{AtomicUsize, Ordering};
  1444. ///
  1445. /// let counter = AtomicUsize::new(0);
  1446. /// let value = (0_i32..2048)
  1447. /// .into_par_iter()
  1448. /// .map(|x| {
  1449. /// counter.fetch_add(1, Ordering::SeqCst);
  1450. /// if x < 1024 { Some(x) } else { None }
  1451. /// })
  1452. /// .while_some()
  1453. /// .max();
  1454. ///
  1455. /// assert!(value = Some(1023));
  1456. /// assert!(counter.load(Ordering::SeqCst) < 2048); // should not have visited every single one
  1457. /// ```
  1458. fn while_some<T>(self) -> WhileSome<Self>
  1459. where
  1460. Self: ParallelIterator<'a, Item = Option<T>>,
  1461. T: Send + 'a,
  1462. {
  1463. WhileSome::new(self)
  1464. }
  1465. /// Wraps an iterator with a fuse in case of panics, to halt all threads
  1466. /// as soon as possible.
  1467. ///
  1468. /// Panics within parallel iterators are always propagated to the caller,
  1469. /// but they don't always halt the rest of the iterator right away, due to
  1470. /// the internal semantics of [`join`]. This adaptor makes a greater effort
  1471. /// to stop processing other items sooner, with the cost of additional
  1472. /// synchronization overhead, which may also inhibit some optimizations.
  1473. ///
  1474. /// [`join`]: ../fn.join.html#panics
  1475. ///
  1476. /// # Examples
  1477. ///
  1478. /// If this code didn't use `panic_fuse()`, it would continue processing
  1479. /// many more items in other threads (with long sleep delays) before the
  1480. /// panic is finally propagated.
  1481. ///
  1482. /// ```should_panic
  1483. /// use rayon::prelude::*;
  1484. /// use std::{thread, time};
  1485. ///
  1486. /// (0..1_000_000)
  1487. /// .into_par_iter()
  1488. /// .panic_fuse()
  1489. /// .for_each(|i| {
  1490. /// // simulate some work
  1491. /// thread::sleep(time::Duration::from_secs(1));
  1492. /// assert!(i > 0); // oops!
  1493. /// });
  1494. /// ```
  1495. fn panic_fuse(self) -> PanicFuse<Self> {
  1496. PanicFuse::new(self)
  1497. }
  1498. /// Creates a fresh collection containing all the elements produced
  1499. /// by this parallel iterator.
  1500. ///
  1501. /// You may prefer [`collect_into_vec()`] implemented on
  1502. /// [`IndexedParallelIterator`], if your underlying iterator also implements
  1503. /// it. [`collect_into_vec()`] allocates efficiently with precise knowledge
  1504. /// of how many elements the iterator contains, and even allows you to reuse
  1505. /// an existing vector's backing store rather than allocating a fresh vector.
  1506. ///
  1507. /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
  1508. /// [`collect_into_vec()`]:
  1509. /// trait.IndexedParallelIterator.html#method.collect_into_vec
  1510. ///
  1511. /// # Examples
  1512. ///
  1513. /// ```
  1514. /// use rayon::prelude::*;
  1515. ///
  1516. /// let sync_vec: Vec<_> = (0..100).into_iter().collect();
  1517. ///
  1518. /// let async_vec: Vec<_> = (0..100).into_par_iter().collect();
  1519. ///
  1520. /// assert_eq!(sync_vec, async_vec);
  1521. /// ```
  1522. fn collect<T>(self) -> Collect<Self, T>
  1523. where
  1524. T: FromParallelIterator<'a, Self::Item>,
  1525. {
  1526. Collect::new(self)
  1527. }
  1528. /// Unzips the items of a parallel iterator into a pair of arbitrary
  1529. /// `ParallelExtend` containers.
  1530. ///
  1531. /// You may prefer to use `unzip_into_vecs()`, which allocates more
  1532. /// efficiently with precise knowledge of how many elements the
  1533. /// iterator contains, and even allows you to reuse existing
  1534. /// vectors' backing stores rather than allocating fresh vectors.
  1535. ///
  1536. /// # Examples
  1537. ///
  1538. /// ```
  1539. /// use rayon::prelude::*;
  1540. ///
  1541. /// let a = [(0, 1), (1, 2), (2, 3), (3, 4)];
  1542. ///
  1543. /// let (left, right): (Vec<_>, Vec<_>) = a.par_iter().cloned().unzip();
  1544. ///
  1545. /// assert_eq!(left, [0, 1, 2, 3]);
  1546. /// assert_eq!(right, [1, 2, 3, 4]);
  1547. /// ```
  1548. ///
  1549. /// Nested pairs can be unzipped too.
  1550. ///
  1551. /// ```
  1552. /// use rayon::prelude::*;
  1553. ///
  1554. /// let (values, squares, cubes): (Vec<_>, Vec<_>, Vec<_>) = (0..4).into_par_iter()
  1555. /// .map(|i| (i, i * i, i * i * i))
  1556. /// .unzip();
  1557. ///
  1558. /// assert_eq!(values, [0, 1, 2, 3]);
  1559. /// assert_eq!(squares, [0, 1, 4, 9]);
  1560. /// assert_eq!(cubes, [0, 1, 8, 27]);
  1561. /// ```
  1562. fn unzip(self) -> Unzip<Self> {
  1563. Unzip::new(self)
  1564. }
  1565. /// Partitions the items of a parallel iterator into a pair of arbitrary
  1566. /// `ParallelExtend` containers. Items for which the `operation` returns
  1567. /// true go into the first container, and the rest go into the second.
  1568. ///
  1569. /// Note: unlike the standard `Iterator::partition`, this allows distinct
  1570. /// collection types for the left and right items. This is more flexible,
  1571. /// but may require new type annotations when converting sequential code
  1572. /// that used type inferrence assuming the two were the same.
  1573. ///
  1574. /// # Examples
  1575. ///
  1576. /// ```
  1577. /// use rayon::prelude::*;
  1578. ///
  1579. /// let (left, right): (Vec<_>, Vec<_>) = (0..8).into_par_iter().partition(|x| x % 2 == 0);
  1580. ///
  1581. /// assert_eq!(left, [0, 2, 4, 6]);
  1582. /// assert_eq!(right, [1, 3, 5, 7]);
  1583. /// ```
  1584. fn partition<O>(self, operation: O) -> Partition<Self, O>
  1585. where
  1586. O: Fn(&Self::Item) -> bool + Sync + Send,
  1587. {
  1588. Partition::new(self, operation)
  1589. }
  1590. /// Partitions and maps the items of a parallel iterator into a pair of
  1591. /// arbitrary `ParallelExtend` containers. `Either::Left` items go into
  1592. /// the first container, and `Either::Right` items go into the second.
  1593. ///
  1594. /// # Examples
  1595. ///
  1596. /// ```
  1597. /// use rayon::prelude::*;
  1598. /// use rayon::iter::Either;
  1599. ///
  1600. /// let (left, right): (Vec<_>, Vec<_>) = (0..8).into_par_iter()
  1601. /// .partition_map(|x| {
  1602. /// if x % 2 == 0 {
  1603. /// Either::Left(x * 4)
  1604. /// } else {
  1605. /// Either::Right(x * 3)
  1606. /// }
  1607. /// });
  1608. ///
  1609. /// assert_eq!(left, [0, 8, 16, 24]);
  1610. /// assert_eq!(right, [3, 9, 15, 21]);
  1611. /// ```
  1612. ///
  1613. /// Nested `Either` enums can be split as well.
  1614. ///
  1615. /// ```
  1616. /// use rayon::prelude::*;
  1617. /// use rayon::iter::Either::*;
  1618. ///
  1619. /// let ((fizzbuzz, fizz), (buzz, other)): ((Vec<_>, Vec<_>), (Vec<_>, Vec<_>)) = (1..20)
  1620. /// .into_par_iter()
  1621. /// .partition_map(|x| match (x % 3, x % 5) {
  1622. /// (0, 0) => Left(Left(x)),
  1623. /// (0, _) => Left(Right(x)),
  1624. /// (_, 0) => Right(Left(x)),
  1625. /// (_, _) => Right(Right(x)),
  1626. /// });
  1627. ///
  1628. /// assert_eq!(fizzbuzz, [15]);
  1629. /// assert_eq!(fizz, [3, 6, 9, 12, 18]);
  1630. /// assert_eq!(buzz, [5, 10]);
  1631. /// assert_eq!(other, [1, 2, 4, 7, 8, 11, 13, 14, 16, 17, 19]);
  1632. /// ```
  1633. fn partition_map<O>(self, operation: O) -> PartitionMap<Self, O> {
  1634. PartitionMap::new(self, operation)
  1635. }
  1636. /// Intersperses clones of an element between items of this iterator.
  1637. ///
  1638. /// # Examples
  1639. ///
  1640. /// ```
  1641. /// use rayon::prelude::*;
  1642. ///
  1643. /// let x = vec![1, 2, 3];
  1644. /// let r: Vec<_> = x.into_par_iter().intersperse(-1).collect();
  1645. ///
  1646. /// assert_eq!(r, vec![1, -1, 2, -1, 3]);
  1647. /// ```
  1648. fn intersperse(self, item: Self::Item) -> Intersperse<Self, Self::Item>
  1649. where
  1650. Self::Item: Clone,
  1651. {
  1652. Intersperse::new(self, item)
  1653. }
  1654. /// Sets the number of splits that are processed in parallel.
  1655. ///
  1656. /// # Examples
  1657. ///
  1658. /// ```
  1659. /// use rayon::prelude::*;
  1660. ///
  1661. /// let min = (0..1_000_000)
  1662. /// .into_par_iter()
  1663. /// .with_splits(8)
  1664. /// .for_each(|| println!("Thread ID: {:?}", std::thread::current().id))
  1665. /// .exec();
  1666. ///
  1667. /// assert!(min >= 1234);
  1668. /// ```
  1669. fn with_splits(self, splits: usize) -> Splits<Self> {
  1670. Splits::new(self, splits)
  1671. }
  1672. }
  1673. /// An iterator that supports "random access" to its data, meaning
  1674. /// that you can split it at arbitrary indices and draw data from
  1675. /// those points.
  1676. ///
  1677. /// **Note:** Not implemented for `u64`, `i64`, `u128`, or `i128` ranges
  1678. pub trait IndexedParallelIterator<'a>: ParallelIterator<'a> {
  1679. /// Internal method used to define the behavior of this parallel
  1680. /// iterator. You should not need to call this directly.
  1681. ///
  1682. /// This method causes the iterator `self` to start producing
  1683. /// items and to feed them to the consumer `consumer` one by one.
  1684. /// It may split the consumer before doing so to create the
  1685. /// opportunity to produce in parallel. If a split does happen, it
  1686. /// will inform the consumer of the index where the split should
  1687. /// occur (unlike `ParallelIterator::drive_unindexed()`).
  1688. ///
  1689. /// See the [README] for more details on the internals of parallel
  1690. /// iterators.
  1691. ///
  1692. /// [README]: README.md
  1693. fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
  1694. where
  1695. E: Executor<'a, D>,
  1696. C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
  1697. D: Send + 'a,
  1698. R: Reducer<D> + Send + 'a;
  1699. /// Produces an exact count of how many items this iterator will
  1700. /// produce, presuming no panic occurs.
  1701. ///
  1702. /// # Examples
  1703. ///
  1704. /// ```
  1705. /// use asparit::*;
  1706. ///
  1707. /// let par_iter = (0..100).into_par_iter().zip(vec![0; 10]);
  1708. /// assert_eq!(par_iter.len(), 10);
  1709. ///
  1710. /// let vec: Vec<_> = par_iter.collect();
  1711. /// assert_eq!(vec.len(), 10);
  1712. /// ```
  1713. fn len_hint(&self) -> usize;
  1714. }