1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Python wrapper for [`crate::containers::mesh::Coords`].
use crate::containers::mesh::{Coords, NodeId};
use crate::py::node::PyNode;
use crate::store::{insert, read, write, Handle};
use pyo3::prelude::*;
/// The registry of live nodes and their coordinates, in a fixed spatial
/// dimension.
///
/// Create one with `Coords(dim)`, then add nodes with
/// `add_node([x, y, ...])`. Every mesh, node and field is attached to a
/// `Coords`.
#[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 {
/// `Coords(dim)` — an empty `Coords` in `dim` dimensions.
#[new]
fn py_new(dim: u8) -> PyResult<Self> {
let coords = Coords::new(dim)?;
Ok(Self {
handle: insert(coords),
})
}
/// Spatial dimension of the coordinates (1, 2 or 3).
#[getter]
fn dim(&self) -> PyResult<u8> {
Ok(read(&self.handle)?.dim())
}
/// Number of live nodes.
fn node_count(&self) -> PyResult<usize> {
Ok(read(&self.handle)?.node_count())
}
/// Number of allocated node slots (live plus not-yet-collected).
fn capacity(&self) -> PyResult<usize> {
Ok(read(&self.handle)?.capacity())
}
/// Whether node `id` is still live (not garbage-collected).
///
/// Takes a **raw id**, not a `Node`, on purpose: a `Node` holds a
/// refcount, so it could never be observed dead. This is the API for
/// inspecting nodes you may no longer hold (post-GC checks).
fn is_alive(&self, id: u32) -> PyResult<bool> {
Ok(read(&self.handle)?.is_alive(NodeId(id)))
}
/// Add a node at `coords` and return it as a `Node` (refcount = 1).
fn add_node(&self, coords: Vec<f64>) -> PyResult<PyNode> {
let id = write(&self.handle)?.add_node(&coords)?;
Ok(PyNode::from_raw(self.handle.clone(), id))
}
/// Return an additional `Node` for an existing id (refcount += 1).
fn acquire(&self, id: u32) -> PyResult<PyNode> {
write(&self.handle)?.incref(NodeId(id))?;
Ok(PyNode::from_raw(self.handle.clone(), NodeId(id)))
}
/// Reference count of node `id` (how many holders keep it alive).
///
/// Takes a **raw id** (see [`Self::is_alive`]): observing a refcount of
/// 0 is impossible while holding the `Node` that would carry it.
fn refcount(&self, id: u32) -> PyResult<u32> {
Ok(read(&self.handle)?.refcount(NodeId(id)))
}
/// Run the garbage collector; return the number of collected nodes.
fn gc(&self) -> PyResult<usize> {
Ok(write(&self.handle)?.gc())
}
// Per-node coordinate access lives on `Node` (`node.coord()` /
// `node.set_coord(...)`): a Node carries its Coords, so the
// (config, id) pair is never needed here.
/// Add a named alternative configuration (same nodes, new coordinates);
/// returns its index.
fn add_config(&self, name: String) -> PyResult<usize> {
Ok(write(&self.handle)?.add_config(name))
}
/// Make configuration `config` the active one.
fn select(&self, config: usize) -> PyResult<()> {
write(&self.handle)?.select(config)?;
Ok(())
}
/// Index of the active configuration.
#[getter]
fn active(&self) -> PyResult<usize> {
Ok(read(&self.handle)?.active())
}
/// Names of the configurations, by index.
fn names(&self) -> PyResult<Vec<String>> {
Ok(read(&self.handle)?.names().to_vec())
}
/// Current node permutation (a renumbering), or `None` if unset.
fn permutation(&self) -> PyResult<Option<Vec<u32>>> {
Ok(read(&self.handle)?.permutation().map(|s| s.to_vec()))
}
/// Set a node permutation (a renumbering of the nodes).
fn set_permutation(&self, perm: Vec<u32>) -> PyResult<()> {
write(&self.handle)?.set_permutation(perm)?;
Ok(())
}
/// Drop any node permutation.
fn clear_permutation(&self) -> PyResult<()> {
write(&self.handle)?.clear_permutation();
Ok(())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", &*read(&self.handle)?))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", &*read(&self.handle)?))
}
}
crate::impl_dump_pymethod!(handle PyCoords, handle);