risingwave_common/array/
iterator.rs

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
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::iter::TrustedLen;

use super::Array;

pub struct ArrayIterator<'a, A: Array> {
    data: &'a A,
    pos: usize,
}

impl<'a, A: Array> ArrayIterator<'a, A> {
    pub fn new(data: &'a A) -> Self {
        Self { data, pos: 0 }
    }
}

impl<'a, A: Array> Iterator for ArrayIterator<'a, A> {
    type Item = Option<A::RefItem<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.data.len() {
            None
        } else {
            // SAFETY: bounds check is done by `self.pos < self.data.len()`.
            let item = unsafe { self.data.value_at_unchecked(self.pos) };
            self.pos += 1;
            Some(item)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.data.len() - self.pos;
        (size, Some(size))
    }
}

impl<A: Array> ExactSizeIterator for ArrayIterator<'_, A> {}
unsafe impl<A: Array> TrustedLen for ArrayIterator<'_, A> {}

#[cfg(test)]
mod tests {
    use paste::paste;

    use super::*;
    use crate::array::{ArrayBuilder, ArrayImpl};
    use crate::for_all_variants;

    macro_rules! test_trusted_len {
        ($( { $data_type:ident, $variant_name:ident, $suffix_name:ident, $scalar:ty, $scalar_ref:ty, $array:ty, $builder:ty } ),*) => {
            $(
                paste! {
                    #[test]
                    fn [<test_trusted_len_for_ $suffix_name _array>]() {
                        let mut builder = $builder::new(5);
                        for _ in 0..5 {
                            builder.append_null();
                        }
                        let array = builder.finish();
                        let mut iter = array.iter();

                        assert_eq!(iter.size_hint(), (5, Some(5))); iter.next();
                        assert_eq!(iter.size_hint(), (4, Some(4))); iter.next();
                        assert_eq!(iter.size_hint(), (3, Some(3))); iter.nth(0);
                        assert_eq!(iter.size_hint(), (2, Some(2))); iter.nth(1);
                        assert_eq!(iter.size_hint(), (0, Some(0)));

                        let array_impl = ArrayImpl::from(array);
                        let mut iter = array_impl.iter();

                        assert_eq!(iter.size_hint(), (5, Some(5))); iter.next();
                        assert_eq!(iter.size_hint(), (4, Some(4))); iter.next();
                        assert_eq!(iter.size_hint(), (3, Some(3))); iter.nth(0);
                        assert_eq!(iter.size_hint(), (2, Some(2))); iter.nth(1);
                        assert_eq!(iter.size_hint(), (0, Some(0)));
                    }
                }
            )*
        };
    }

    for_all_variants! { test_trusted_len }
}