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
use super::wrappers::values::ConIterValues;
use crate::{
    next::{Next, NextMany},
    ConIterIdsAndValues,
};

/// Trait defining a concurrent iterator with `next` and `next_id_and_chunk` methods which can safely be called my multiple threads concurrently.
pub trait ConcurrentIter: Send + Sync {
    /// Type of the items that the iterator yields.
    type Item: Send + Sync;

    /// Advances the iterator and returns the next value together with its enumeration index.
    ///
    /// Returns [None] when iteration is finished.
    fn next_id_and_value(&self) -> Option<Next<Self::Item>>;

    /// Advances the iterator `chunk_size` times and returns an iterator of at most `chunk_size` consecutive next values.
    /// Further, the beginning enumeration index of the yielded values is returned.
    ///
    /// This method:
    /// * returns an iterator of `chunk_size` elements if there exists sufficient elements left in the iteration, or
    /// * it might return an iterator of `m < chunk_size` elements if there exists only `m` elements left, or
    /// * it might return an empty iterator.
    ///
    /// This call would be equivalent to calling `next_id_and_value` method `chunk_size` times in a single-threaded execution.
    /// However, calling `next` method `chunk_size` times in a concurrent execution does not guarantee to return `chunk_size` consecutive elements.
    /// On the other hand, `next_id_and_chunk` guarantees that it returns consecutive elements, preventing any intermediate calls.
    fn next_id_and_chunk(
        &self,
        chunk_size: usize,
    ) -> NextMany<Self::Item, impl Iterator<Item = Self::Item>>;

    /// Advances the iterator and returns the next value.
    ///
    /// Returns [None] when iteration is finished.
    #[inline(always)]
    fn next(&self) -> Option<Self::Item> {
        self.next_id_and_value().map(|x| x.value)
    }

    /// Advances the iterator `chunk_size` times and returns an iterator of at most `chunk_size` consecutive next values.
    ///
    /// This method:
    /// * returns an iterator of `chunk_size` elements if there exists sufficient elements left in the iteration, or
    /// * it might return an iterator of `m < chunk_size` elements if there exists only `m` elements left, or
    /// * it might return an empty iterator.
    ///
    /// This call would be equivalent to calling `next` method `n` times in a single-threaded execution.
    /// However, calling `next` method `chunk_size` times in a concurrent execution does not guarantee to return `n` consecutive elements.
    /// On the other hand, `next_id_and_chunk` guarantees that it returns consecutive elements, preventing any intermediate calls.
    #[inline(always)]
    fn next_chunk(&self, chunk_size: usize) -> impl Iterator<Item = Self::Item> {
        self.next_id_and_chunk(chunk_size).values
    }

    /// Returns an `Iterator` over the values of elements of the concurrent iterator.
    ///
    /// The iterator's `next` method does nothing but call the `next`; this iterator is only to allow for using `for` loops directly.
    fn values(&self) -> ConIterValues<Self>
    where
        Self: Sized,
    {
        self.into()
    }

    /// Returns an `Iterator` over the ids and values of elements of the concurrent iterator.
    ///
    /// The iterator's `next` method does nothing but call the `next_id_and_value`; this iterator is only to allow for using `for` loops directly.
    fn ids_and_values(&self) -> ConIterIdsAndValues<Self>
    where
        Self: Sized,
    {
        self.into()
    }
}

/// A concurrent iterator that knows its exact length.
pub trait ExactSizeConcurrentIter: ConcurrentIter {
    /// Returns the exact remaining length of the concurrent iterator.
    fn len(&self) -> usize;

    /// Returns true if the iterator is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use orx_concurrent_bag::ConcurrentBag;
    use std::ops::Add;

    pub(crate) fn test_values<C: ConcurrentIter>(num_threads: usize, len: usize, con_iter: C)
    where
        C::Item: Add<usize, Output = usize>,
    {
        let collected = ConcurrentBag::new();
        let bag = &collected;
        let iter = &con_iter;

        std::thread::scope(|s| {
            for _ in 0..num_threads {
                s.spawn(move || {
                    for value in iter.values() {
                        bag.push(value + 0usize);
                    }
                });
            }
        });

        assert_eq!(collected.len(), len);

        let mut collected = collected.into_inner().to_vec();

        collected.sort();
        assert_eq!(collected, (0..len).collect::<Vec<_>>());
    }

    pub(crate) fn test_ids_and_values<C: ConcurrentIter>(
        num_threads: usize,
        len: usize,
        con_iter: C,
    ) where
        C::Item: Add<usize, Output = usize>,
    {
        let collected = ConcurrentBag::new();
        let bag = &collected;
        let iter = &con_iter;

        std::thread::scope(|s| {
            for _ in 0..num_threads {
                s.spawn(move || {
                    for (i, value) in iter.ids_and_values() {
                        bag.push((i, value + 0usize));
                    }
                });
            }
        });

        assert_eq!(collected.len(), len);

        let mut collected = collected.into_inner().to_vec();
        for (i, value) in &collected {
            assert_eq!(i, value);
        }

        collected.sort();
        assert_eq!(collected, (0..len).map(|x| (x, x)).collect::<Vec<_>>());
    }
}