lamellar/array/iterator/distributed_iterator/
map.rs

1use crate::array::iterator::{distributed_iterator::*, IterLockFuture};
2
3/// `Map` is an iterator that applies a function to each element of the input iterator, transforming it into a new type.
4#[derive(Clone, Debug)]
5pub struct Map<I, F> {
6    iter: I,
7    f: F,
8}
9
10impl<I: InnerIter, F: Clone> InnerIter for Map<I, F> {
11    fn lock_if_needed(&self, _s: Sealed) -> Option<IterLockFuture> {
12        self.iter.lock_if_needed(_s)
13    }
14    fn iter_clone(&self, _s: Sealed) -> Self {
15        Map {
16            iter: self.iter.iter_clone(Sealed),
17            f: self.f.clone(),
18        }
19    }
20}
21
22impl<I, F> Map<I, F>
23where
24    I: DistributedIterator,
25{
26    pub(crate) fn new(iter: I, f: F) -> Map<I, F> {
27        // println!("new Map {:?} ",count);
28        Map { iter, f }
29    }
30}
31
32impl<B, I, F> DistributedIterator for Map<I, F>
33where
34    I: DistributedIterator,
35    F: FnMut(I::Item) -> B + SyncSend + Clone + 'static,
36    B: Send,
37{
38    type Item = B;
39    type Array = <I as DistributedIterator>::Array;
40    fn init(&self, start_i: usize, cnt: usize, _s: Sealed) -> Map<I, F> {
41        // println!("init enumerate start_i: {:?} cnt {:?} end_i {:?}",start_i, cnt, start_i+cnt );
42        Map::new(self.iter.init(start_i, cnt, _s), self.f.clone())
43    }
44    fn array(&self) -> Self::Array {
45        self.iter.array()
46    }
47    fn next(&mut self) -> Option<Self::Item> {
48        self.iter.next().map(&mut self.f)
49    }
50
51    fn elems(&self, in_elems: usize) -> usize {
52        let in_elems = self.iter.elems(in_elems);
53        // println!("enumerate elems {:?}",in_elems);
54        in_elems
55    }
56    // fn global_index(&self, index: usize) -> Option<usize> {
57    //     let g_index = self.iter.global_index(index);
58    //     // println!("enumerate index: {:?} global_index {:?}", index,g_index);
59    //     g_index
60    // }
61    // fn subarray_index(&self, index: usize) -> Option<usize> {
62    //     let g_index = self.iter.subarray_index(index);
63    //                                                    // println!("enumerate index: {:?} global_index {:?}", index,g_index);
64    //     g_index
65    // }
66    fn advance_index(&mut self, count: usize) {
67        self.iter.advance_index(count);
68    }
69}
70
71// impl<B, I, F> IndexedDistributedIterator for Map<I, F>
72// where
73//     I: IndexedDistributedIterator,
74//     F: FnMut(I::Item) -> B + SyncSend + Clone + 'static,
75//     B: Send,
76// {
77//     fn iterator_index(&self, index: usize) -> Option<usize> {
78//         let g_index = self.iter.iterator_index(index);
79//         // println!("enumerate index: {:?} global_index {:?}", index,g_index);
80//         g_index
81//     }
82// }