use std::cmp::Ordering;
#[derive(Clone, Debug)]
pub struct Simplex {
pub(crate) vertices: Vec<usize>,
pub(crate) value: f64,
}
impl Simplex {
pub fn new(vertices: Vec<usize>, value: f64) -> crate::Result<Self> {
if vertices.is_empty() || vertices.windows(2).any(|p| p[0] >= p[1]) {
return Err(crate::Error::InvalidComplex {
cell: None,
reason: "vertices must be nonempty and strictly increasing",
});
}
if !value.is_finite() {
return Err(crate::Error::NonFiniteValue {
field: "filtration value",
index: None,
});
}
Ok(Self {
vertices,
value: crate::canonical_zero(value),
})
}
pub fn vertices(&self) -> &[usize] {
&self.vertices
}
pub fn dimension(&self) -> usize {
self.vertices.len() - 1
}
pub fn value(&self) -> f64 {
self.value
}
}
impl PartialEq for Simplex {
fn eq(&self, other: &Self) -> bool {
self.value == other.value && self.vertices == other.vertices
}
}
impl Eq for Simplex {}
impl PartialOrd for Simplex {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Simplex {
fn cmp(&self, other: &Self) -> Ordering {
compare_filtration(
self.value,
other.value,
self.dimension().cmp(&other.dimension()),
other.vertices.iter().rev().cmp(self.vertices.iter().rev()),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SimplexId(pub(crate) usize);
impl SimplexId {
pub fn index(self) -> usize {
self.0
}
}
pub(crate) fn compare_filtration(
left: f64,
right: f64,
dimensions: Ordering,
decreasing_colex: Ordering,
) -> Ordering {
left.total_cmp(&right)
.then(dimensions)
.then(decreasing_colex)
}