Skip to content
Closed
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
2 changes: 1 addition & 1 deletion vortex-vector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ mod vector_mut;
pub use bool::{BoolVector, BoolVectorMut};
pub use null::{NullVector, NullVectorMut};
pub use ops::{VectorMutOps, VectorOps};
pub use primitive::{PVector, PVectorMut, PrimitiveVector, PrimitiveVectorMut};
pub use primitive::{PVec, PVecMut, PrimitiveVector, PrimitiveVectorMut};
pub use vector::Vector;
pub use vector_mut::VectorMut;
24 changes: 12 additions & 12 deletions vortex-vector/src/primitive/from_iter.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! [`FromIterator`] and related implementations for [`PVectorMut<T>`].
//! [`FromIterator`] and related implementations for [`PVecMut<T>`].

use vortex_buffer::BufferMut;
use vortex_dtype::NativePType;
use vortex_mask::MaskMut;

use crate::PVectorMut;
use crate::PVecMut;

impl<T: NativePType> FromIterator<Option<T>> for PVectorMut<T> {
/// Creates a new [`PVectorMut<T>`] from an iterator of `Option<T>` values.
impl<T: NativePType> FromIterator<Option<T>> for PVecMut<T> {
/// Creates a new [`PVecMut<T>`] from an iterator of `Option<T>` values.
///
/// `None` values will be marked as invalid in the validity mask.
///
/// # Examples
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// let mut vec = PVectorMut::<i32>::from_iter([Some(1), None, Some(3)]);
/// let mut vec = PVecMut::<i32>::from_iter([Some(1), None, Some(3)]);
/// assert_eq!(vec.len(), 3);
/// ```
fn from_iter<I>(iter: I) -> Self
Expand All @@ -45,24 +45,24 @@ impl<T: NativePType> FromIterator<Option<T>> for PVectorMut<T> {
}
}

PVectorMut {
PVecMut {
elements: BufferMut::from_iter(elements),
validity,
}
}
}

