Struct SimpleVertex

Source
pub struct SimpleVertex<K: Hash + Eq + Clone, V> { /* private fields */ }
Expand description

A default implementation for vertexes. This implementation should be suitable for most of the problem one can encounter requiring graph.

Contrary to other graph implementation this library does not expect the vertexes to store if they have been visited, or if they were marked. The reason for this is that the author believes such an information should be stored in a structure extern and independent to the graph, this to ensure consistency between threads and to allow different algorithms to use different structures according to their needs

methods are self explanatory

Implementations§

Source§

impl<K: Hash + Eq + Clone, V> SimpleVertex<K, V>

Source

pub fn new(key: K, value: V) -> SimpleVertex<K, V>

Creates a new instance of SimpleVertex

Trait Implementations§

Source§

impl<K: Debug + Hash + Eq + Clone, V: Debug> Debug for SimpleVertex<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash + Eq + Clone, V, W: Add + Sub + Eq + Ord + Copy> DirectedGraph<SimpleVertex<K, V>, DirectedEdge<K, W>, K, V, W, CompoundKey<K>> for AdjacencyGraph<K, V, W>

AdjacencyGraph implement the DirectedGraph trait Specifying the vertex type (DirectedVertex), the edge type (Directed Edge), and the edge key type (CompoundKey). But the vertex key type, the vertex value type and the edge weight type remain generics.

Source§

fn adjacent(&self, from: &K, to: &K) -> bool

Check if an edge going from the first to the second vertex exists

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges, DirectedGraph};
use generic_graph::adjacency_list::elements::DirectedEdge;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));
graph.add_edge(DirectedEdge::new(2, 3, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(1, 3, 3)).expect("Won't fail");
assert_eq!(true, graph.adjacent(&1, &3));
assert_eq!(false, graph.adjacent(&3, &1));
assert_eq!(false, graph.adjacent(&2, &1));
Source§

fn neighbors(&self, from: &K) -> Vec<&K>

Returns a Vector containing the keys of the vertexes reached by edges leaving from the vertex identified by the passed key

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges, DirectedGraph};
use generic_graph::adjacency_list::elements::DirectedEdge;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));
graph.add_edge(DirectedEdge::new(2, 3, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(2, 1, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(1, 3, 3)).expect("Won't fail");

let mut neighbors = graph.neighbors(&2);
neighbors.sort();
assert_eq!(neighbors, vec![&1,&3]);
Source§

fn leading_to(&self, to: &K) -> Vec<&K>

Returns a vector containing the keys of the Vertexes from which an edge leave to reach the vertex identified by the passed key

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges, DirectedGraph};
use generic_graph::adjacency_list::elements::DirectedEdge;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));
graph.add_edge(DirectedEdge::new(2, 3, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(1, 3, 3)).expect("Won't fail");

let mut leading_to = graph.leading_to(&3);
leading_to.sort();
assert_eq!(leading_to, vec![&1,&2]);
Source§

fn get_all_keys(&self) -> Vec<&K>

Returns a vector containing the references to keys of all vertexes in the graph

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges, DirectedGraph};
use generic_graph::adjacency_list::elements::DirectedEdge;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));
graph.add_edge(DirectedEdge::new(2, 3, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(1, 3, 3)).expect("Won't fail");

let mut keys = graph.get_all_keys();
keys.sort();
assert_eq!(keys, vec![&1, &2, &3]);
Source§

fn get_all_pairs(&self) -> Vec<(&K, &K)>

Returns a vector containing the pairs of all edges in the graph

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges, DirectedGraph};
use generic_graph::adjacency_list::elements::DirectedEdge;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));
graph.add_edge(DirectedEdge::new(2, 3, 3)).expect("Won't fail");
graph.add_edge(DirectedEdge::new(1, 3, 3)).expect("Won't fail");

let mut pairs = graph.get_all_pairs();
pairs.sort();
assert_eq!(pairs, vec![(&1, &3), (&2, &3)]);
Source§

fn get_vertex(&self, key: &K) -> Option<&SimpleVertex<K, V>>

Returns a reference to the vertex identified by the passed key

Source§

fn get_mut_vertex(&mut self, key: &K) -> Option<&mut SimpleVertex<K, V>>

Returns a mutable reference to the vertex identified by the passed key

Source§

fn get_edge(&self, pair: (&K, &K)) -> Option<&DirectedEdge<K, W>>

Returns a reference to the edge identified by the passed pair of keys

Source§

fn get_mut_edge(&mut self, pair: (&K, &K)) -> Option<&mut DirectedEdge<K, W>>

Returns a mutable reference to the edge identified by the passed pair of keys

Source§

impl<K: PartialEq + Hash + Eq + Clone, V: PartialEq> PartialEq for SimpleVertex<K, V>

Source§

