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
impl Block
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Creates an empty Block with the specified capacity.
Sourcepub fn nrows(&self) -> Option<usize>
pub fn nrows(&self) -> Option<usize>
Returns the common axis-0 length of all arrays, or None if empty.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the Block contains the specified key.
Sourcepub fn insert<T: BlockDtype>(
&mut self,
key: impl Into<String>,
arr: ArrayD<T>,
) -> Result<(), BlockError>
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::RankZeroif the array has rank 0 - Returns
BlockError::RaggedAxis0if the array’s axis-0 length doesn’t match the Block’s existingnrows
§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());Sourcepub fn get(&self, key: &str) -> Option<&Column>
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.
Sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut Column>
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.
Sourcepub fn get_float(&self, key: &str) -> Option<&ArrayD<F>>
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.
Sourcepub fn get_float_mut(&mut self, key: &str) -> Option<&mut ArrayD<F>>
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.
Sourcepub fn get_int(&self, key: &str) -> Option<&ArrayD<I>>
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.
Sourcepub fn get_int_mut(&mut self, key: &str) -> Option<&mut ArrayD<I>>
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.
Sourcepub fn get_bool(&self, key: &str) -> Option<&ArrayD<bool>>
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.
Sourcepub fn get_bool_mut(&mut self, key: &str) -> Option<&mut ArrayD<bool>>
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.
Sourcepub fn get_uint(&self, key: &str) -> Option<&ArrayD<U>>
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.
Sourcepub fn get_uint_mut(&mut self, key: &str) -> Option<&mut ArrayD<U>>
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.
Sourcepub fn get_u8(&self, key: &str) -> Option<&ArrayD<u8>>
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.
Sourcepub fn get_u8_mut(&mut self, key: &str) -> Option<&mut ArrayD<u8>>
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.
Sourcepub fn get_string(&self, key: &str) -> Option<&ArrayD<String>>
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.
Sourcepub fn get_string_mut(&mut self, key: &str) -> Option<&mut ArrayD<String>>
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.
Sourcepub fn remove(&mut self, key: &str) -> Option<Column>
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.
Sourcepub fn rename_column(&mut self, old_key: &str, new_key: &str) -> bool
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"));Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Column)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &Column)>
Returns an iterator over (&str, &Column).
Sourcepub fn values(&self) -> impl Iterator<Item = &Column>
pub fn values(&self) -> impl Iterator<Item = &Column>
Returns an iterator over column references.
Sourcepub fn dtype(&self, key: &str) -> Option<DType>
pub fn dtype(&self, key: &str) -> Option<DType>
Returns the data type of the column with the given key, if it exists.
Sourcepub fn resize(&mut self, new_nrows: usize) -> Result<(), MolRsError>
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 firstnew_nrowsrows. - 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
nrowswithout 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]);Sourcepub fn merge(&mut self, other: &Block) -> Result<(), BlockError>
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 succeedsErr(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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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