adjacency_matrix/static_half/
display.rs

1use super::*;
2
3#[cfg(feature = "wolfram")]
4use graph_types::{ToWolfram, WolframValue};
5
6impl Display for StaticUndirected {
7    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8        let size = self.max_degree().to_string().len();
9        let max = self.nodes();
10        for i in 0..max {
11            for j in 0..max {
12                if j > i {
13                    write!(f, "{:width$} ", ".", width = size)?;
14                }
15                else {
16                    let index = i * (i + 1) / 2 + j;
17                    let edge = unsafe { self.edges.get_unchecked(index) };
18                    write!(f, "{:width$} ", edge, width = size)?;
19                }
20            }
21            writeln!(f)?;
22        }
23        Ok(())
24    }
25}
26
27#[cfg(feature = "wolfram")]
28impl ToWolfram for StaticUndirected {
29    fn to_wolfram(&self) -> WolframValue {
30        let nodes = self.nodes();
31        let mut rows = Vec::with_capacity(nodes);
32        for i in 0..nodes {
33            let mut row = Vec::with_capacity(nodes);
34            for j in 0..nodes {
35                if j > i {
36                    let index = j * (j + 1) / 2 + i;
37                    let edge = unsafe { self.edges.get_unchecked(index) };
38                    row.push(WolframValue::Integer64(*edge as i64));
39                }
40                else {
41                    let index = i * (i + 1) / 2 + j;
42                    let edge = unsafe { self.edges.get_unchecked(index) };
43                    row.push(WolframValue::Integer64(*edge as i64));
44                }
45            }
46            rows.push(WolframValue::list(row));
47        }
48        WolframValue::function("AdjacencyGraph", vec![WolframValue::list(rows)])
49    }
50}