Skip to content

Commit 3fcccdb

Browse files
authored
Chore: Move stuff around in vectors (#5072)
Resurrected from #5040 This is a purely cosmetic change --------- Signed-off-by: Connor Tsui <[email protected]>
1 parent 1d798bb commit 3fcccdb

File tree

14 files changed

+202
-179
lines changed

14 files changed

+202
-179
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! [`FromIterator`] and related implementations for [`BoolVectorMut`].
5+
6+
use vortex_buffer::BitBufferMut;
7+
use vortex_mask::MaskMut;
8+
9+
use crate::BoolVectorMut;
10+
11+
impl FromIterator<Option<bool>> for BoolVectorMut {
12+
/// Creates a new [`BoolVectorMut`] from an iterator of `Option<bool>` values.
13+
///
14+
/// `None` values will be marked as invalid in the validity mask.
15+
///
16+
/// # Examples
17+
///
18+
/// ```
19+
/// use vortex_vector::{BoolVectorMut, VectorMutOps};
20+
///
21+
/// let mut vec = BoolVectorMut::from_iter([Some(true), None, Some(false)]);
22+
/// assert_eq!(vec.len(), 3);
23+
/// ```
24+
fn from_iter<I>(iter: I) -> Self
25+
where
26+
I: IntoIterator<Item = Option<bool>>,
27+
{
28+
let iter = iter.into_iter();
29+
let (lower_bound, _) = iter.size_hint();
30+
31+
let mut bits = Vec::with_capacity(lower_bound);
32+
let mut validity = MaskMut::with_capacity(lower_bound);
33+
34+
for opt_val in iter {
35+
match opt_val {
36+
Some(val) => {
37+
bits.push(val);
38+
validity.append_n(true, 1);
39+
}
40+
None => {
41+
bits.push(false); // Value doesn't matter for invalid entries.
42+
validity.append_n(false, 1);
43+
}
44+
}
45+
}
46+
47+
BoolVectorMut {
48+
bits: BitBufferMut::from_iter(bits),
49+
validity,
50+
}
51+
}
52+
}
53+
54+
impl FromIterator<bool> for BoolVectorMut {
55+
/// Creates a new [`BoolVectorMut`] from an iterator of `bool` values.
56+
///
57+
/// All values will be treated as non-null.
58+
///
59+
/// # Examples
60+
///
61+
/// ```
62+
/// use vortex_vector::{BoolVectorMut, VectorMutOps};
63+
///
64+
/// let mut vec = BoolVectorMut::from_iter([true, false, false, true]);
65+
/// assert_eq!(vec.len(), 4);
66+
/// ```
67+
fn from_iter<I>(iter: I) -> Self
68+
where
69+
I: IntoIterator<Item = bool>,
70+
{
71+
let buffer = BitBufferMut::from_iter(iter);
72+
let validity = MaskMut::new_true(buffer.len());
73+
74+
BoolVectorMut {
75+
bits: buffer,
76+
validity,
77+
}
78+
}
79+
}

vortex-vector/src/bool/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub use vector::BoolVector;
99
mod vector_mut;
1010
pub use vector_mut::BoolVectorMut;
1111

12+
mod from_iter;
13+
1214
use crate::{Vector, VectorMut};
1315

1416
impl From<BoolVector> for Vector {

vortex-vector/src/bool/vector.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use vortex_buffer::BitBuffer;
77
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
88
use vortex_mask::Mask;
99

10-
use super::BoolVectorMut;
11-
use crate::VectorOps;
10+
use crate::{BoolVectorMut, VectorOps};
1211

1312
/// An immutable vector of boolean values.
1413
///

vortex-vector/src/bool/vector_mut.rs

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use vortex_buffer::BitBufferMut;
77
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
88
use vortex_mask::MaskMut;
99

10-
use super::BoolVector;
11-
use crate::{VectorMutOps, VectorOps};
10+
use crate::{BoolVector, VectorMutOps, VectorOps};
1211

1312
/// A mutable vector of boolean values.
1413
///
@@ -128,76 +127,6 @@ impl BoolVectorMut {
128127
}
129128
}
130129

131-
impl FromIterator<Option<bool>> for BoolVectorMut {
132-
/// Creates a new [`BoolVectorMut`] from an iterator of `Option<bool>` values.
133-
///
134-
/// `None` values will be marked as invalid in the validity mask.
135-
///
136-
/// # Examples
137-
///
138-
/// ```
139-
/// use vortex_vector::{BoolVectorMut, VectorMutOps};
140-
///
141-
/// let mut vec = BoolVectorMut::from_iter([Some(true), None, Some(false)]);
142-
/// assert_eq!(vec.len(), 3);
143-
/// ```
144-
fn from_iter<I>(iter: I) -> Self
145-
where
146-
I: IntoIterator<Item = Option<bool>>,
147-
{
148-
let iter = iter.into_iter();
149-
let (lower_bound, _) = iter.size_hint();
150-
151-
let mut bits = Vec::with_capacity(lower_bound);
152-
let mut validity = MaskMut::with_capacity(lower_bound);
153-
154-
for opt_val in iter {
155-
match opt_val {
156-
Some(val) => {
157-
bits.push(val);
158-
validity.append_n(true, 1);
159-
}
160-
None => {
161-
bits.push(false); // Value doesn't matter for invalid entries.
162-
validity.append_n(false, 1);
163-
}
164-
}
165-
}
166-
167-
BoolVectorMut {
168-
bits: BitBufferMut::from_iter(bits),
169-
validity,
170-
}
171-
}
172-
}
173-
174-
impl FromIterator<bool> for BoolVectorMut {
175-
/// Creates a new [`BoolVectorMut`] from an iterator of `bool` values.
176-
///
177-
/// All values will be treated as non-null.
178-
///
179-
/// # Examples
180-
///
181-
/// ```
182-
/// use vortex_vector::{BoolVectorMut, VectorMutOps};
183-
///
184-
/// let mut vec = BoolVectorMut::from_iter([true, false, false, true]);
185-
/// assert_eq!(vec.len(), 4);
186-
/// ```
187-
fn from_iter<I>(iter: I) -> Self
188-
where
189-
I: IntoIterator<Item = bool>,
190-
{
191-
let buffer = BitBufferMut::from_iter(iter);
192-
let validity = MaskMut::new_true(buffer.len());
193-
194-
BoolVectorMut {
195-
bits: buffer,
196-
validity,
197-
}
198-
}
199-
}
200-
201130
impl VectorMutOps for BoolVectorMut {
202131
type Immutable = BoolVector;
203132

vortex-vector/src/macros.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
//! Helper macros for working with the different variants of [`Vector`](crate::Vector) and
55
//! [`VectorMut`](crate::VectorMut).
6-
//!
7-
//! All macros are exported at the crate level with `#[macro_export]`.
86
97
/// Matches on all variants of [`Vector`] and executes the same code for each variant branch.
108
///
@@ -13,8 +11,8 @@
1311
///
1412
/// # Examples
1513
///
16-
/// ```
17-
/// use vortex_vector::{Vector, BoolVectorMut, NullVector, VectorOps, VectorMutOps, match_each_vector};
14+
/// ```ignore
15+
/// use vortex_vector::{Vector, BoolVectorMut, NullVector, VectorOps, VectorMutOps};
1816
///
1917
/// fn get_vector_length(vector: &Vector) -> usize {
2018
/// match_each_vector!(vector, |v| { v.len() })
@@ -35,7 +33,6 @@
3533
///
3634
/// [`Vector`]: crate::Vector
3735
/// [`VectorOps`]: crate::VectorOps
38-
#[macro_export]
3936
macro_rules! match_each_vector {
4037
($self:expr, | $vec:ident | $body:block) => {{
4138
match $self {
@@ -55,15 +52,17 @@ macro_rules! match_each_vector {
5552
}};
5653
}
5754

55+
pub(crate) use match_each_vector;
56+
5857
/// Matches on all variants of [`VectorMut`] and executes the same code for each variant branch.
5958
///
6059
/// This macro eliminates repetitive match statements when implementing operations that need to work
6160
/// uniformly across all mutable vector type variants.
6261
///
6362
/// # Examples
6463
///
65-
/// ```
66-
/// use vortex_vector::{VectorMut, BoolVectorMut, NullVectorMut, VectorMutOps, match_each_vector_mut};
64+
/// ```ignore
65+
/// use vortex_vector::{VectorMut, BoolVectorMut, NullVectorMut, VectorMutOps};
6766
///
6867
/// fn reserve_space(vector: &mut VectorMut, additional: usize) {
6968
/// match_each_vector_mut!(vector, |v| { v.reserve(additional) })
@@ -84,7 +83,6 @@ macro_rules! match_each_vector {
8483
///
8584
/// [`VectorMut`]: crate::VectorMut
8685
/// [`VectorMutOps`]: crate::VectorMutOps
87-
#[macro_export]
8886
macro_rules! match_each_vector_mut {
8987
($self:expr, | $vec:ident | $body:block) => {{
9088
match $self {
@@ -103,3 +101,5 @@ macro_rules! match_each_vector_mut {
103101
}
104102
}};
105103
}
104+
105+
pub(crate) use match_each_vector_mut;

vortex-vector/src/null/vector_mut.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
//! Definition and implementation of [`NullVectorMut`].
55
6-
use super::NullVector;
7-
use crate::VectorMutOps;
6+
use crate::{NullVector, VectorMutOps};
87

98
/// A mutable vector of null values.
109
///
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! [`FromIterator`] and related implementations for [`PVectorMut<T>`].
5+
6+
use vortex_buffer::BufferMut;
7+
use vortex_dtype::NativePType;
8+
use vortex_mask::MaskMut;
9+
10+
use crate::PVectorMut;
11+
12+
impl<T: NativePType> FromIterator<Option<T>> for PVectorMut<T> {
13+
/// Creates a new [`PVectorMut<T>`] from an iterator of `Option<T>` values.
14+
///
15+
/// `None` values will be marked as invalid in the validity mask.
16+
///
17+
/// # Examples
18+
///
19+
/// ```
20+
/// use vortex_vector::{PVectorMut, VectorMutOps};
21+
///
22+
/// let mut vec = PVectorMut::<i32>::from_iter([Some(1), None, Some(3)]);
23+
/// assert_eq!(vec.len(), 3);
24+
/// ```
25+
fn from_iter<I>(iter: I) -> Self
26+
where
27+
I: IntoIterator<Item = Option<T>>,
28+
{
29+
let iter = iter.into_iter();
30+
let (lower_bound, _) = iter.size_hint();
31+
32+
let mut elements = Vec::with_capacity(lower_bound);
33+
let mut validity = MaskMut::with_capacity(lower_bound);
34+
35+
for opt_val in iter {
36+
match opt_val {
37+
Some(val) => {
38+
elements.push(val);
39+
validity.append_n(true, 1);
40+
}
41+
None => {
42+
elements.push(T::default()); // Use default for invalid entries.
43+
validity.append_n(false, 1);
44+
}
45+
}
46+
}
47+
48+
PVectorMut {
49+
elements: BufferMut::from_iter(elements),
50+
validity,
51+
}
52+
}
53+
}
54+
55+
impl<T: NativePType> FromIterator<T> for PVectorMut<T> {
56+
/// Creates a new [`PVectorMut<T>`] from an iterator of `T` values.
57+
///
58+
/// All values will be treated as non-null.
59+
///
60+
/// # Examples
61+
///
62+
/// ```
63+
/// use vortex_vector::{PVectorMut, VectorMutOps};
64+
///
65+
/// let mut vec = PVectorMut::<i32>::from_iter([1, 2, 3, 4]);
66+
/// assert_eq!(vec.len(), 4);
67+
/// ```
68+
fn from_iter<I>(iter: I) -> Self
69+
where
70+
I: IntoIterator<Item = T>,
71+
{
72+
let buffer = BufferMut::from_iter(iter);
73+
let validity = MaskMut::new_true(buffer.len());
74+
75+
PVectorMut {
76+
elements: buffer,
77+
validity,
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)