Skip to main content

MolBuilder

Struct MolBuilder 

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

可变的单分子构造器。

自带邻接索引;见模块文档

Implementations§

Source§

impl MolBuilder

Source

pub fn new() -> Self

空分子。

Source

pub fn with_capacity(n_atoms: usize, n_bonds: usize) -> Self

预分配容量的空分子。解析器已知大致规模时用这个避免反复扩容。

Source

pub fn add_atom(&mut self, atomic_num: u8) -> u32

追加一个只指定元素的原子,返回其局部下标。

Source

pub fn add_atom_data(&mut self, atom: AtomData) -> u32

追加一个完整指定的原子,返回其局部下标。

Source

pub fn add_bond( &mut self, begin: u32, end: u32, order: BondOrder, ) -> Result<u32>

追加一条键,返回其局部下标。

§Errors

端点越界、或两端相同(自环)时返回错误。分子图中不存在自环, 让它静默通过会在 CSR 构建时产生难以追查的度数异常。

Source

pub fn add_bond_data(&mut self, bond: BondData) -> Result<u32>

追加一条完整指定的键,返回其局部下标。

同时把两条半边挂进端点的邻接链表,O(1)。

§Errors

add_bond

§Panics

键数超过 u32::MAX / 2 时 —— 半边编号会与哨兵冲突。这需要 32 GB 仅存键表,实际不可达,但宁可响亮地停下也不要静默算错。

Source

pub fn swap_bond_ends(&mut self, bond: u32) -> Result<()>

交换一条键的两个端点。

端点顺序对配位键有语义(begin 是给电子的一端),这个接口就是为它 准备的。其余键型的端点顺序只是书写痕迹,交换它没有意义。

§为什么是整体重建索引

半边链表是单向的:把两条半边在两个原子的链表之间对调,要各自找到前驱 再改指针,还要顾及链首链尾 —— 几十行且容易出错。而整体重建只有几行, 显然正确,并且保持“邻居顺序 = 键的插入顺序“这条不变量(重建按键号 递增走,每个原子的相对顺序不变)。

代价是 O(原子数 + 键数)。这个接口的调用场景是净化第 2 步, 实测 8839 条语料里只触发 2 次 —— 为它做精细的指针手术不划算。

§Errors

键下标越界时返回 Error::BondIndexOutOfRange

Source

pub fn neighbors(&self, atom: u32) -> Neighbors<'_>

遍历某原子的邻居,产出 (邻居原子下标, 键下标)。O(度数)。

顺序即键的插入顺序,与 MolView::neighbors 一致 —— 手性语义依赖于这个顺序,两处必须给出同样的序列。

原子下标越界时产出空序列。

Source

pub fn degree(&self, atom: u32) -> usize

原子的度(不含隐式氢)。O(1);越界返回 0。

Source

pub fn bond_between(&self, a: u32, b: u32) -> Option<u32>

连接 ab 的键下标。O(min 度数);不相邻时返回 None

Source

pub fn num_atoms(&self) -> usize

原子数

Source

pub fn num_bonds(&self) -> usize

键数

Source

pub fn atoms(&self) -> &[AtomData]

只读访问全部原子

Source

pub fn bonds(&self) -> &[BondData]

只读访问全部键

Source

pub fn atom_mut(&mut self, idx: u32) -> Option<&mut AtomData>

可变访问单个原子

Source

pub fn bond_mut(&mut self, idx: u32) -> Option<BondMut<'_>>

可变访问单条键的属性

返回的 BondMut 刻意不暴露端点 —— 端点即拓扑,而邻接索引是随建边 增量维护的。若允许就地改端点,索引会在无人察觉的情况下失效,而这类 bug 只会在很久以后以“某个原子少了个邻居“的形式冒出来。

Source

pub fn name(&self) -> Option<&str>

分子名(SDF 的标题行、SMILES 后跟的名字)

Source

pub fn set_name(&mut self, name: impl Into<String>)

设置分子名

Trait Implementations§

Source§

impl Clone for MolBuilder

Source§

fn clone(&self) -> MolBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for MolBuilder

Source§

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

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

impl Default for MolBuilder

Source§

fn default() -> MolBuilder

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

Auto Trait Implementations§

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> 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 = !

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.