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
10 changes: 10 additions & 0 deletions vortex-buffer/src/bit/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use crate::{BitBuffer, BufferMut, ByteBufferMut, buffer_mut};
#[derive(Debug, Clone, Eq)]
pub struct BitBufferMut {
buffer: ByteBufferMut,
/// Represents the offset of the bit buffer into the first byte.
///
/// This is always less than 8 (for when the bit buffer is not aligned to a byte).
offset: usize,
len: usize,
}
Expand Down Expand Up @@ -162,6 +165,13 @@ impl BitBufferMut {
self.buffer.reserve(additional_bytes);
}

/// Clears the bit buffer (but keeps any allocated memory).
pub fn clear(&mut self) {
// Since there are no items we need to drop, we simply set the length to 0.
self.len = 0;
self.offset = 0;
}

/// Set the bit at `index` to the given boolean value.
///
/// This operation is checked so if `index` exceeds the buffer length, this will panic.
Expand Down
57 changes: 57 additions & 0 deletions vortex-mask/src/mask_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::ops::Sub;
use std::sync::Arc;

use vortex_buffer::BitBufferMut;
use vortex_error::vortex_panic;

use crate::Mask;

Expand Down Expand Up @@ -56,6 +57,29 @@ impl MaskMut {
})
}

/// Returns the boolean value at a given index.
///
/// # Panics
///
/// Panics if the index is out of bounds.
pub fn value(&self, index: usize) -> bool {
match &self.0 {
Inner::Empty { .. } => {
vortex_panic!("index out of bounds: the length is 0 but the index is {index}")
}
Inner::Constant { value, len, .. } => {
assert!(
index < *len,
"index out of bounds: the length is {} but the index is {index}",
*len
);

*value
}
Inner::Builder(bit_buffer) => bit_buffer.value(index),
}
}

/// Reserve capacity for at least `additional` more values to be appended.
pub fn reserve(&mut self, additional: usize) {
match &mut self.0 {
Expand All @@ -71,6 +95,39 @@ impl MaskMut {
}
}

/// Clears the mask.
///
/// Note that this method has no effect on the allocated capacity of the mask.
pub fn clear(&mut self) {
match &mut self.0 {
Inner::Empty { .. } => {}
Inner::Constant { capacity, .. } => {
self.0 = Inner::Empty {
capacity: *capacity,
}
}
Inner::Builder(bit_buffer) => bit_buffer.clear(),
};
}

/// Shortens the mask, keeping the first `len` bits.
///
/// If `len` is greater or equal to the vector’s current length, this has no effect.
///
/// Note that this method has no effect on the allocated capacity of the mask.
pub fn truncate(&mut self, len: usize) {
let truncated_len = len;
if truncated_len > self.len() {
return;
}

match &mut self.0 {
Inner::Empty { .. } => {}
Inner::Constant { len, .. } => *len = truncated_len.min(*len),
Inner::Builder(bit_buffer) => bit_buffer.truncate(truncated_len),
};
}

/// Append n values to the mask.
pub fn append_n(&mut self, new_value: bool, n: usize) {
match &mut self.0 {
Expand Down
81 changes: 0 additions & 81 deletions vortex-vector/src/primitive/from_iter.rs

This file was deleted.

36 changes: 36 additions & 0 deletions vortex-vector/src/primitive/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@ impl<T: NativePType> PVector<T> {

Self { elements, validity }
}

/// Gets a nullable element at the given index.
///
/// If the element at the given index is null, returns `None`. Otherwise, returns `Some(x)`,
/// where `x: T`.
///
/// # Panics
///
/// Panics if the index is out of bounds.
pub fn get(&self, index: usize) -> Option<T> {
self.validity.value(index).then(|| self.elements[index])
}

/// Returns the internal [`Buffer`] of the [`PVector`].
///
/// Note that the internal buffer may hold garbage data in place of nulls. That information is
/// tracked by the [`validity()`](Self::validity).
#[inline]
pub fn elements(&self) -> &Buffer<T> {
&self.elements
}
}

impl<T: NativePType> AsRef<[T]> for PVector<T> {
/// Returns an immutable slice over the internal buffer with elements of type `T`.
///
/// Note that this slice may contain garbage data where the [`validity()`] mask states that an
/// element is invalid.
///
/// The caller should check the [`validity()`] before performing any operations.
///
/// [`validity()`]: crate::VectorOps::validity
#[inline]
fn as_ref(&self) -> &[T] {
self.elements.as_slice()
}
}

impl<T: NativePType> VectorOps for PVector<T> {
Expand Down
6 changes: 3 additions & 3 deletions vortex-vector/src/primitive/generic_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::{PVector, VectorMutOps, VectorOps};
/// `T` is expected to be bound by [`NativePType`], which templates an internal [`BufferMut<T>`]
/// that stores the elements of the vector.
///
/// `PVectorMut<T>` is the primary way to construct primitive vectors. It provides efficient methods
/// for building vectors incrementally before converting them to an immutable [`PVector<T>`] using
/// the [`freeze`](crate::VectorMutOps::freeze) method.
/// [`PVectorMut<T>`] is the primary way to construct primitive vectors. It provides efficient
/// methods for building vectors incrementally before converting them to an immutable [`PVector<T>`]
/// using the [`freeze`](crate::VectorMutOps::freeze) method.
///
/// # Examples
///
Expand Down
3 changes: 1 addition & 2 deletions vortex-vector/src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ pub use generic_mut::PVectorMut;
mod vector;
pub use vector::PrimitiveVector;

mod pvector_impl;
mod vector_mut;
pub use vector_mut::PrimitiveVectorMut;

mod from_iter;

mod macros;

use vortex_dtype::NativePType;
Expand Down
Loading
Loading