1use fnv::{FnvHashMap, FnvHasher};
2use std::hash::{BuildHasherDefault, Hash};
3
4pub struct Graph<V, E> {
5 vertices: FnvHashMap<V, Vec<E>>,
6}
7
8impl<V, E> Graph<V, E>
9where
10 V: Eq + Hash,
11 E: Eq,
12{
13 pub fn new() -> Graph<V, E> {
14 Graph {
15 vertices: FnvHashMap::default(),
16 }
17 }
18
19 pub fn with_capacity_and_hasher(
20 capacity: usize,
21 hash_builder: BuildHasherDefault<FnvHasher>,
22 ) -> Graph<V, E> {
23 Graph {
24 vertices: FnvHashMap::with_capacity_and_hasher(capacity, hash_builder),
25 }
26 }
27
28 pub fn insert_vertice(&mut self, vertice: V) {
29 self.vertices.insert(vertice, Vec::new());
30 }
31
32 pub fn remove_edge(&mut self, vertice: &V, edge: &E) {
33 if let Some(vert) = self.vertices.get_mut(vertice) {
34 vert.retain(|e| e != edge);
35 };
36 }
37
38 pub fn add_edge(&mut self, vertice: &V, edge: E) {
39 if let Some(vert) = self.vertices.get_mut(vertice) {
40 vert.push(edge);
41 }
42 }
43}