Skip to main content

use_complex/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_cell::Cell;
5
6/// A general geometric complex count summary.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct GeometricComplex {
9    dimension: usize,
10    cell_count: usize,
11}
12
13impl GeometricComplex {
14    /// Creates a geometric complex descriptor.
15    #[must_use]
16    pub const fn new(dimension: usize, cell_count: usize) -> Option<Self> {
17        if cell_count > 0 {
18            Some(Self {
19                dimension,
20                cell_count,
21            })
22        } else {
23            None
24        }
25    }
26
27    /// Returns the maximum dimension.
28    #[must_use]
29    pub const fn dimension(self) -> usize {
30        self.dimension
31    }
32
33    /// Returns the cell count.
34    #[must_use]
35    pub const fn cell_count(self) -> usize {
36        self.cell_count
37    }
38}
39
40/// A collection of geometric cells.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct CellComplex {
43    cells: Vec<Cell>,
44}
45
46impl CellComplex {
47    /// Creates a cell complex.
48    #[must_use]
49    pub const fn new(cells: Vec<Cell>) -> Self {
50        Self { cells }
51    }
52
53    /// Returns the cells.
54    #[must_use]
55    pub fn cells(&self) -> &[Cell] {
56        &self.cells
57    }
58
59    /// Returns the number of cells.
60    #[must_use]
61    pub fn cell_count(&self) -> usize {
62        self.cells.len()
63    }
64}
65
66/// A simplicial complex represented by simplex dimensions.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct SimplicialComplex {
69    simplex_dimensions: Vec<usize>,
70}
71
72impl SimplicialComplex {
73    /// Creates a simplicial complex from simplex dimensions.
74    #[must_use]
75    pub fn new(simplex_dimensions: Vec<usize>) -> Option<Self> {
76        if simplex_dimensions.iter().all(|dimension| *dimension > 0) {
77            Some(Self { simplex_dimensions })
78        } else {
79            None
80        }
81    }
82
83    /// Returns simplex dimensions.
84    #[must_use]
85    pub fn simplex_dimensions(&self) -> &[usize] {
86        &self.simplex_dimensions
87    }
88
89    /// Returns the number of simplexes.
90    #[must_use]
91    pub fn simplex_count(&self) -> usize {
92        self.simplex_dimensions.len()
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::{CellComplex, GeometricComplex, SimplicialComplex};
99    use use_cell::Cell;
100
101    #[test]
102    fn stores_geometric_complex_metadata() {
103        let cell = Cell::new(2, 3).expect("valid cell");
104        let cell_complex = CellComplex::new(vec![cell]);
105        let geometric = GeometricComplex::new(2, 1).expect("valid complex");
106        let simplicial = SimplicialComplex::new(vec![1, 2]).expect("valid simplicial complex");
107
108        assert_eq!(cell_complex.cell_count(), 1);
109        assert_eq!(cell_complex.cells(), &[cell]);
110        assert_eq!(geometric.dimension(), 2);
111        assert_eq!(simplicial.simplex_count(), 2);
112        assert_eq!(SimplicialComplex::new(vec![0]), None);
113    }
114}