use std::{cmp::Ordering, fmt::Display};
#[must_use = "A weighted undirected edge means nothing on its own"]
#[derive(Debug, Clone, Copy)]
pub struct WeightedEdge(usize, usize, f64);
impl PartialEq for WeightedEdge {
fn eq(&self, other: &Self) -> bool {
self.2.total_cmp(&other.2).is_eq()
}
}
impl Eq for WeightedEdge {}
impl PartialOrd for WeightedEdge {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for WeightedEdge {
fn cmp(&self, other: &Self) -> Ordering {
self.2.total_cmp(&other.2)
}
}
impl Display for WeightedEdge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}-{:?} {:.2?}", self.0, self.1, self.2)
}
}
impl WeightedEdge {
pub fn new(v: usize, w: usize, weight: f64) -> Self {
Self(v, w, weight)
}
pub fn either(&self) -> usize {
self.0
}
pub fn other(&self, vertex: usize) -> usize {
match vertex {
v if v == self.0 => self.1,
w if w == self.1 => self.0,
_ => panic!("Unknown edge"),
}
}
pub fn weight(&self) -> f64 {
self.2
}
}
#[must_use = "A weighted directed edge means nothing on its own"]
#[derive(Debug, Clone, Copy)]
pub struct WeightedDirectedEdge(usize, usize, f64);
impl PartialEq for WeightedDirectedEdge {
fn eq(&self, other: &Self) -> bool {
self.2.total_cmp(&other.2).is_eq()
}
}
impl Eq for WeightedDirectedEdge {}
impl PartialOrd for WeightedDirectedEdge {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for WeightedDirectedEdge {
fn cmp(&self, other: &Self) -> Ordering {
self.2.total_cmp(&other.2)
}
}
impl Display for WeightedDirectedEdge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}->{:?} {:.2?}", self.0, self.1, self.2)
}
}
impl WeightedDirectedEdge {
pub fn new(v: usize, w: usize, weight: f64) -> Self {
Self(v, w, weight)
}
pub fn from(&self) -> usize {
self.0
}
pub fn to(&self) -> usize {
self.1
}
pub fn weight(&self) -> f64 {
self.2
}
}