1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use super::{
    atomic_iter::{AtomicIter, AtomicIterWithInitialLen},
    buffered::{
        buffered_chunk::BufferedChunk, buffered_iter::BufferedIter,
        cloned_buffered_chunk::ClonedBufferedChunk,
    },
};
use crate::{ConcurrentIter, NextChunk};
use std::marker::PhantomData;

/// An concurrent iterator, backed with an atomic iterator, that clones the elements of an underlying iterator.
///
/// This `struct` is created by the `cloned` method on the concurrent iterator.
pub struct Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T>,
{
    iter: A,
    phantom: PhantomData<&'a T>,
}

impl<'a, T, A> Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T>,
{
    pub(crate) fn new(iter: A) -> Self {
        Self {
            iter,
            phantom: PhantomData,
        }
    }

    pub(crate) fn underlying_iter(&self) -> &A {
        &self.iter
    }
}

impl<'a, T, A> AtomicIter<T> for Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T>,
{
    #[inline(always)]
    fn counter(&self) -> &crate::AtomicCounter {
        self.iter.counter()
    }

    #[inline(always)]
    fn progress_and_get_begin_idx(&self, number_to_fetch: usize) -> Option<usize> {
        self.iter.progress_and_get_begin_idx(number_to_fetch)
    }

    #[inline(always)]
    fn get(&self, item_idx: usize) -> Option<T> {
        self.iter.get(item_idx).cloned()
    }

    #[inline(always)]
    fn fetch_n(&self, n: usize) -> Option<NextChunk<T, impl ExactSizeIterator<Item = T>>> {
        self.iter.fetch_n(n).map(|x| NextChunk {
            begin_idx: x.begin_idx,
            values: x.values.cloned(),
        })
    }

    fn early_exit(&self) {
        self.iter.early_exit()
    }
}

impl<'a, T, A> AtomicIterWithInitialLen<T> for Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T> + AtomicIterWithInitialLen<&'a T>,
{
    fn initial_len(&self) -> usize {
        self.iter.initial_len()
    }
}

unsafe impl<'a, T, A> Sync for Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T>,
{
}

unsafe impl<'a, T, A> Send for Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T>,
{
}

impl<'a, T, A> ConcurrentIter for Cloned<'a, T, A>
where
    T: Send + Sync + Clone,
    A: AtomicIter<&'a T> + ConcurrentIter<Item = &'a T>,
{
    type Item = T;

    type BufferedIter = ClonedBufferedChunk<'a, T, A::BufferedIter>;

    type SeqIter = std::iter::Cloned<A::SeqIter>;

    fn into_seq_iter(self) -> Self::SeqIter {
        self.iter.into_seq_iter().cloned()
    }

    #[inline(always)]
    fn next_id_and_value(&self) -> Option<crate::Next<Self::Item>> {
        self.fetch_one()
    }

    #[inline(always)]
    fn next_chunk(
        &self,
        chunk_size: usize,
    ) -> Option<NextChunk<Self::Item, impl ExactSizeIterator<Item = Self::Item>>> {
        self.fetch_n(chunk_size)
    }

    fn buffered_iter(&self, chunk_size: usize) -> BufferedIter<Self::Item, Self::BufferedIter> {
        let buffered_iter = Self::BufferedIter::new(chunk_size);
        BufferedIter::new(buffered_iter, self)
    }

    fn skip_to_end(&self) {
        self.early_exit()
    }

    #[inline(always)]
    fn try_get_len(&self) -> Option<usize> {
        self.iter.try_get_len()
    }
}

/// Trait converting a concurrent iterator yielding &T to one yielding T by cloning elements.
pub trait IntoCloned<'a, T, C>
where
    Self: Into<C>,
    T: Send + Sync + Clone + 'a,
    C: AtomicIter<&'a T>,
{
    /// Converts this concurrent iterator over references into another concurrent iterator yielding clones of the elements.
    fn cloned(self) -> Cloned<'a, T, C> {
        Cloned::new(self.into())
    }
}

impl<'a, T, C> IntoCloned<'a, T, C> for C
where
    T: Send + Sync + Clone + 'a,
    C: AtomicIter<&'a T>,
{
}