LVec

Struct LVec 

Source
pub struct LVec<T: Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

A parallelized vector container with deferred execution.

LVec is the main data structure in Leopard. It represents a vector that supports parallel operations through deferred execution. Operations on LVec are recorded when called between LQueue::start and LQueue::end, and executed in bulk when end() is called.

§Creating LVec

LVec instances must be created through an LQueue:

use leopard::{LQueue, LVec};

let q = LQueue::new();
let vec: LVec<f64> = q.lvec_with_capacity(1000);

§Operations

All operations must be called between q.start() and q.end():

§Retrieving Results

After q.end(), use materialize to get the computed data:

use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<f64> = q.lvec_with_capacity(10);

q.start();
let x = x.fill_with(|i| i as f64);
q.end();

let data = x.materialize().unwrap();
println!("{:?}", &data[..]);

§Pending State

After an operation is recorded, the resulting LVec is in a “pending” state. The actual data is not available until after q.end() is called and materialize is used to retrieve it.

Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> LVec<T>

Source

pub fn len(&self) -> usize

Returns the length of the vector.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let vec: LVec<f64> = q.lvec_with_capacity(100);
assert_eq!(vec.len(), 100);
Source

pub fn is_empty(&self) -> bool

Returns true if the vector has no elements.

Source

pub fn capacity(&self) -> usize

Returns the capacity of the vector.

Source

pub fn is_pending(&self) -> bool

Returns true if this vector has a pending operation.

A pending vector’s data is not yet available. Call materialize after LQueue::end to retrieve the computed data.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<f64> = q.lvec_with_capacity(10);
assert!(!x.is_pending());

q.start();
let y = x.fill(1.0);
assert!(y.is_pending());
q.end();
Source

pub fn materialize(&self) -> Option<Arc<Vec<T>>>

Retrieves the computed data after LQueue::end has been called.

For pending vectors (those created by operations), this returns the computed result. For non-pending vectors, this returns the original data.

§Returns
  • Some(Arc<Vec<T>>) if the data is available
  • None if the vector is pending and q.end() hasn’t been called
§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<f64> = q.lvec_with_capacity(5);

q.start();
let x = x.fill_with(|i| i as f64 * 2.0);
q.end();

let data = x.materialize().unwrap();
assert_eq!(&data[..], &[0.0, 2.0, 4.0, 6.0, 8.0]);
Source

pub fn as_slice(&self) -> &[T]

Returns the data as a slice.

§Warning

For pending vectors, this returns an empty or default-initialized slice, not the computed result. Use materialize after LQueue::end to get the actual computed data.

Source

pub fn fill_with<F>(&self, f: F) -> Self
where F: Fn(usize) -> T + Send + Sync + 'static,

Fills the vector using a closure that takes an index.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • f - A closure that takes an index usize and returns the value T
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<f64> = q.lvec_with_capacity(5);

q.start();
let x = x.fill_with(|i| i as f64 * 10.0);
q.end();

let data = x.materialize().unwrap();
assert_eq!(&data[..], &[0.0, 10.0, 20.0, 30.0, 40.0]);
Source

pub fn fill(&self, value: T) -> Self

Fills the vector with a constant value.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • value - The value to fill the vector with
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<i32> = q.lvec_with_capacity(5);

q.start();
let x = x.fill(42);
q.end();

let data = x.materialize().unwrap();
assert_eq!(&data[..], &[42, 42, 42, 42, 42]);
Source

pub fn map<F>(&self, f: F) -> Self
where F: Fn(usize, &T) -> T + Send + Sync + 'static,

Transforms each element using a closure.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • f - A closure that takes an index and a reference to the element, and returns the transformed value
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<f64> = q.lvec_with_capacity(5);

q.start();
let x = x.fill_with(|i| i as f64);
let y = x.map(|_, v| v * v);  // Square each element
q.end();

let data = y.materialize().unwrap();
assert_eq!(&data[..], &[0.0, 1.0, 4.0, 9.0, 16.0]);
Source

pub fn map_where<C, TF, FF>( &self, condition: C, if_true: TF, if_false: FF, ) -> Self
where C: Fn(usize, &T) -> bool + Send + Sync + 'static, TF: Fn(usize, &T) -> T + Send + Sync + 'static, FF: Fn(usize, &T) -> T + Send + Sync + 'static,

Applies different transformations based on a condition.

For each element, if the condition returns true, if_true is applied; otherwise, if_false is applied. This is SIMD-style branchless conditional execution.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • condition - A closure that returns true or false for each element
  • if_true - Transformation to apply when condition is true
  • if_false - Transformation to apply when condition is false
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<i32> = q.lvec_with_capacity(6);

q.start();
let x = x.fill_with(|i| i as i32 + 1);
let y = x.map_where(
    |_, v| *v % 2 == 0,  // Is even?
    |_, v| v * 10,       // If even: multiply by 10
    |_, v| v + 1000,     // If odd: add 1000
);
q.end();

let data = y.materialize().unwrap();
// [1, 2, 3, 4, 5, 6] -> [1001, 20, 1003, 40, 1005, 60]
Source

pub fn mask<F>(&self, predicate: F) -> LMask
where F: Fn(usize, &T) -> bool,

Creates a mask from a predicate applied to each element.

Unlike other operations, this executes immediately (not recorded) because masks are needed to define subsequent masking operations.

