Skip to content

Commit

Permalink
Scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
badumbatish committed Jul 1, 2024
1 parent c95ea51 commit ca2dc18
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/adjacency_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
hash::Hash,
};

use crate::{storage::GraphStorage, u_graph::UGraph};
use crate::{storage::GraphStorage};

pub struct AdjacencyList<V, E> {
adjacency_list: HashMap<V, HashSet<(V, E)>>,
Expand All @@ -20,6 +20,12 @@ where
}
}

fn remove_vertex(&mut self, vertex: V) {
self.adjacency_list.remove(&vertex);
for neighbors in self.adjacency_list.values_mut() {
neighbors.retain(|(v, _)| v != &vertex);
}
}
fn add_vertex(&mut self, vertex: V) {
self.adjacency_list
.entry(vertex.clone())
Expand Down
30 changes: 30 additions & 0 deletions src/di_graph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::storage::GraphStorage;

use std::hash::Hash;
pub struct DiGraph<V, E, S>
where
Expand All @@ -9,3 +10,32 @@ where
storage: S,
_phantom: std::marker::PhantomData<(V, E)>,
}

impl<V, E, S> DiGraph<V, E, S>
where
V: Eq + Hash + Clone,
E: Eq + Hash + Clone,
S: GraphStorage<V, E>,
{
pub fn new() -> Self {
DiGraph {
storage: S::new(),
_phantom: std::marker::PhantomData,
}
}

pub fn add_edge(&mut self, from: V, to: V, edge: E) {
self.storage.add_edge(from, to, edge);
}

pub fn edges(&self) -> Vec<E> {
self.storage.edges()
}

pub fn vertices(&self) -> Vec<V> {
self.storage.vertices()
}
pub fn add_vertex(&mut self, vertex: V) {
self.storage.add_vertex(vertex);
}
}
1 change: 1 addition & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ where
{
fn new() -> Self;
fn add_vertex(&mut self, vertex: V);
fn remove_vertex(&mut self, vertex: V);
fn add_edge(&mut self, from: V, to: V, edge: E);
fn remove_edge(&mut self, from: &V, to: &V, edge: E);
fn has_vertex(&self, vertex: &V) -> bool;
Expand Down
47 changes: 37 additions & 10 deletions src/u_graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::storage::GraphStorage;
use std::hash::Hash;
use std::{
hash::Hash,
};
pub struct UGraph<V, E, S>
where
V: Eq + Hash + Clone,
Expand All @@ -22,6 +24,25 @@ where
_phantom: std::marker::PhantomData,
}
}
pub fn neighbors(&self, vertex: &V) -> Vec<(V, E)> {
self.storage.neighbors(vertex)
}

pub fn has_vertex(&self, vertex: &V) -> bool {
self.storage.vertices().contains(vertex)
}

pub fn has_edge(&self, from: &V, to: &V, edge: &E) -> bool {
self.storage.has_edge(from, to, edge)
}

pub fn remove_edge(&mut self, from: &V, to: &V, edge: E) {
self.storage.remove_edge(from, to, edge);
}

pub fn remove_vertex(&mut self, vertex: V) {
self.storage.remove_vertex(vertex);
}

pub fn add_edge(&mut self, from: V, to: V, edge: E) {
self.storage.add_edge(from, to, edge);
Expand All @@ -31,6 +52,9 @@ where
self.storage.edges()
}

pub fn vertices(&self) -> Vec<V> {
self.storage.vertices()
}
pub fn add_vertex(&mut self, vertex: V) {
self.storage.add_vertex(vertex);
}
Expand All @@ -52,23 +76,26 @@ mod u_graph_test_i32 {
assert!(graph.edges().contains(&4));

assert_eq!(graph.edges().len(), 2);

graph.add_edge(1, 2, 5);
assert!(graph.edges().contains(&5));
assert_eq!(graph.edges().len(), 3);

graph.add_edge(1, 2, 3);
assert_eq!(graph.edges().len(), 3);
}

fn test_vertices_template<S: GraphStorage<i32, i32>>() {
let mut graph = UGraph::<i32, i32, S>::new();
assert!(graph.edges().is_empty());

graph.add_vertex(1, 2, 3);
assert!(graph.edges().contains(&3));
assert!(graph.vertices().is_empty());

graph.add_edge(2, 3, 4);
assert!(graph.edges().contains(&4));

assert_eq!(graph.edges().len(), 2);
graph.add_vertex(1);
assert!(graph.vertices().contains(&1));
}

#[test]
fn test_edges() {
fn test_adj_list() {
test_edges_template::<crate::adjacency_list::AdjacencyList<i32, i32>>();
test_vertices_template::<crate::adjacency_list::AdjacencyList<i32, i32>>();
}
}

0 comments on commit ca2dc18

Please sign in to comment.