Graph

Struct Graph 

Source
pub struct Graph<K, V>(/* private fields */)
where
    K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug,
    V: PartialEq + Clone + Debug;
Expand description

This struct contains all functionality implemented in this module. It is can be used for building and sorting a graph of causally connected nodes.

Sorting is deterministic with > comparison of contained node data being the deciding factor on which paths to walk first.

§Example

use p2panda_rs::graph::{Graph, Reducer};
use p2panda_rs::graph::error::ReducerError;

// First we define a reducer we will use later on.

#[derive(Default)]
struct CharReducer {
    acc: String,
}

impl Reducer<char> for CharReducer {
    type Error = ReducerError;

    fn combine(&mut self, value: &char) -> Result<(), Self::Error> {
        self.acc = format!("{}{}", self.acc, value);
        Ok(())
    }
}

// Instantiate the graph.

let mut graph = Graph::new();

// Add some nodes to the graph.

graph.add_node(&'a', 'A');
graph.add_node(&'b', 'B');
graph.add_node(&'c', 'C');

assert!(graph.get_node(&'a').is_some());
assert!(graph.get_node(&'x').is_none());

// Add some links between the nodes.

graph.add_link(&'a', &'b');
graph.add_link(&'a', &'c');

// The graph looks like this:
//
//  /--[B]
// [A]<--[C]

// We can sort it topologically and pass in our reducer.

let mut reducer = CharReducer::default();
let nodes = graph.reduce(&mut reducer)?;

assert_eq!(nodes.sorted(), vec!['A', 'B', 'C']);
assert_eq!(reducer.acc, "ABC".to_string());

// Add another link which creates a cycle (oh dear!).

graph.add_link(&'b', &'a');

let mut reducer = CharReducer::default();
assert!(graph.reduce(&mut reducer).is_err());

Implementations§

Source§

impl<'a, K, V> Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug, V: PartialEq + Clone + Debug,

Source

pub fn new() -> Self

Instantiate a new empty graph.

Source

pub fn add_node(&mut self, key: &K, data: V)

Add a node to the graph. This node will be detached until it is linked to another node.

Add a link between existing nodes to the graph. Returns true if the link was added. Returns false if the link was unable to be added. This happens if either of the nodes were not present in the graph, or if the link creates a single node loop.

Source

pub fn get_node(&'a self, key: &K) -> Option<&Node<K, V>>

Get node from the graph by key, returns None if it wasn’t found.

Source

pub fn root_node(&self) -> Result<&Node<K, V>, GraphError>

Returns a reference to the root node of this graph.

Source

pub fn root_node_key(&self) -> Result<&K, GraphError>

Returns the root node key.

Source

pub fn trim(&'a mut self, tip_nodes: &[K]) -> Result<Graph<K, V>, GraphError>

Returns a new graph instance which has had all nodes removed which aren’t causally linked to the passed nodes.

Data contained in the returned nodes is unchanged, but the nodes’ next arrays are edited to reflect the new graph connections. The returned graph can be sorted in order to arrive at a linear ordering of nodes.

Source

pub fn walk_from( &'a self, key: &K, reducer: &mut impl Reducer<V>, ) -> Result<GraphData<V>, GraphError>

Sorts the graph topologically and returns the result.

Source

pub fn reduce( &'a self, reducer: &mut impl Reducer<V>, ) -> Result<GraphData<V>, GraphError>

Sort the entire graph, starting from the root node.

Accepts a mutable reducer as an argument. As each node is sorted into topological order its value is passed into the combine method.

Trait Implementations§

Source§

impl<K, V> Clone for Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug + Clone, V: PartialEq + Clone + Debug + Clone,

Source§

fn clone(&self) -> Graph<K, V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug + Debug, V: PartialEq + Clone + Debug + Debug,

Source§

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

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

impl<K, V> Default for Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug, V: PartialEq + Clone + Debug,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V> PartialEq for Graph<K, V>

Source§

fn eq(&self, other: &Graph<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, V> Eq for Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug + Eq, V: PartialEq + Clone + Debug + Eq,

Source§

impl<K, V> StructuralPartialEq for Graph<K, V>
where K: Hash + Ord + PartialOrd + Eq + PartialEq + Clone + Debug, V: PartialEq + Clone + Debug,

Auto Trait Implementations§

§

impl<K, V> Freeze for Graph<K, V>

§

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

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for Graph<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V