1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use super::*;
use std::fmt::{Display, Formatter};

/// [UndirectedEdge](https://reference.wolfram.com/language/ref/UndirectedEdge.html)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UndirectedEdge {
    pub from: usize,
    pub goto: usize,
}

impl Display for UndirectedEdge {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ↔ {}", self.from + 1, self.goto + 1)
    }
}

impl Edge for UndirectedEdge {
    type Weight = ();

    fn from(&self) -> usize {
        self.min_index()
    }

    fn goto(&self) -> usize {
        self.max_index()
    }
}

impl From<(usize, usize)> for UndirectedEdge {
    fn from(ordinal: (usize, usize)) -> Self {
        Self { from: ordinal.0 - 1, goto: ordinal.1 - 1 }
    }
}

impl UndirectedEdge {
    pub fn max_index(&self) -> usize {
        max(self.from, self.goto)
    }
    pub fn min_index(&self) -> usize {
        min(self.from, self.goto)
    }
    pub fn as_range(&self) -> Range<usize> {
        self.min_index()..self.max_index()
    }
}