Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1794 строки
61 KiB

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