Skip to main content

Block

Struct Block 

Source
pub struct Block { /* private fields */ }
Expand description

A dictionary from string keys to ndarray arrays with a consistent axis-0 length.

This Block supports heterogeneous column types (float, int, bool).

Implementations§

Source§

impl Block

Source

pub fn new() -> Self

Creates an empty Block.

Source

pub fn with_capacity(cap: usize) -> Self

Creates an empty Block with the specified capacity.

Source

pub fn len(&self) -> usize

Number of keys (columns).

Source

pub fn is_empty(&self) -> bool

Returns true if there are no arrays in the block.

Source

pub fn nrows(&self) -> Option<usize>

Returns the common axis-0 length of all arrays, or None if empty.

Source

pub fn contains_key(&self, key: &str) -> bool

Returns true if the Block contains the specified key.

Source

pub fn insert<T: BlockDtype>( &mut self, key: impl Into<String>, arr: ArrayD<T>, ) -> Result<(), BlockError>

Inserts an array under key, enforcing consistent axis-0 length.

This method uses generic dispatch via the BlockDtype trait to accept any supported type (float, int, bool) without requiring users to manually construct Column enums.

§Errors
  • Returns BlockError::RankZero if the array has rank 0
  • Returns BlockError::RaggedAxis0 if the array’s axis-0 length doesn’t match the Block’s existing nrows
§Examples
use molrs::block::Block;
use molrs::types::{F, I};
use ndarray::Array1;

let mut block = Block::new();

// Insert float array
let arr_float = Array1::from_vec(vec![1.0 as F, 2.0 as F]).into_dyn();
block.insert("x", arr_float).unwrap();

// Insert int array with same nrows
let arr_int = Array1::from_vec(vec![10 as I, 20 as I]).into_dyn();
block.insert("id", arr_int).unwrap();

// This would error - different nrows
let arr_bad = Array1::from_vec(vec![1.0 as F, 2.0 as F, 3.0 as F]).into_dyn();
assert!(block.insert("bad", arr_bad).is_err());
Source

pub fn get(&self, key: &str) -> Option<&Column>

Gets an immutable reference to the column for key if present.

For type-safe access, prefer using get_float(), get_int(), etc.

Source

pub fn get_mut(&mut self, key: &str) -> Option<&mut Column>

Gets a mutable reference to the column for key if present.

For type-safe access, prefer using get_float_mut(), get_int_mut(), etc.

§Warning

Mutating the column’s shape through this reference is allowed but NOT revalidated. It’s the caller’s responsibility to maintain axis-0 consistency.

Source

pub fn get_float(&self, key: &str) -> Option<&ArrayD<F>>

Gets an immutable reference to a float array for key if present and of correct type.

Source

pub fn get_float_mut(&mut self, key: &str) -> Option<&mut ArrayD<F>>

Gets a mutable reference to a float array for key if present and of correct type.

Source

pub fn get_int(&self, key: &str) -> Option<&ArrayD<I>>

Gets an immutable reference to an int array for key if present and of correct type.

Source

pub fn get_int_mut(&mut self, key: &str) -> Option<&mut ArrayD<I>>

Gets a mutable reference to an int array for key if present and of correct type.

Source

pub fn get_bool(&self, key: &str) -> Option<&ArrayD<bool>>

Gets an immutable reference to a bool array for key if present and of correct type.

Source

pub fn get_bool_mut(&mut self, key: &str) -> Option<&mut ArrayD<bool>>

Gets a mutable reference to a bool array for key if present and of correct type.

Source

pub fn get_uint(&self, key: &str) -> Option<&ArrayD<U>>

Gets an immutable reference to a uint array for key if present and of correct type.

Source

pub fn get_uint_mut(&mut self, key: &str) -> Option<&mut ArrayD<U>>

Gets a mutable reference to a uint array for key if present and of correct type.

Source

pub fn get_u8(&self, key: &str) -> Option<&ArrayD<u8>>

Gets an immutable reference to a u8 array for key if present and of correct type.

Source

pub fn get_u8_mut(&mut self, key: &str) -> Option<&mut ArrayD<u8>>

Gets a mutable reference to a u8 array for key if present and of correct type.

