Skip to main content

use_polytope/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// A small n-dimensional polytope descriptor.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct Polytope {
7    dimension: usize,
8    vertex_count: usize,
9    facet_count: usize,
10}
11
12impl Polytope {
13    /// Creates a polytope descriptor with positive dimension and counts.
14    #[must_use]
15    pub const fn new(dimension: usize, vertex_count: usize, facet_count: usize) -> Option<Self> {
16        if dimension > 0 && vertex_count > 0 && facet_count > 0 {
17            Some(Self {
18                dimension,
19                vertex_count,
20                facet_count,
21            })
22        } else {
23            None
24        }
25    }
26
27    /// Returns the dimension.
28    #[must_use]
29    pub const fn dimension(self) -> usize {
30        self.dimension
31    }
32
33    /// Returns the vertex count.
34    #[must_use]
35    pub const fn vertex_count(self) -> usize {
36        self.vertex_count
37    }
38
39    /// Returns the facet count.
40    #[must_use]
41    pub const fn facet_count(self) -> usize {
42        self.facet_count
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::Polytope;
49
50    #[test]
51    fn stores_polytope_counts() {
52        let polytope = Polytope::new(4, 8, 16).expect("valid polytope");
53
54        assert_eq!(polytope.dimension(), 4);
55        assert_eq!(polytope.vertex_count(), 8);
56        assert_eq!(polytope.facet_count(), 16);
57        assert_eq!(Polytope::new(0, 8, 16), None);
58    }
59}