pub struct Graph<const SIZE: usize> {
data: [[bool; SIZE]; SIZE],
}
impl<const SIZE: usize> Graph<SIZE> {
pub const fn add_edge(&mut self, i: usize, j: usize) {
self.data[i][j] = true;
}
pub const fn add_edge_undirected(
&mut self,
i: usize,
j: usize,
) {
self.data[i][j] = true;
self.data[j][i] = true;
}
pub const fn remove_edge(&mut self, i: usize, j: usize) {
self.data[i][j] = false;
}
pub const fn remove_edge_undirected(
&mut self,
i: usize,
j: usize,
) {
self.data[i][j] = false;
self.data[j][i] = false;
}
pub const fn has_edge(&self, i: usize, j: usize) -> bool {
self.data[i][j]
}
pub const fn get_edges(
&self,
vertex: usize,
) -> &[bool; SIZE] {
&self.data[vertex]
}
pub const fn get_inverse_edges(
&self,
vertex: usize,
) -> [bool; SIZE] {
let mut edges = [false; SIZE];
let mut neighbor = 0;
while neighbor < SIZE {
edges[neighbor] = self.data[neighbor][vertex];
neighbor += 1;
}
edges
}
pub const fn max_number_of_edges(&self) -> usize {
SIZE * (SIZE - 1)
}
pub const fn density(&self) -> f32 {
let mut edges = 0;
let mut i = 0;
while i < SIZE {
let mut j = 0;
while j < SIZE {
if self.data[i][j] {
edges += 1;
}
j += 1;
}
i += 1;
}
edges as f32 / self.max_number_of_edges() as f32
}
pub const fn clear(&mut self) {
let mut i = 0;
while i < SIZE {
let mut j = 0;
while j < SIZE {
self.data[i][j] = false;
j += 1;
}
i += 1;
}
}
pub const fn new() -> Graph<SIZE> {
Graph {
data: [[false; SIZE]; SIZE],
}
}
}