ella_tensor/mask/
iter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::{shape::ShapeIndexIter, Mask, Shape};

use super::MaskData;

pub enum ValidityIter {
    Constant(usize, bool),
    Values { inner: MaskData, index: usize },
}

impl ValidityIter {
    pub(crate) fn new(inner: MaskData) -> Self {
        Self::as_constant(&inner).unwrap_or(Self::Values { inner, index: 0 })
    }

    fn as_constant(inner: &MaskData) -> Option<Self> {
        if inner.num_masked() == 0 {
            Some(Self::Constant(inner.len(), true))
        } else if inner.num_valid() == 0 {
            Some(Self::Constant(inner.len(), false))
        } else {
            None
        }
    }
}

impl Iterator for ValidityIter {
    type Item = bool;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            ValidityIter::Constant(remaining, value) => {
                if *remaining > 0 {
                    *remaining -= 1;
                    Some(*value)
                } else {
                    None
                }
            }
            ValidityIter::Values { inner, index } => {
                if *index < inner.len() {
                    let value = inner.is_valid(*index as isize);
                    *index += 1;
                    Some(value)
                } else {
                    None
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len(), Some(self.len()))
    }
}

impl ExactSizeIterator for ValidityIter {
    fn len(&self) -> usize {
        match self {
            ValidityIter::Constant(remaining, _) => *remaining,
            ValidityIter::Values { inner, index } => inner.len() - *index,
        }
    }
}

pub enum MaskIter<S: Shape> {
    Flat(ValidityIter),
    Shaped {
        inner: Mask<S>,
        shape: ShapeIndexIter<S>,
    },
}

impl<S> MaskIter<S>
where
    S: Shape,
{
    pub(crate) fn new(inner: Mask<S>) -> Self {
        if inner.is_standard_layout() {
            Self::Flat(inner.values.into_iter())
        } else {
            let shape = inner.shape().clone().indices();
            Self::Shaped { inner, shape }
        }
    }
}

impl<S> Iterator for MaskIter<S>
where
    S: Shape,
{
    type Item = bool;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            MaskIter::Flat(inner) => inner.next(),
            MaskIter::Shaped { inner, shape } => shape.next().map(|index| inner.index(index)),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len(), Some(self.len()))
    }
}

impl<S> ExactSizeIterator for MaskIter<S>
where
    S: Shape,
{
    fn len(&self) -> usize {
        match self {
            MaskIter::Flat(inner) => inner.len(),
            MaskIter::Shaped { shape, .. } => shape.len(),
        }
    }
}