diff --git a/ylong_runtime/src/iter/parallel/slice.rs b/ylong_runtime/src/iter/parallel/slice.rs index a8a10ac72be985ec7b7f007fc5ade93cbce8cfc1..36d014dd7b3e4a7c9d376f6740ddaa663c237263 100644 --- a/ylong_runtime/src/iter/parallel/slice.rs +++ b/ylong_runtime/src/iter/parallel/slice.rs @@ -26,7 +26,11 @@ impl ParSplit for &[T] { fn split(self) -> (Self, Option) { let idx = self.len() >> 1; let (left, right) = self.split_at(idx); - (left, Some(right)) + if right.is_empty(){ + (left,None) + }else{ + (left, Some(right)) + } } } @@ -42,6 +46,10 @@ impl<'a, T> ParSplit for &'a mut [T] { fn split(self) -> (Self, Option) { let idx = self.len() >> 1; let (left, right) = self.split_at_mut(idx); - (left, Some(right)) + if right.is_empty(){ + (left,None) + }else{ + (left, Some(right)) + } } } diff --git a/ylong_runtime/tests/par_iter.rs b/ylong_runtime/tests/par_iter.rs index 99d77ad6c222249a94ca603c0cb290c2313f2d50..d16ef5f1827a5c90eca3ad5a931ae1ce98452794 100644 --- a/ylong_runtime/tests/par_iter.rs +++ b/ylong_runtime/tests/par_iter.rs @@ -23,10 +23,11 @@ use std::collections::{HashMap, HashSet}; use ylong_runtime::iter::prelude::*; #[test] fn sdv_par_iter_test() { - let fut = async { + ylong_runtime::block_on(async { let a = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let b = vec![2, 3, 4, 5]; + let mut b = vec![1, 2, 3, 4]; + b.par_iter_mut().for_each(|x|*x+=1).await.unwrap(); let sum = a .par_iter() .map(|x| *x + 1) @@ -38,16 +39,16 @@ fn sdv_par_iter_test() { .unwrap(); assert_eq!(sum, 29); - let s = a.iter().copied().collect::>(); - let sum = s.into_par_iter().sum().await.unwrap(); + let hash_set = a.iter().copied().collect::>(); + let sum = hash_set.into_par_iter().sum().await.unwrap(); assert_eq!(sum, 55); - let m = a + let hash_map = a .iter() .zip(b.iter()) .map(|x| (*x.0, *x.1)) .collect::>(); - let sum = m.into_par_iter().map(|x| x.0 * x.1).sum().await.unwrap(); + let sum = hash_map.into_par_iter().map(|x| x.0 * x.1).sum().await.unwrap(); assert_eq!(sum, 40); let sum = a @@ -60,6 +61,23 @@ fn sdv_par_iter_test() { .await .unwrap(); assert_eq!(sum, 29); - }; - ylong_runtime::block_on(fut); + + let mut a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let b = [2, 3, 4, 5]; + let mut c = [1]; + let mut d = [1,2,3,4]; + a.par_iter_mut().for_each(|x|*x+=1).await.unwrap(); + + let sum = a + .par_iter() + .filter(|x| **x < 5) + .zip(b.par_iter()) + .zip(c.par_iter_mut()) + .zip(d.par_iter_mut()) + .map(|x| {*x.1=2;*x.0.1*=2;x.0.0.0 * (*x.0.0.1)+*x.0.1*(*x.1)}) + .sum() + .await + .unwrap(); + assert_eq!(sum, 8); + }); }