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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::{
    atomic_counter::AtomicCounter,
    con_iter::{ConcurrentIter, ExactSizeConcurrentIter},
};
use crate::next::{Next, NextMany};
use std::cmp::Ordering;

/// Trait defining a set of concurrent iterators which internally uses an atomic counter of elements to be yielded.
///
/// Note that every `A: AtomicIter` also implements `ConcurrentIter`.
pub trait AtomicIter: Send + Sync {
    /// Type of the items that the iterator yields.
    type Item: Send + Sync;

    /// Returns a reference to the underlying advanced item counter.
    fn counter(&self) -> &AtomicCounter;

    /// Returns the `item_idx`-th item that the iterator yields; returns None if the iterator completes before.
    fn get(&self, item_idx: usize) -> Option<Self::Item>;

    /// Returns the next item that the iterator yields; returns None if the iteration has completed.
    #[inline(always)]
    fn fetch_one(&self) -> Option<Next<Self::Item>> {
        let idx = self.counter().fetch_and_increment();
        self.get(idx).map(|value| Next { idx, value })
    }

    /// Returns an iterator of the next `n` **consecutive items** that the iterator yields.
    /// It might return an iterator of less or no items if the iteration does not have sufficient elements left.
    fn fetch_n(&self, n: usize) -> NextMany<Self::Item, impl Iterator<Item = Self::Item>> {
        let begin_idx = self.counter().fetch_and_add(n);

        let idx_range = begin_idx..(begin_idx + n);
        let values = idx_range
            .map(|i| self.get(i))
            .take_while(|x| x.is_some())
            .map(|x| x.expect("is-some is checked"));

        NextMany { begin_idx, values }
    }
}

/// An atomic counter based iterator with exactly known initial length.
pub trait AtomicIterWithInitialLen: AtomicIter {
    /// Returns the initial length of the atomic iterator.
    fn initial_len(&self) -> usize;
}

impl<A: AtomicIter> ConcurrentIter for A {
    type Item = A::Item;

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

    #[inline(always)]
    fn next_id_and_chunk(
        &self,
        n: usize,
    ) -> NextMany<<Self as AtomicIter>::Item, impl Iterator<Item = <Self as AtomicIter>::Item>>
    {
        self.fetch_n(n)
    }
}

impl<A: AtomicIterWithInitialLen> ExactSizeConcurrentIter for A {
    fn len(&self) -> usize {
        let current = self.counter().current();
        match current.cmp(&self.initial_len()) {
            Ordering::Less => self.initial_len() - current,
            _ => 0,
        }
    }
}

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

    pub(crate) const ATOMIC_TEST_LEN: usize = 512;

    pub(crate) const ATOMIC_FETCH_N: [usize; 8] = [
        1,
        2,
        4,
        8,
        ATOMIC_TEST_LEN / 2,
        ATOMIC_TEST_LEN,
        ATOMIC_TEST_LEN + 1,
        ATOMIC_TEST_LEN * 2,
    ];

    pub(crate) fn atomic_fetch_one<A>(iter: A)
    where
        A: AtomicIter,
        A::Item: Add<usize, Output = usize>,
    {
        assert_eq!(0, iter.counter().current());

        let mut i = 0;
        while let Some(next) = iter.fetch_one() {
            let value = next.value + 0usize;
            assert_eq!(value, i);
            i += 1;
            assert_eq!(i, iter.counter().current());
        }
    }

    pub(crate) fn atomic_fetch_n<A>(iter: A, n: usize)
    where
        A: AtomicIter,
        A::Item: Add<usize, Output = usize>,
    {
        assert_eq!(0, iter.counter().current());

        let mut i = 0;
        let mut has_more = true;

        while has_more {
            has_more = false;
            let next_id_and_chunk = iter.fetch_n(n);
            for (j, value) in next_id_and_chunk.values.enumerate() {
                let value = value + 0usize;
                assert_eq!(value, next_id_and_chunk.begin_idx + j);
                assert_eq!(value, i);

                i += 1;

                has_more = true;
            }
        }
    }

    pub(crate) fn atomic_exact_fetch_one<A>(iter: A)
    where
        A: AtomicIterWithInitialLen,
        A::Item: Add<usize, Output = usize>,
    {
        let mut remaining = ATOMIC_TEST_LEN;

        assert!(!iter.is_empty());
        assert_eq!(iter.len(), remaining);

        while iter.fetch_one().is_some() {
            remaining -= 1;
            assert_eq!(iter.len(), remaining);
        }

        assert_eq!(iter.len(), 0);
        assert!(iter.is_empty());
    }

    pub(crate) fn atomic_exact_fetch_n<A>(iter: A, n: usize)
    where
        A: AtomicIterWithInitialLen,
        A::Item: Add<usize, Output = usize>,
    {
        let mut remaining = ATOMIC_TEST_LEN;

        assert!(!iter.is_empty());
        assert_eq!(iter.len(), remaining);

        let mut has_more = true;
        while has_more {
            has_more = false;

            let mut next_id_and_chunk = iter.fetch_n(n);
            if next_id_and_chunk.values.next().is_some() {
                has_more = true;
            }

            if n > remaining {
                remaining = 0;
            } else {
                remaining -= n;
            }

            assert_eq!(iter.len(), remaining);
        }

        assert_eq!(iter.len(), 0);
        assert!(iter.is_empty());
    }
}