use crate::error::{GraphError, ErrorKind};
pub mod error;
pub mod algorithms;
pub mod utils;
mod stack;
mod dsu;
pub struct Vertex <T> {
id: usize,
edges: Vec<Edge<T>>
}
impl <T> Vertex<T> {
fn new(id: usize) -> Self {
Vertex{ id, edges: vec![] }
}
pub fn id(&self) -> usize {
self.id
}
}
struct Edge<T> {
to: usize,
#[allow(dead_code)]
weight: T
}
impl <T> Edge<T> {
fn new(to: usize, weight: T) -> Self {
Edge{ to, weight }
}
}
pub struct Graph <T> {
adj: Vec<Vertex<T>>
}
impl <T> Graph<T> {
pub fn new(n: usize) -> Self {
let mut vertices = Vec::with_capacity(n + 1);
for id in 0..=n {
vertices.push(Vertex::new(id));
}
Graph{
adj: vertices
}
}
pub fn add_edge(&mut self, from: usize, to: usize, weight: T) -> Result<(), GraphError> {
let bound = self.adj.len() - 1;
if from > bound || to > bound || from == 0 || to == 0 {
return Err(GraphError::Regular(ErrorKind::UnableCreateEdge(from, to)))
}
self.adj[from].edges.push(Edge::new(to, weight));
Ok(())
}
fn size(&self) -> usize {
self.adj.len()
}
pub fn get_vertex(&self, id: usize) -> Result<&Vertex<T>, GraphError> {
if id > self.size() - 1 || id == 0 {
return Err(GraphError::Regular(ErrorKind::VertexNotFound(id)));
}
Ok(&self.adj[id])
}
}