vortex_vector/null/
vector_mut.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition and implementation of [`NullVectorMut`].
5
6use vortex_mask::MaskMut;
7
8use crate::VectorMutOps;
9use crate::null::NullScalar;
10use crate::null::NullVector;
11
12/// A mutable vector of null values.
13///
14/// Since a "null" value does not require any data storage, the nulls are stored internally with a
15/// single `length` counter.
16///
17/// The immutable equivalent of this type is [`NullVector`].
18#[derive(Debug, Clone)]
19pub struct NullVectorMut {
20    /// The total number of nulls.
21    pub(super) len: usize,
22    /// The validity mask. We only store this in order to implement the
23    /// [`validity()`](Self::validity) method.
24    pub(super) validity: MaskMut,
25}
26
27impl NullVectorMut {
28    /// Creates a new mutable vector of nulls with the given length.
29    pub fn new(len: usize) -> Self {
30        Self {
31            len,
32            validity: MaskMut::new_false(len),
33        }
34    }
35}
36
37impl VectorMutOps for NullVectorMut {
38    type Immutable = NullVector;
39
40    fn len(&self) -> usize {
41        self.len
42    }
43
44    fn validity(&self) -> &MaskMut {
45        &self.validity
46    }
47
48    fn capacity(&self) -> usize {
49        usize::MAX
50    }
51
52    fn reserve(&mut self, _additional: usize) {
53        // We do not allocate memory for `NullVector`, so this is a no-op.
54    }
55
56    fn clear(&mut self) {
57        self.len = 0;
58    }
59
60    fn truncate(&mut self, len: usize) {
61        self.len = self.len.min(len);
62    }
63
64    fn extend_from_vector(&mut self, other: &NullVector) {
65        self.len += other.len;
66    }
67
68    fn append_nulls(&mut self, n: usize) {
69        self.len += n;
70    }
71
72    fn append_zeros(&mut self, n: usize) {
73        self.len += n;
74    }
75
76    fn append_scalars(&mut self, _scalar: &NullScalar, n: usize) {
77        self.len += n;
78    }
79
80    fn freeze(self) -> NullVector {
81        NullVector::new(self.len)
82    }
83
84    fn split_off(&mut self, at: usize) -> Self {
85        assert!(
86            at <= self.capacity(),
87            "split_off out of bounds: {:?} <= {:?}",
88            at,
89            self.capacity(),
90        );
91
92        let new_len = self.len.saturating_sub(at);
93        self.len = std::cmp::min(self.len, at);
94        NullVectorMut {
95            len: new_len,
96            validity: MaskMut::new_false(new_len),
97        }
98    }
99
100    fn unsplit(&mut self, other: Self) {
101        self.len += other.len;
102    }
103}