Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions vortex-vector/src/bool/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! Definition and implementation of [`BoolVector`].

use vortex_buffer::BitBuffer;
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
use vortex_mask::Mask;

use super::BoolVectorMut;
Expand All @@ -30,11 +31,36 @@ impl BoolVector {
///
/// Panics if the length of the validity mask does not match the length of the bits.
pub fn new(bits: BitBuffer, validity: Mask) -> Self {
assert_eq!(
Self::try_new(bits, validity)
.vortex_expect("`BoolVector` validity mask must have the same length as bits")
}

/// Tries to create a new [`BoolVector`] from the given bits and validity mask.
///
/// # Errors
///
/// Returns an error if the length of the validity mask does not match the length of the bits.
pub fn try_new(bits: BitBuffer, validity: Mask) -> VortexResult<Self> {
vortex_ensure!(
validity.len() == bits.len(),
"`BoolVector` validity mask must have the same length as bits"
);

Ok(Self { bits, validity })
}

/// Creates a new [`BoolVector`] from the given bits and validity mask without validation.
///
/// # Safety
///
/// The caller must ensure that the validity mask has the same length as the bits.
pub fn new_unchecked(bits: BitBuffer, validity: Mask) -> Self {
debug_assert_eq!(
validity.len(),
bits.len(),
"BoolVector validity mask must have the same length as bits"
"`BoolVector` validity mask must have the same length as bits"
);

Self { bits, validity }
}

Expand Down
43 changes: 37 additions & 6 deletions vortex-vector/src/bool/vector_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! Definition and implementation of [`BoolVectorMut`].

use vortex_buffer::BitBufferMut;
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
use vortex_mask::MaskMut;

use super::BoolVector;
Expand Down Expand Up @@ -71,15 +72,45 @@ pub struct BoolVectorMut {
}

impl BoolVectorMut {
/// Create a mutable vector from the given parts, without checking lengths or capacities.
/// Creates a new [`BoolVectorMut`] from the given bits and validity mask.
///
/// # SAFETY
/// # Panics
///
/// The caller must ensure both parts have the same length and capacity. Ideally they are
/// taken from `into_parts`, mutated in a way that doesn't re-allocate, and then passed back
/// to this function.
/// Panics if the length of the validity mask does not match the length of the bits.
pub fn new(bits: BitBufferMut, validity: MaskMut) -> Self {
Self::try_new(bits, validity)
.vortex_expect("`BoolVector` validity mask must have the same length as bits")
}

/// Tries to create a new [`BoolVectorMut`] from the given bits and validity mask.
///
/// # Errors
///
/// Returns an error if the length of the validity mask does not match the length of the bits.
pub fn try_new(bits: BitBufferMut, validity: MaskMut) -> VortexResult<Self> {
vortex_ensure!(
validity.len() == bits.len(),
"`BoolVector` validity mask must have the same length as bits"
);

Ok(Self { bits, validity })
}

/// Creates a new [`BoolVectorMut`] from the given bits and validity mask without validation.
///
/// # Safety
///
/// The caller must ensure that the validity mask has the same length as the bits.
///
/// Ideally, they are taken from `into_parts`, mutated in a way that doesn't re-allocate, and
/// then passed back to this function.
pub unsafe fn new_unchecked(bits: BitBufferMut, validity: MaskMut) -> Self {
debug_assert_eq!(bits.len(), validity.len());
debug_assert_eq!(
bits.len(),
validity.len(),
"`BoolVector` validity mask must have the same length as bits"
);

Self { bits, validity }
}

Expand Down
44 changes: 44 additions & 0 deletions vortex-vector/src/primitive/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use vortex_buffer::Buffer;
use vortex_dtype::NativePType;
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
use vortex_mask::Mask;

use crate::{PVectorMut, VectorOps};
Expand All @@ -26,6 +27,49 @@ pub struct PVector<T> {
pub(super) validity: Mask,
}

impl<T: NativePType> PVector<T> {
/// Creates a new [`PVector<T>`] from the given elements buffer and validity mask.
///
/// # Panics
///
/// Panics if the length of the validity mask does not match the length of the elements buffer.
pub fn new(elements: Buffer<T>, validity: Mask) -> Self {
Self::try_new(elements, validity)
.vortex_expect("`PVector` validity mask must have the same length as elements")
}

/// Tries to create a new [`PVector<T>`] from the given elements buffer and validity mask.
///
/// # Errors
///
/// Returns an error if the length of the validity mask does not match the length of the
/// elements buffer.
pub fn try_new(elements: Buffer<T>, validity: Mask) -> VortexResult<Self> {
vortex_ensure!(
validity.len() == elements.len(),
"`PVector` validity mask must have the same length as elements"
);

Ok(Self { elements, validity })
}

/// Creates a new [`PVector<T>`] from the given elements buffer and validity mask without
/// validation.
///
/// # Safety
///
/// The caller must ensure that the validity mask has the same length as the elements buffer.
pub fn new_unchecked(elements: Buffer<T>, validity: Mask) -> Self {
debug_assert_eq!(
validity.len(),
elements.len(),
"`PVector` validity mask must have the same length as elements"
);

Self { elements, validity }
}
}

impl<T: NativePType> VectorOps for PVector<T> {
type Mutable = PVectorMut<T>;

Expand Down
45 changes: 45 additions & 0 deletions vortex-vector/src/primitive/generic_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use vortex_buffer::BufferMut;
use vortex_dtype::NativePType;
use vortex_error::{VortexExpect, VortexResult, vortex_ensure};
use vortex_mask::MaskMut;

use crate::{PVector, VectorMutOps, VectorOps};
Expand Down Expand Up @@ -107,6 +108,50 @@ pub struct PVectorMut<T> {
}

impl<T> PVectorMut<T> {
/// Creates a new [`PVectorMut<T>`] from the given elements buffer and validity mask.
///
/// # Panics
///
/// Panics if the length of the validity mask does not match the length of the elements buffer.
pub fn new(elements: BufferMut<T>, validity: MaskMut) -> Self {
Self::try_new(elements, validity)
.vortex_expect("`PVectorMut` validity mask must have the same length as elements")
}

/// Tries to create a new [`PVectorMut<T>`] from the given elements buffer and validity mask.
///
/// # Errors
///
/// Returns an error if the length of the validity mask does not match the length of the
/// elements buffer.
pub fn try_new(elements: BufferMut<T>, validity: MaskMut) -> VortexResult<Self> {
vortex_ensure!(
validity.len() == elements.len(),
"`PVectorMut` validity mask must have the same length as elements"
);

Ok(Self { elements, validity })
}

/// Creates a new [`PVectorMut<T>`] from the given elements buffer and validity mask without
/// validation.
///
/// # Safety
///
/// The caller must ensure that the validity mask has the same length as the elements buffer.
///
/// Ideally, they are taken from `into_parts`, mutated in a way that doesn't re-allocate, and
/// then passed back to this function.
pub unsafe fn new_unchecked(elements: BufferMut<T>, validity: MaskMut) -> Self {
debug_assert_eq!(
elements.len(),
validity.len(),
"`PVectorMut` validity mask must have the same length as elements"
);

Self { elements, validity }
}

/// Create a new mutable primitive vector with the given capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
Expand Down
Loading