Skip to content

Commit

Permalink
Add equality and order implementations for Cell, Facet, and Vertex
Browse files Browse the repository at this point in the history
- Implement equality for cells based on the sorted vector of vertices
- Implement order for cells based on the lexicographic order of sorted vector of vertices
- Implement equality for vertices based on the elements in the vector of coordinates
- Implement order for vertices based on the lexicographic order of elements in the vector of coordinates
- Test equality for facets, which is based on equality of cells and vertices
- Test order for facets, which is based on order of cells and vertices

Crate updates.
  • Loading branch information
acgetchell committed Jan 22, 2024
1 parent 530cd68 commit 0e16c16
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ num-traits = "0.2.17"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
serde_test = "1.0.176"
uuid = { version = "1.6.1", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }
uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }

[lints.rust]
unsafe_code = "forbid"
Expand Down
9 changes: 3 additions & 6 deletions src/delaunay_core/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ where
}
}

/// Equality of cells is based on equality of sorted vector of vertices.
impl<T, U, V, const D: usize> PartialEq for Cell<T, U, V, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
Expand All @@ -408,6 +409,7 @@ where
}
}

/// Order of cells is based on lexicographic order of sorted vector of vertices.
impl<T, U, V, const D: usize> PartialOrd for Cell<T, U, V, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
Expand Down Expand Up @@ -715,7 +717,7 @@ mod tests {
let cell2: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();
let cell3: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex4, vertex2, vertex3, vertex1]).unwrap();
Cell::new(vec![vertex4, vertex3, vertex2, vertex1]).unwrap();
let cell4: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex5, vertex4, vertex3, vertex2]).unwrap();

Expand All @@ -741,11 +743,6 @@ mod tests {
let cell3: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex5, vertex4, vertex3, vertex2]).unwrap();

assert!(cell1 < cell3);
assert_eq!(cell1, cell2);
// These should fail
// assert!(cell1 < cell2);
// assert!(cell2 < cell1);
assert!(cell1 < cell3);
assert!(cell2 < cell3);
assert!(cell3 > cell1);
Expand Down
38 changes: 37 additions & 1 deletion src/delaunay_core/facet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use super::{cell::Cell, vertex::Vertex};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, PartialOrd)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, PartialOrd, Serialize)]
/// The `Facet` struct represents a facet of a d-dimensional simplex.
/// Passing in a `Vertex` and a `Cell` containing that vertex to the
/// constructor will create a `Facet` struct.
Expand Down Expand Up @@ -179,4 +179,40 @@ mod tests {
// Human readable output for cargo test -- --nocapture
println!("Serialized = {:?}", serialized);
}

#[test]
fn facet_partial_eq() {
let vertex1 = Vertex::new(Point::new([0.0, 0.0, 0.0]));
let vertex2 = Vertex::new(Point::new([1.0, 0.0, 0.0]));
let vertex3 = Vertex::new(Point::new([0.0, 1.0, 0.0]));
let vertex4 = Vertex::new(Point::new([0.0, 0.0, 1.0]));
let cell: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();
let facet1 = Facet::new(cell.clone(), vertex1).unwrap();
let facet2 = Facet::new(cell.clone(), vertex1).unwrap();
let facet3 = Facet::new(cell.clone(), vertex2).unwrap();

assert_eq!(facet1, facet2);
assert_ne!(facet1, facet3);
}

#[test]
fn facet_partial_ord() {
let vertex1 = Vertex::new(Point::new([0.0, 0.0, 0.0]));
let vertex2 = Vertex::new(Point::new([1.0, 0.0, 0.0]));
let vertex3 = Vertex::new(Point::new([0.0, 1.0, 0.0]));
let vertex4 = Vertex::new(Point::new([0.0, 0.0, 1.0]));
let cell: Cell<f64, Option<()>, Option<()>, 3> =
Cell::new(vec![vertex1, vertex2, vertex3, vertex4]).unwrap();
let facet1 = Facet::new(cell.clone(), vertex1).unwrap();
let facet2 = Facet::new(cell.clone(), vertex1).unwrap();
let facet3 = Facet::new(cell.clone(), vertex2).unwrap();
let facet4 = Facet::new(cell.clone(), vertex3).unwrap();

assert!(facet1 < facet3);
assert!(facet2 < facet3);
assert!(facet3 > facet1);
assert!(facet3 > facet2);
assert!(facet3 > facet4);
}
}
10 changes: 8 additions & 2 deletions src/delaunay_core/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ where
}
}

/// Equality of vertices is based on equality of elements in vector of coords.
impl<T, U, const D: usize> PartialEq for Vertex<T, U, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
Expand All @@ -191,6 +192,7 @@ where
}
}

/// Order of vertices is based on lexicographic order of elements in vector of coords.
impl<T, U, const D: usize> PartialOrd for Vertex<T, U, D>
where
T: Clone + Copy + Default + PartialEq + PartialOrd,
Expand Down Expand Up @@ -315,15 +317,19 @@ mod tests {
let vertex3: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 4.0]));

assert_eq!(vertex1, vertex2);
assert_ne!(vertex1, vertex3);
assert_ne!(vertex2, vertex3);
}

#[test]
fn vertex_partial_ord() {
let vertex1: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 3.0]));
let vertex2: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([1.0, 2.0, 4.0]));
let vertex3: Vertex<f64, Option<()>, 3> = Vertex::new(Point::new([10.0, 0.0, 0.0]));
let vertex4 = Vertex::new(Point::new([0.0, 0.0, 10.0]));

assert!(vertex1 < vertex2);
assert!(vertex2 > vertex1);
assert!(vertex3 > vertex2);
assert!(vertex1 < vertex3);
assert!(vertex1 > vertex4);
}
}

0 comments on commit 0e16c16

Please sign in to comment.