fn eq(&self, other: &SimpleVertex<K, V>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: Hash + Eq + Clone, V, W: Add + Sub + Eq + Ord + Copy> VariableEdges<SimpleVertex<K, V>, DirectedEdge<K, W>, K, V, W, CompoundKey<K>> for AdjacencyGraph<K, V, W>

AdjacencyGraph uses HashMaps to store edges, allowing fast insertion and removal of the latter

Source§

fn add_edge( &mut self, edge: DirectedEdge<K, W>, ) -> Result<Option<DirectedEdge<K, W>>, EdgeSide>

The add_edge() method shall return Ok(None) if the element was not previously set. Otherwise the element shall be updated (but no the key) and the old element shall be returned as Ok(Some(old_element)). If one or both of the concerned vertexes are missing an error containing an enum specifying which side is missing (Err(EdgeSide))

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges};
use generic_graph::adjacency_list::elements::DirectedEdge;
use generic_graph::EdgeSide::Right;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));
graph.add_vertex(SimpleVertex::new(3, "c"));

assert_eq!(Ok(None), graph.add_edge(DirectedEdge::new(1, 2, 0)));
assert_eq!(Ok(None), graph.add_edge(DirectedEdge::new(2, 1, 0)));
assert_eq!(Ok(None), graph.add_edge(DirectedEdge::new(3, 2, 0)));
assert_eq!(
     Ok(Some(DirectedEdge::new(1, 2, 0))),
     graph.add_edge(DirectedEdge::new(1, 2, 3))
);
assert_eq!(Err(Right), graph.add_edge(DirectedEdge::new(1, 4, 0)));
Source§

fn remove_edge(&mut self, pair: (&K, &K)) -> Option<DirectedEdge<K, W>>

The remove_edge() method shall return None if the element was not found, or Some(element) if it was found and removed.

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes, VariableEdges};
use generic_graph::adjacency_list::elements::DirectedEdge;
use generic_graph::EdgeSide::Right;
let mut graph = AdjacencyGraph::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));

graph.add_edge(DirectedEdge::new(1, 2, 3));

assert_eq!(
        Some(DirectedEdge::new(1, 2, 3)),
        graph.remove_edge((&1, &2))
);
assert_eq!(None, graph.remove_edge((&1, &2)));
Source§

impl<K: Hash + Eq + Clone, V, W: Add + Sub + Eq + Ord + Copy> VariableVertexes<SimpleVertex<K, V>, DirectedEdge<K, W>, K, V, W, CompoundKey<K>> for AdjacencyGraph<K, V, W>

AdjacencyGraph uses HashMaps to store vertexes, allowing fast insertion and removal of the latter

Source§

fn add_vertex( &mut self, vertex: SimpleVertex<K, V>, ) -> Option<SimpleVertex<K, V>>

This method adds (or, if present, updates maintaining its edges) a vertex and returns None ore Some(old_vertex)

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes};
let mut graph = AdjacencyGraph::<i32, &str, i32>::new();

assert_eq!(None, graph.add_vertex(SimpleVertex::new(1, "a")));
assert_eq!(None, graph.add_vertex(SimpleVertex::new(2, "b")));
assert_eq!(Some(SimpleVertex::new(1, "a")), graph.add_vertex(SimpleVertex::new(1, "c")))
Source§

fn remove_vertex(&mut self, key: K) -> Option<SimpleVertex<K, V>>

This method removes a vertex and its edges from the graph and returns None ore Some(old_vertex)

§Examples
use generic_graph::adjacency_list::AdjacencyGraph;
use generic_graph::{SimpleVertex, VariableVertexes};
let mut graph = AdjacencyGraph::<i32, &str, i32>::new();
graph.add_vertex(SimpleVertex::new(1, "a"));
graph.add_vertex(SimpleVertex::new(2, "b"));

assert_eq!(None, graph.remove_vertex(0));
assert_eq!(Some(SimpleVertex::new(1, "a")), graph.remove_vertex(1));
assert_eq!(Some(SimpleVertex::new(2, "b")), graph.remove_vertex(2));
assert_eq!(None, graph.remove_vertex(1));
Source§

impl<K: Hash + Eq + Clone, V> Vertex<K, V> for SimpleVertex<K, V>

SimpleVertex implement the Vertex trait maintaining the key type and the value type generics

Source§

fn get_value(&self) -> &V

Get the value stored in a vertex

Source§

fn get_mut_value(&mut self) -> &mut V

Get the value as mutable reference

Source§

fn key(&self) -> K

Returns the key of the vertex

Source§

impl<K: Eq + Hash + Eq + Clone, V: Eq> Eq for SimpleVertex<K, V>

Source§

impl<K: Hash + Eq + Clone, V> StructuralPartialEq for SimpleVertex<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for SimpleVertex<K, V>
where K: Freeze, V: Freeze,

§

impl<K, V> RefUnwindSafe for SimpleVertex<K, V>

§

impl<K, V> Send for SimpleVertex<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for SimpleVertex<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for SimpleVertex<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SimpleVertex<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.