vortex_vector/bool/
iter.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Iterator implementations for [`BoolVectorMut`].
5
6use vortex_buffer::BitBuffer;
7use vortex_buffer::BitBufferMut;
8use vortex_mask::Mask;
9use vortex_mask::MaskMut;
10
11use crate::VectorMutOps;
12use crate::bool::BoolVector;
13use crate::bool::BoolVectorMut;
14
15impl FromIterator<Option<bool>> for BoolVectorMut {
16    /// Creates a new [`BoolVectorMut`] from an iterator of `Option<bool>` values.
17    ///
18    /// `None` values will be marked as invalid in the validity mask.
19    ///
20    /// # Examples
21    ///
22    /// ```
23    /// use vortex_vector::bool::BoolVectorMut;
24    /// use vortex_vector::VectorMutOps;
25    ///
26    /// let mut vec = BoolVectorMut::from_iter([Some(true), None, Some(false)]);
27    /// assert_eq!(vec.len(), 3);
28    /// ```
29    fn from_iter<I>(iter: I) -> Self
30    where
31        I: IntoIterator<Item = Option<bool>>,
32    {
33        let iter = iter.into_iter();
34        // Since we do not know the length of the iterator, we can only guess how much memory we
35        // need to reserve. Note that these hints may be inaccurate.
36        let (lower_bound, _) = iter.size_hint();
37
38        // We choose not to use the optional upper bound size hint to match the standard library.
39
40        let mut bits = BitBufferMut::with_capacity(lower_bound);
41        let mut validity = MaskMut::with_capacity(lower_bound);
42
43        for opt_val in iter {
44            match opt_val {
45                Some(val) => {
46                    bits.append(val);
47                    validity.append_n(true, 1);
48                }
49                None => {
50                    bits.append(false); // Value doesn't matter for invalid entries.
51                    validity.append_n(false, 1);
52                }
53            }
54        }
55
56        BoolVectorMut { bits, validity }
57    }
58}
59
60impl FromIterator<bool> for BoolVectorMut {
61    /// Creates a new [`BoolVectorMut`] from an iterator of `bool` values.
62    ///
63    /// All values will be treated as non-null.
64    ///
65    /// # Examples
66    ///
67    /// ```
68    /// use vortex_vector::bool::BoolVectorMut;
69    /// use vortex_vector::VectorMutOps;
70    ///
71    /// let mut vec = BoolVectorMut::from_iter([true, false, false, true]);
72    /// assert_eq!(vec.len(), 4);
73    /// ```
74    fn from_iter<I>(iter: I) -> Self
75    where
76        I: IntoIterator<Item = bool>,
77    {
78        let buffer = BitBufferMut::from_iter(iter);
79        let validity = MaskMut::new_true(buffer.len());
80
81        BoolVectorMut {
82            bits: buffer,
83            validity,
84        }
85    }
86}
87
88impl FromIterator<bool> for BoolVector {
89    /// Creates a new [`BoolVector`] from an iterator of `bool` values.
90    fn from_iter<I>(iter: I) -> Self
91    where
92        I: IntoIterator<Item = bool>,
93    {
94        let buffer = BitBuffer::from_iter(iter);
95        let validity = Mask::new_true(buffer.len());
96
97        BoolVector {
98            bits: buffer,
99            validity,
100        }
101    }
102}
103
104/// Iterator over a [`BoolVectorMut`] that yields [`Option<bool>`] values.
105///
106/// This iterator is created by calling [`IntoIterator::into_iter`] on a [`BoolVectorMut`].
107///
108/// It consumes the mutable vector and iterates over the elements, yielding `None` for null values
109/// and `Some(value)` for valid values.
110#[derive(Debug)]
111pub struct BoolVectorMutIterator {
112    /// The vector being iterated over.
113    vector: BoolVectorMut,
114    /// The current index into the vector.
115    index: usize,
116}
117
118impl Iterator for BoolVectorMutIterator {
119    type Item = Option<bool>;
120
121    fn next(&mut self) -> Option<Self::Item> {
122        (self.index < self.vector.len()).then(|| {
123            let value = self
124                .vector
125                .validity
126                .value(self.index)
127                .then(|| self.vector.bits.value(self.index));
128            self.index += 1;
129            value
130        })
131    }
132
133    fn size_hint(&self) -> (usize, Option<usize>) {
134        let remaining = self.vector.len() - self.index;
135        (remaining, Some(remaining))
136    }
137}
138
139impl IntoIterator for BoolVectorMut {
140    type Item = Option<bool>;
141    type IntoIter = BoolVectorMutIterator;
142
143    /// Converts the mutable vector into an iterator over `Option<bool>` values.
144    ///
145    /// This method consumes the `BoolVectorMut` and returns an iterator that yields `None` for
146    /// null values and `Some(value)` for valid values.
147    ///
148    /// # Examples
149    ///
150    /// ```
151    /// use vortex_vector::bool::BoolVectorMut;
152    ///
153    /// let vec = BoolVectorMut::from_iter([Some(true), None, Some(false), Some(true)]);
154    /// let collected: Vec<_> = vec.into_iter().collect();
155    /// assert_eq!(collected, vec![Some(true), None, Some(false), Some(true)]);
156    /// ```
157    fn into_iter(self) -> Self::IntoIter {
158        BoolVectorMutIterator {
159            vector: self,
160            index: 0,
161        }
162    }
163}