1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use super::pariter::ParallelIterator; 15 16 /// Parallel iterator implementation for array 17 pub mod array; 18 /// Parallel iterator implementation for std collections 19 pub mod collections; 20 21 /// Parallel iterator implementation for slice 22 pub mod slice; 23 24 /// Parallel iterator implementation for vec 25 pub mod vec; 26 27 /// Splits data into two parts of the same type 28 pub trait ParSplit: Sized + IntoIterator { 29 /// Returns the number of the elements in the data len(&self) -> usize30 fn len(&self) -> usize; 31 32 /// Reduces the number of elements in the data reduce(self, len: usize) -> Self33 fn reduce(self, len: usize) -> Self; 34 35 /// Splits data into two parts, if it can no longer be divided, returns 36 /// None. split(self) -> (Self, Option<Self>)37 fn split(self) -> (Self, Option<Self>); 38 39 /// Returns true if the parsplit has a length of 0 is_empty(&self) -> bool40 fn is_empty(&self) -> bool { 41 self.len() == 0 42 } 43 } 44 45 /// Trait that defines interfaces for a struct to convert itself into `ParIter`. 46 pub trait IntoParIter { 47 /// Type that can be splitted 48 type Data: ParSplit; 49 50 /// Crates a parallel iterator from a value. into_par_iter(self) -> ParIter<Self::Data>51 fn into_par_iter(self) -> ParIter<Self::Data>; 52 } 53 54 /// Parallel iterator 55 pub struct ParIter<T: ParSplit> { 56 data: T, 57 } 58 59 impl<T: ParSplit> IntoParIter for T { 60 type Data = T; into_par_iter(self) -> ParIter<Self::Data>61 fn into_par_iter(self) -> ParIter<Self::Data> { 62 ParIter { data: self } 63 } 64 } 65 66 /// Trait that defines interfaces for a struct to convert itself into `ParIter`. 67 pub trait AsParIter<'a> { 68 /// Type of data that can be splitted 69 type Data: ParSplit + 'a; 70 71 /// Crates a immutable parallel iterator for a immutable reference par_iter(&'a self) -> ParIter<Self::Data>72 fn par_iter(&'a self) -> ParIter<Self::Data>; 73 } 74 75 impl<'a, T: 'a> AsParIter<'a> for T 76 where 77 &'a T: IntoParIter, 78 { 79 type Data = <&'a T as IntoParIter>::Data; par_iter(&'a self) -> ParIter<Self::Data>80 fn par_iter(&'a self) -> ParIter<Self::Data> { 81 self.into_par_iter() 82 } 83 } 84 85 /// Trait that defines interfaces for a struct to convert itself into `ParIter`. 86 pub trait AsParIterMut<'a> { 87 /// Type of data that can be splitted. 88 type Data: ParSplit + 'a; 89 90 /// Crates a mutable parallel iterator for a mutable reference par_iter_mut(&'a mut self) -> ParIter<Self::Data>91 fn par_iter_mut(&'a mut self) -> ParIter<Self::Data>; 92 } 93 94 impl<'a, T: 'a> AsParIterMut<'a> for T 95 where 96 &'a mut T: IntoParIter, 97 { 98 type Data = <&'a mut T as IntoParIter>::Data; 99 par_iter_mut(&'a mut self) -> ParIter<Self::Data>100 fn par_iter_mut(&'a mut self) -> ParIter<Self::Data> { 101 self.into_par_iter() 102 } 103 } 104 105 impl<T: ParSplit + Send> ParallelIterator for ParIter<T> { 106 type Item = T::Item; 107 type Iter = T::IntoIter; 108 len(&self) -> usize109 fn len(&self) -> usize { 110 self.data.len() 111 } 112 reduce(self, len: usize) -> Self113 fn reduce(self, len: usize) -> Self { 114 Self { 115 data: self.data.reduce(len), 116 } 117 } 118 iter(self) -> Self::Iter119 fn iter(self) -> Self::Iter { 120 self.data.into_iter() 121 } 122 split(self) -> (Self, Option<Self>)123 fn split(self) -> (Self, Option<Self>) { 124 let (left, right) = self.data.split(); 125 let left = ParIter { data: left }; 126 let right = right.map(|data| ParIter { data }); 127 (left, right) 128 } 129 } 130