Source

pub fn get_string(&self, key: &str) -> Option<&ArrayD<String>>

Gets an immutable reference to a String array for key if present and of correct type.

Source

pub fn get_string_mut(&mut self, key: &str) -> Option<&mut ArrayD<String>>

Gets a mutable reference to a String array for key if present and of correct type.

Source

pub fn remove(&mut self, key: &str) -> Option<Column>

Removes and returns the column for key, if present.

If the Block becomes empty after removal, resets nrows to None.

Source

pub fn rename_column(&mut self, old_key: &str, new_key: &str) -> bool

Renames a column from old_key to new_key.

Returns true if the column was successfully renamed, false if old_key doesn’t exist or new_key already exists.

§Examples
use molrs::block::Block;
use molrs::types::F;
use ndarray::Array1;

let mut block = Block::new();
block.insert("x", Array1::from_vec(vec![1.0 as F]).into_dyn()).unwrap();

assert!(block.rename_column("x", "position_x"));
assert!(!block.contains_key("x"));
assert!(block.contains_key("position_x"));
Source

pub fn clear(&mut self)

Clears the Block, removing all keys and resetting nrows.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Column)>

Returns an iterator over (&str, &Column).

Source

pub fn keys(&self) -> impl Iterator<Item = &str>

Returns an iterator over keys.

Source

pub fn values(&self) -> impl Iterator<Item = &Column>

Returns an iterator over column references.

Source

pub fn dtype(&self, key: &str) -> Option<DType>

Returns the data type of the column with the given key, if it exists.

Source

pub fn resize(&mut self, new_nrows: usize) -> Result<(), MolRsError>

Resize all columns along axis 0 to new_nrows.

  • Shrink (new_nrows < current): slices each column to keep the first new_nrows rows.
  • Grow (new_nrows > current): extends each column with default values (0.0 for Float, 0 for Int/UInt/U8, false for Bool, empty string for String).
  • Same size: no-op, returns Ok(()).
  • Empty block (no columns): sets nrows without touching columns.

Multi-dimensional columns (e.g. Nx3 positions) are resized only along axis 0; trailing dimensions are preserved.

§Arguments
  • new_nrows - The desired number of rows after resize.
§Returns
  • Ok(()) on success.
§Examples
use molrs::block::Block;
use molrs::types::F;
use ndarray::Array1;

let mut block = Block::new();
block.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F]).into_dyn()).unwrap();

block.resize(4).unwrap();
assert_eq!(block.nrows(), Some(4));
let x = block.get_float("x").unwrap();
assert_eq!(x.as_slice_memory_order().unwrap(), &[1.0, 2.0, 0.0, 0.0]);
Source

pub fn merge(&mut self, other: &Block) -> Result<(), BlockError>

Merge another block into this one by concatenating columns along axis-0.

Both blocks must have the same set of column keys and matching dtypes. The resulting block will have nrows = self.nrows + other.nrows.

§Arguments
  • other - The block to merge into this one
§Returns
  • Ok(()) if merge succeeds
  • Err(BlockError) if blocks have incompatible columns
§Examples
use molrs::block::Block;
use molrs::types::F;
use ndarray::Array1;

let mut block1 = Block::new();
block1.insert("x", Array1::from_vec(vec![1.0 as F, 2.0 as F]).into_dyn()).unwrap();

let mut block2 = Block::new();
block2.insert("x", Array1::from_vec(vec![3.0 as F, 4.0 as F]).into_dyn()).unwrap();

block1.merge(&block2).unwrap();
assert_eq!(block1.nrows(), Some(4));

Trait Implementations§

Source§

impl Clone for Block

Source§

fn clone(&self) -> Block

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 Debug for Block

Source§

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

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

impl Default for Block

Source§

fn default() -> Block

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

impl Index<&str> for Block

Source§

type Output = Column

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<&str> for Block

Source§

fn index_mut(&mut self, key: &str) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl Freeze for Block

§

impl RefUnwindSafe for Block

§

impl Send for Block

§

impl Sync for Block

§

impl Unpin for Block

§

impl UnsafeUnpin for Block

§

impl UnwindSafe for Block

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V