use std::hash::Hash;
use std::ops::{Add, Sub};
pub mod adjacency_list;
pub trait Vertex<K, V>
where K: Hash + Eq + Clone
{
fn get_value(&self) -> &V;
fn get_mut_value(&mut self) -> &mut V;
fn key(&self) -> K;
}
pub trait Edge<K, W, C>
where K: Hash + Eq + Clone,
C: Hash + Eq + Clone,
W: Add + Sub + Eq + Ord + Copy
{
fn get_weight(&self) -> W;
fn set_weight(&mut self, weight: W);
fn left(&self) -> &K;
fn right(&self) -> &K;
fn get_pair(&self) -> (&K, &K);
fn generate_key(pair: (&K, &K)) -> C;
fn key(&self) -> C;
}
pub trait DirectedGraph<T, E, K, V, W, C>
where K: Hash + Eq + Clone,
C: Hash + Eq + Clone,
W: Add + Sub + Eq + Ord + Copy,
T: Vertex<K, V>,
E: Edge<K, W, C>
{
fn adjacent(&self, from: &K, to: &K) -> bool;
fn neighbors(&self, from: &K) -> Vec<&K>;
fn leading_to(&self, to: &K) -> Vec<&K>;
fn get_all_keys(&self) -> Vec<&K>;
fn get_all_pairs(&self) -> Vec<(&K, &K)>;
fn get_vertex(&self, key: &K) -> Option<&T>;
fn get_mut_vertex(&mut self, key: &K) -> Option<&mut T>;
fn get_edge(&self, pair: (&K, &K)) -> Option<&E>;
fn get_mut_edge(&mut self, pair: (&K, &K)) -> Option<&mut E>;
}
pub trait VariableEdges<T, E, K, V, W, C>: DirectedGraph<T, E, K, V, W, C>
where K: Hash + Eq + Clone,
C: Hash + Eq + Clone,
W: Add + Sub + Eq + Ord + Copy,
T: Vertex<K, V>,
E: Edge<K, W, C>
{
fn add_edge(&mut self, edge: E) -> Result<Option<E>, EdgeSide>;
fn remove_edge(&mut self, pair: (&K, &K)) -> Option<E>;
}
pub trait VariableVertexes<T, E, K, V, W, C>: DirectedGraph<T, E, K, V, W, C>
where K: Hash + Eq + Clone,
C: Hash + Eq + Clone,
W: Add + Sub + Eq + Ord + Copy,
T: Vertex<K, V>,
E: Edge<K, W, C>
{
fn add_vertex(&mut self, vertex: T) -> Option<T>;
fn remove_vertex(&mut self, key: K) -> Option<T>;
}
pub trait Graph<T, E, K, V, W, C>: DirectedGraph<T, E, K, V, W, C>
where K: Hash + Eq + Clone,
C: Hash + Eq + Clone,
W: Add + Sub + Eq + Ord + Copy,
T: Vertex<K, V>,
E: Edge<K, W, C>
{}
#[derive(Debug, Eq, PartialEq)]
pub struct SimpleVertex<K: Hash + Eq + Clone, V> {
key: K,
value: V,
}
impl<K: Hash + Eq + Clone, V> SimpleVertex<K, V> {
pub fn new(key: K, value: V) -> SimpleVertex<K, V> {
SimpleVertex {
key,
value,
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum EdgeSide {
Left,
Right,
Both
}
impl<K: Hash + Eq + Clone, V> Vertex<K, V> for SimpleVertex<K, V> {
fn get_value(&self) -> &V {
&(self.value)
}
fn get_mut_value(&mut self) -> &mut V {
&mut (self.value)
}
fn key(&self) -> K {
self.key.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_vertex_construction() {
let vertex = SimpleVertex::new(1,0);
assert_eq!(vertex, SimpleVertex {key: 1, value: 0});
}
#[test]
fn simple_vertex_getters() {
let mut vertex = SimpleVertex::new(1,0);
assert_eq!(vertex.get_value(), &0);
assert_eq!(vertex.key(), 1);
*vertex.get_mut_value() += 3;
assert_eq!(vertex.get_value(), &3);
}
}