§Arguments
  • predicate - A closure that returns true or false for each element
§Warning

This only works on non-pending vectors. For pending vectors, use LMask::from_fn with the appropriate pattern.

§Examples
use leopard::{LQueue, LVec};

let q = LQueue::new();
let x: LVec<i32> = q.lvec_with_capacity(10);

// Note: mask() works on the initial (non-pending) vector
let mask = x.mask(|i, _| i >= 5);
Source

pub fn blend(&self, other: &Self, mask: &LMask) -> Self

Blends two vectors using a mask (SIMD-style select).

For each element, if the mask is true, the value from other is used; otherwise, the value from self is used.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • other - The vector to blend with
  • mask - The mask determining which vector to select from
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec, LMask};

let q = LQueue::new();
let a: LVec<i32> = q.lvec_with_capacity(5);
let b: LVec<i32> = q.lvec_with_capacity(5);
let mask = LMask::from_fn(5, |i| i >= 3);

q.start();
let a = a.fill(1);
let b = b.fill(100);
let c = a.blend(&b, &mask);
q.end();

let data = c.materialize().unwrap();
// mask: [false, false, false, true, true]
// result: [1, 1, 1, 100, 100]
Source

pub fn select(mask: &LMask, if_true: &Self, if_false: &Self) -> Self

Selects between two vectors based on a mask.

This is equivalent to if_false.blend(if_true, mask).

§Arguments
  • mask - The mask determining which vector to select from
  • if_true - Values to use where mask is true
  • if_false - Values to use where mask is false
§Panics

Panics if called outside of a start()/end() block.

Source

pub fn masked_apply<F>(&self, mask: &LMask, f: F) -> Self
where F: Fn(usize, &T) -> T + Send + Sync + 'static,

Applies a function only where the mask is true.

Elements where the mask is false retain their original values.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • mask - The mask determining where to apply the function
  • f - The function to apply
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec, LMask};

let q = LQueue::new();
let x: LVec<i32> = q.lvec_with_capacity(5);
let mask = LMask::from_fn(5, |i| i >= 3);

q.start();
let x = x.fill_with(|i| i as i32);
let y = x.masked_apply(&mask, |_, v| v * 100);
q.end();

let data = y.materialize().unwrap();
// [0, 1, 2, 3, 4] with mask [F, F, F, T, T]
// result: [0, 1, 2, 300, 400]
Source

pub fn masked_fill(&self, mask: &LMask, value: T) -> Self
where T: 'static,

Fills with a constant value only where the mask is true.

Elements where the mask is false retain their original values.

This is a recorded operation that must be called between LQueue::start and LQueue::end.

§Arguments
  • mask - The mask determining where to fill
  • value - The value to fill with
§Panics

Panics if called outside of a start()/end() block.

§Examples
use leopard::{LQueue, LVec, LMask};

let q = LQueue::new();
let x: LVec<i32> = q.lvec_with_capacity(5);
let mask = LMask::from_fn(5, |i| i >= 3);

q.start();
let x = x.fill_with(|i| i as i32);
let y = x.masked_fill(&mask, 999);
q.end();

let data = y.materialize().unwrap();
// [0, 1, 2, 3, 4] with mask [F, F, F, T, T]
// result: [0, 1, 2, 999, 999]

Trait Implementations§

Source§

impl<T> Add for &LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn add(self, other: Self) -> Self::Output

Adds two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the + operator.
Source§

impl<T> Add for LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn add(self, other: Self) -> Self::Output

Adds two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the + operator.
Source§

impl<T: Clone + Clone + Send + Sync + 'static> Clone for LVec<T>

Source§

fn clone(&self) -> LVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Clone + Send + Sync + Debug + 'static> Debug for LVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + Send + Sync + Default + 'static> Default for LVec<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Div for &LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn div(self, other: Self) -> Self::Output

Divides two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the / operator.
Source§

impl<T> Div for LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn div(self, other: Self) -> Self::Output

Divides two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the / operator.
Source§

impl<T: Clone + Send + Sync + 'static> Index<usize> for LVec<T>

Source§

fn index(&self, index: usize) -> &Self::Output

Accesses the element at the given index.

§Warning

For pending vectors, this accesses the original (uncomputed) data, not the result of the pending operation. Use materialize after LQueue::end to get the computed data.

§Panics

Panics if index >= self.len().

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> Mul for &LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn mul(self, other: Self) -> Self::Output

Multiplies two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the * operator.
Source§

impl<T> Mul for LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn mul(self, other: Self) -> Self::Output

Multiplies two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the * operator.
Source§

impl<T> Sub for &LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn sub(self, other: Self) -> Self::Output

Subtracts two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the - operator.
Source§

impl<T> Sub for LVec<T>
where T: Clone + Send + Sync + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + 'static,

Source§

fn sub(self, other: Self) -> Self::Output

Subtracts two vectors element-wise (recorded operation).

Source§

type Output = LVec<T>

The resulting type after applying the - operator.

Auto Trait Implementations§

§

impl<T> Freeze for LVec<T>

§

impl<T> !RefUnwindSafe for LVec<T>

§

impl<T> !Send for LVec<T>

§

impl<T> !Sync for LVec<T>

§

impl<T> Unpin for LVec<T>

§

impl<T> !UnwindSafe for LVec<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.