arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/util/src/lib.rs
util/src/lib.rsRaw
 1// Copyright (c) 2022 Espresso Systems (espressosys.com)
 2// This file is part of the Jellyfish library.
 3// You should have received a copy of the MIT License
 4// along with the Jellyfish library. If not, see <https://mit-license.org/>.
 5
 6//! Utilities for parallel code.
 7
 8/// this function helps with slice iterator creation that optionally use
 9/// `par_iter()` when feature flag `parallel` is on.
10///
11/// # Usage
12/// let v = [1, 2, 3, 4, 5];
13/// let sum = parallelizable_slice_iter(&v).sum();
14///
15/// // the above code is a shorthand for (thus equivalent to)
16/// #[cfg(feature = "parallel")]
17/// let sum = v.par_iter().sum();
18/// #[cfg(not(feature = "parallel"))]
19/// let sum = v.iter().sum();
20#[cfg(feature = "parallel")]
21pub fn parallelizable_slice_iter<T: Sync>(data: &[T]) -> rayon::slice::Iter<T> {
22    use rayon::iter::IntoParallelIterator;
23    data.into_par_iter()
24}
25
26#[cfg(not(feature = "parallel"))]
27pub fn parallelizable_slice_iter<T>(data: &[T]) -> core::slice::Iter<T> {
28    data.iter()
29}