impl<T: NativePType> FromIterator<T> for PVectorMut<T> {
/// Creates a new [`PVectorMut<T>`] from an iterator of `T` values.
impl<T: NativePType> FromIterator<T> for PVecMut<T> {
/// Creates a new [`PVecMut<T>`] from an iterator of `T` values.
///
/// All values will be treated as non-null.
///
/// # Examples
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// let mut vec = PVectorMut::<i32>::from_iter([1, 2, 3, 4]);
/// let mut vec = PVecMut::<i32>::from_iter([1, 2, 3, 4]);
/// assert_eq!(vec.len(), 4);
/// ```
fn from_iter<I>(iter: I) -> Self
Expand All @@ -72,7 +72,7 @@ impl<T: NativePType> FromIterator<T> for PVectorMut<T> {
let buffer = BufferMut::from_iter(iter);
let validity = MaskMut::new_true(buffer.len());

PVectorMut {
PVecMut {
elements: buffer,
validity,
}
Expand Down
36 changes: 18 additions & 18 deletions vortex-vector/src/primitive/generic.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Definition and implementation of [`PVector<T>`].
//! Definition and implementation of [`PVec<T>`].

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

use crate::{PVectorMut, VectorOps};
use crate::{PVecMut, VectorOps};

/// An immutable vector of generic primitive values.
///
/// `T` is expected to be bound by [`NativePType`], which templates an internal [`Buffer<T>`] that
/// stores the elements of the vector.
///
/// `PVector<T>` can be considered a borrowed / frozen version of [`PVectorMut<T>`], which is
/// `PVec<T>` can be considered a borrowed / frozen version of [`PVecMut<T>`], which is
/// created via the [`freeze`](crate::VectorMutOps::freeze) method.
///
/// See the documentation for [`PVectorMut<T>`] for more information.
/// See the documentation for [`PVecMut<T>`] for more information.
#[derive(Debug, Clone)]
pub struct PVector<T> {
pub struct PVec<T> {
/// The buffer representing the vector elements.
pub(super) elements: Buffer<T>,
/// The validity mask (where `true` represents an element is **not** null).
pub(super) validity: Mask,
}

impl<T: NativePType> PVector<T> {
/// Creates a new [`PVector<T>`] from the given elements buffer and validity mask.
impl<T: NativePType> PVec<T> {
/// Creates a new [`PVec<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")
.vortex_expect("`PVec` validity mask must have the same length as elements")
}

/// Tries to create a new [`PVector<T>`] from the given elements buffer and validity mask.
/// Tries to create a new [`PVec<T>`] from the given elements buffer and validity mask.
///
/// # Errors
///
Expand All @@ -47,13 +47,13 @@ impl<T: NativePType> PVector<T> {
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"
"`PVec` 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
/// Creates a new [`PVec<T>`] from the given elements buffer and validity mask without
/// validation.
///
/// # Safety
Expand All @@ -63,15 +63,15 @@ impl<T: NativePType> PVector<T> {
debug_assert_eq!(
validity.len(),
elements.len(),
"`PVector` validity mask must have the same length as elements"
"`PVec` validity mask must have the same length as elements"
);

Self { elements, validity }
}
}

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

fn len(&self) -> usize {
self.elements.len()
Expand All @@ -82,23 +82,23 @@ impl<T: NativePType> VectorOps for PVector<T> {
}

/// Try to convert self into a mutable vector.
fn try_into_mut(self) -> Result<PVectorMut<T>, Self> {
fn try_into_mut(self) -> Result<PVecMut<T>, Self> {
let elements = match self.elements.try_into_mut() {
Ok(elements) => elements,
Err(elements) => {
return Err(PVector {
return Err(PVec {
elements,
validity: self.validity,
});
}
};

match self.validity.try_into_mut() {
Ok(validity_mut) => Ok(PVectorMut {
Ok(validity_mut) => Ok(PVecMut {
elements,
validity: validity_mut,
}),
Err(validity) => Err(PVector {
Err(validity) => Err(PVec {
elements: elements.freeze(),
validity,
}),
Expand Down
62 changes: 31 additions & 31 deletions vortex-vector/src/primitive/generic_mut.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Definition and implementation of [`PVectorMut<T>`].
//! Definition and implementation of [`PVecMut<T>`].

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

use crate::{PVector, VectorMutOps, VectorOps};
use crate::{PVec, VectorMutOps, VectorOps};

/// A mutable vector of generic primitive values.
///
/// `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
/// `PVecMut<T>` is the primary way to construct primitive vectors. It provides efficient methods
/// for building vectors incrementally before converting them to an immutable [`PVec<T>`] using
/// the [`freeze`](crate::VectorMutOps::freeze) method.
///
/// # Examples
///
/// ## Creating and building a vector
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// // Create with initial capacity for i32 values.
/// let mut vec = PVectorMut::<i32>::with_capacity(10);
/// let mut vec = PVecMut::<i32>::with_capacity(10);
/// assert_eq!(vec.len(), 0);
/// assert!(vec.capacity() >= 10);
///
/// // Create from an iterator of optional values.
/// let mut vec = PVectorMut::<i32>::from_iter([Some(1), None, Some(3)]);
/// let mut vec = PVecMut::<i32>::from_iter([Some(1), None, Some(3)]);
/// assert_eq!(vec.len(), 3);
///
/// // Works with different primitive types.
/// let mut f64_vec = PVectorMut::<f64>::from_iter([1.5, 2.5, 3.5].map(Some));
/// let mut f64_vec = PVecMut::<f64>::from_iter([1.5, 2.5, 3.5].map(Some));
/// assert_eq!(f64_vec.len(), 3);
/// ```
///
/// ## Extending and appending
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// let mut vec1 = PVectorMut::<i32>::from_iter([1, 2].map(Some));
/// let vec2 = PVectorMut::<i32>::from_iter([3, 4].map(Some)).freeze();
/// let mut vec1 = PVecMut::<i32>::from_iter([1, 2].map(Some));
/// let vec2 = PVecMut::<i32>::from_iter([3, 4].map(Some)).freeze();
///
/// // Extend from another vector.
/// vec1.extend_from_vector(&vec2);
Expand All @@ -60,9 +60,9 @@ use crate::{PVector, VectorMutOps, VectorOps};
/// ## Splitting and unsplitting
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// let mut vec = PVectorMut::<i64>::from_iter([10, 20, 30, 40, 50].map(Some));
/// let mut vec = PVecMut::<i64>::from_iter([10, 20, 30, 40, 50].map(Some));
///
/// // Split the vector at index 3.
/// let mut second_half = vec.split_off(3);
Expand All @@ -77,10 +77,10 @@ use crate::{PVector, VectorMutOps, VectorOps};
/// ## Working with nulls
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps};
/// use vortex_vector::{PVecMut, VectorMutOps};
///
/// // Create a vector with some null values.
/// let mut vec = PVectorMut::<u32>::from_iter([Some(100), None, Some(200), None]);
/// let mut vec = PVecMut::<u32>::from_iter([Some(100), None, Some(200), None]);
/// assert_eq!(vec.len(), 4);
///
/// // Add more nulls.
Expand All @@ -91,34 +91,34 @@ use crate::{PVector, VectorMutOps, VectorOps};
/// ## Converting to immutable
///
/// ```
/// use vortex_vector::{PVectorMut, VectorMutOps, VectorOps};
/// use vortex_vector::{PVecMut, VectorMutOps, VectorOps};
///
/// let mut vec = PVectorMut::<f32>::from_iter([1.0, 2.0, 3.0].map(Some));
/// let mut vec = PVecMut::<f32>::from_iter([1.0, 2.0, 3.0].map(Some));
///
/// // Freeze into an immutable vector.
/// let immutable = vec.freeze();
/// assert_eq!(immutable.len(), 3);
/// ```
#[derive(Debug, Clone)]
pub struct PVectorMut<T> {
pub struct PVecMut<T: NativePType> {
/// The mutable buffer representing the vector elements.
pub(super) elements: BufferMut<T>,
/// The validity mask (where `true` represents an element is **not** null).
pub(super) validity: MaskMut,
}

impl<T> PVectorMut<T> {
/// Creates a new [`PVectorMut<T>`] from the given elements buffer and validity mask.
impl<T: NativePType> PVecMut<T> {
/// Creates a new [`PVecMut<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")
.vortex_expect("`PVecMut` validity mask must have the same length as elements")
}

/// Tries to create a new [`PVectorMut<T>`] from the given elements buffer and validity mask.
/// Tries to create a new [`PVecMut<T>`] from the given elements buffer and validity mask.
///
/// # Errors
///
Expand All @@ -127,13 +127,13 @@ impl<T> PVectorMut<T> {
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"
"`PVecMut` 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
/// Creates a new [`PVecMut<T>`] from the given elements buffer and validity mask without
/// validation.
///
/// # Safety
Expand All @@ -146,7 +146,7 @@ impl<T> PVectorMut<T> {
debug_assert_eq!(
elements.len(),
validity.len(),
"`PVectorMut` validity mask must have the same length as elements"
"`PVecMut` validity mask must have the same length as elements"
);

Self { elements, validity }
Expand All @@ -161,8 +161,8 @@ impl<T> PVectorMut<T> {
}
}

impl<T: NativePType> VectorMutOps for PVectorMut<T> {
type Immutable = PVector<T>;
impl<T: NativePType> VectorMutOps for PVecMut<T> {
type Immutable = PVec<T>;

fn len(&self) -> usize {
self.elements.len()
Expand All @@ -178,7 +178,7 @@ impl<T: NativePType> VectorMutOps for PVectorMut<T> {
}

/// Extends the vector by appending elements from another vector.
fn extend_from_vector(&mut self, other: &PVector<T>) {
fn extend_from_vector(&mut self, other: &PVec<T>) {
self.elements.extend_from_slice(other.elements.as_slice());
self.validity.append_mask(other.validity());
}
Expand All @@ -189,15 +189,15 @@ impl<T: NativePType> VectorMutOps for PVectorMut<T> {
}

/// Freeze the vector into an immutable one.
fn freeze(self) -> PVector<T> {
PVector {
fn freeze(self) -> PVec<T> {
PVec {
elements: self.elements.freeze(),
validity: self.validity.freeze(),
}
}

fn split_off(&mut self, at: usize) -> Self {
PVectorMut {
PVecMut {
elements: self.elements.split_off(at),
validity: self.validity.split_off(at),
}
Expand Down
Loading
Loading