ZeroCopyOps

Trait ZeroCopyOps 

Source
pub trait ZeroCopyOps<A: Clone + Copy + 'static + Send + Sync> {
    // Required methods
    fn map_zero_copy<F>(&self, f: F) -> CoreResult<MemoryMappedArray<A>>
       where F: Fn(A) -> A + Send + Sync;
    fn reduce_zero_copy<F>(&self, init: A, f: F) -> CoreResult<A>
       where F: Fn(A, A) -> A + Send + Sync;
    fn combine_zero_copy<F>(
        &self,
        other: &Self,
        f: F,
    ) -> CoreResult<MemoryMappedArray<A>>
       where F: Fn(A, A) -> A + Send + Sync;
    fn filter_zero_copy<F>(&self, predicate: F) -> CoreResult<Vec<A>>
       where F: Fn(&A) -> bool + Send + Sync;
    fn max_zero_copy(&self) -> CoreResult<A>
       where A: PartialOrd;
    fn min_zero_copy(&self) -> CoreResult<A>
       where A: PartialOrd;
    fn sum_zero_copy(&self) -> CoreResult<A>
       where A: Add<Output = A> + From<u8>;
    fn product_zero_copy(&self) -> CoreResult<A>
       where A: Mul<Output = A> + From<u8>;
    fn mean_zero_copy(&self) -> CoreResult<A>
       where A: Add<Output = A> + Div<Output = A> + From<u8> + From<usize>;
}
Expand description

Trait for zero-copy operations on memory-mapped arrays.

This trait provides methods for performing operations on memory-mapped arrays without unnecessary memory allocations or copies. The operations are designed to work efficiently with large datasets by processing data in chunks and maintaining memory-mapping where possible.

Required Methods§

Source

fn map_zero_copy<F>(&self, f: F) -> CoreResult<MemoryMappedArray<A>>
where F: Fn(A) -> A + Send + Sync,

Maps a function over each element of the array without loading the entire array.

This is similar to the map function in functional programming, but implemented to work efficiently with memory-mapped arrays by processing chunks.

§Arguments
  • f - A function that takes an element of type A and returns a new element of the same type
§Returns

A new memory-mapped array containing the mapped values

§Example
// Double each element
let doubled = mmap.map_zero_copy(|x| x * 2.0);
Source

fn reduce_zero_copy<F>(&self, init: A, f: F) -> CoreResult<A>
where F: Fn(A, A) -> A + Send + Sync,

Reduces the array to a single value by applying a binary operation.

This is similar to the fold or reduce function in functional programming, but implemented to work efficiently with memory-mapped arrays by processing chunks.

§Arguments
  • init - The initial value for the reduction
  • f - A function that takes two values of type A and combines them into one
§Returns

The reduced value

§Example
// Sum all elements
let sum = mmap.reduce_zero_copy(0.0, |acc, x| acc + x);
Source

fn combine_zero_copy<F>( &self, other: &Self, f: F, ) -> CoreResult<MemoryMappedArray<A>>
where F: Fn(A, A) -> A + Send + Sync,

Performs a binary operation between two memory-mapped arrays element-wise.

This allows for operations like addition, subtraction, etc. between two arrays without loading both arrays entirely into memory.

§Arguments
  • other - Another memory-mapped array with the same shape
  • f - A function that takes two elements (one from each array) and returns a new element
§Returns

A new memory-mapped array containing the result of the binary operation

§Example
// Add two arrays element-wise
let sum_array = mmap1.combine_zero_copy(&mmap2, |a, b| a + b);
Source

fn filter_zero_copy<F>(&self, predicate: F) -> CoreResult<Vec<A>>
where F: Fn(&A) -> bool + Send + Sync,

Filters elements based on a predicate function.

Returns a new array containing only the elements that satisfy the predicate.

§Arguments
  • predicate - A function that takes an element and returns a boolean
§Returns

A new array containing only the elements that satisfy the predicate

§Example
// Get only positive elements
let positives = mmap.filter_zero_copy(|&x| x > 0.0);
Source

fn max_zero_copy(&self) -> CoreResult<A>
where A: PartialOrd,

Returns the maximum element in the array.

§Returns

The maximum element, or an error if the array is empty

§Example
let max_value = mmap.max_zero_copy();
Source

fn min_zero_copy(&self) -> CoreResult<A>
where A: PartialOrd,

Returns the minimum element in the array.

§Returns

The minimum element, or an error if the array is empty

§Example
let min_value = mmap.min_zero_copy();
Source

fn sum_zero_copy(&self) -> CoreResult<A>
where A: Add<Output = A> + From<u8>,

Calculates the sum of all elements in the array.

§Returns

The sum of all elements

§Example
let total = mmap.sum_zero_copy();
Source

fn product_zero_copy(&self) -> CoreResult<A>
where A: Mul<Output = A> + From<u8>,

Calculates the product of all elements in the array.

§Returns

The product of all elements

§Example
let product = mmap.product_zero_copy();
Source

fn mean_zero_copy(&self) -> CoreResult<A>
where A: Add<Output = A> + Div<Output = A> + From<u8> + From<usize>,

Calculates the mean of all elements in the array.

§Returns

The mean of all elements

§Example
let avg = mmap.mean_zero_copy();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A: Clone + Copy + 'static + Send + Sync + Zero> ZeroCopyOps<A> for MemoryMappedArray<A>