use crate::atoms::NodeId;
use crate::coords::Coords;
use crate::handle::Handle;
use crate::py::node::PyNode;
use pyo3::prelude::*;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "Coords")]
pub struct PyCoords {
pub(crate) handle: Handle<Coords>,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyCoords {
#[new]
fn py_new(dim: u8) -> PyResult<Self> {
let coords = Coords::new(dim)?;
Ok(Self {
handle: Handle::new(coords),
})
}
#[classmethod]
fn axisymmetric(_cls: &pyo3::Bound<'_, pyo3::types::PyType>) -> PyResult<Self> {
Ok(Self {
handle: Handle::new(Coords::axisymmetric()?),
})
}
#[getter]
fn dim(&self) -> u8 {
self.handle.read().dim()
}
#[getter]
fn is_axisymmetric(&self) -> bool {
self.handle.read().is_axisymmetric()
}
#[getter]
fn node_count(&self) -> usize {
self.handle.read().node_count()
}
#[getter]
fn capacity(&self) -> usize {
self.handle.read().capacity()
}
fn is_alive(&self, id: u32) -> bool {
self.handle.read().is_alive(NodeId(id))
}
fn add_node(&self, coords: Vec<f64>) -> PyResult<PyNode> {
let id = self.handle.write().add_node(&coords)?;
Ok(PyNode::from_raw(self.handle.clone(), id))
}
fn acquire(&self, id: u32) -> PyResult<PyNode> {
self.handle.write().incref(NodeId(id))?;
Ok(PyNode::from_raw(self.handle.clone(), NodeId(id)))
}
fn refcount(&self, id: u32) -> u32 {
self.handle.read().refcount(NodeId(id))
}
fn gc(&self) -> usize {
self.handle.write().gc()
}
fn add_config(&self, name: String) -> usize {
self.handle.write().add_config(name)
}
fn select(&self, config: usize) -> PyResult<()> {
self.handle.write().select(config)?;
Ok(())
}
#[getter]
fn active(&self) -> usize {
self.handle.read().active()
}
fn names(&self) -> Vec<String> {
self.handle.read().names().to_vec()
}
fn in_config(&self, config: usize) -> PyInConfig {
PyInConfig {
handle: self.handle.clone(),
config,
previous: Vec::new(),
}
}
fn permutation(&self) -> Option<Vec<u32>> {
self.handle.read().permutation().map(|s| s.to_vec())
}
fn set_permutation(&self, perm: Vec<u32>) -> PyResult<()> {
self.handle.write().set_permutation(perm)?;
Ok(())
}
fn clear_permutation(&self) {
self.handle.write().clear_permutation();
}
}
crate::impl_dump_pymethod!(handle PyCoords, handle);
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "InConfig")]
pub struct PyInConfig {
handle: Handle<Coords>,
config: usize,
previous: Vec<usize>,
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyInConfig {
fn __enter__(&mut self) -> PyResult<()> {
let mut c = self.handle.write();
let previous = c.active();
c.select(self.config)?;
self.previous.push(previous);
Ok(())
}
fn __exit__(
&mut self,
_exc_type: &Bound<'_, PyAny>,
_exc_value: &Bound<'_, PyAny>,
_traceback: &Bound<'_, PyAny>,
) -> PyResult<bool> {
let previous = self.previous.pop().ok_or_else(|| {
pyo3::exceptions::PyRuntimeError::new_err("in_config: left without being entered")
})?;
self.handle.write().select(previous)?;
Ok(false)
}
}