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