Struct ttgraph::Graph

source ·
pub struct Graph<NodeT: NodeEnum> { /* private fields */ }
Expand description

A graph with typed nodes The graph can only by modified by commiting a transaction, which avoids mutable borrow of the graph

§Example:

use ttgraph::*;

#[derive(TypedNode)]
struct NodeA{
  link: NodeIndex,
  data: usize,
}

#[derive(TypedNode)]
struct NodeB{
  links: BTreeSet<NodeIndex>,
  another_data: String,
}

node_enum!{
  enum Node{
    A(NodeA),
    B(NodeB)
  }
}

let ctx = Context::new();
let mut graph = Graph::<Node>::new(&ctx);
let mut trans = Transaction::new(&ctx);
// Does some operations on the transaction
graph.commit(trans);

Implementations§

source§

impl<NodeT: NodeEnum> Graph<NodeT>

source

pub fn new(context: &Context) -> Self

Create an empty graph

source

pub fn get(&self, idx: NodeIndex) -> Option<&NodeT>

Get the reference of a node. For convinience, if the type of the node is previously known, use get_node!() instead.

§Example
use ttgraph::*;

#[derive(TypedNode)]
struct NodeA{
  data: usize,
}

node_enum!{
  enum Node{
    A(NodeA)
  }
}

let ctx = Context::new();
let mut graph = Graph::<Node>::new(&ctx);
let mut trans = Transaction::new(&ctx);
let idx = trans.insert(Node::A(NodeA{
  data: 1
}));
graph.commit(trans);

// node: Option<&Node>
let node = graph.get(idx);
if let Some(Node::A(node)) = node {
  assert_eq!(node.data, 1);
} else {
  panic!();
}

assert!(graph.get(NodeIndex::empty()).is_none());
source

pub fn iter(&self) -> Iter<'_, NodeIndex, NodeT>

Iterate all nodes in the graph following the order of NodeIndex. If only a type of node is wanted, use iter_nodes! instead.

§Example
use ttgraph::*;

#[derive(TypedNode)]
struct NodeA{
  a: usize
}
#[derive(TypedNode)]
struct NodeB{
  b: usize
}

node_enum!{
  enum Node{
    A(NodeA),
    B(NodeB),
  }
}

let ctx = Context::new();
let mut graph = Graph::<Node>::new(&ctx);
let mut trans = Transaction::new(&ctx);

trans.insert(Node::A(NodeA{ a: 1 }));
trans.insert(Node::A(NodeA{ a: 2 }));
trans.insert(Node::B(NodeB{ b: 0 }));
graph.commit(trans);

// iterator.next() returns Option<(NodeIndex, &Node)>
let iterator = graph.iter();
for (i, (_, node)) in (1..3).zip(iterator) {
  if let Node::A(a) = node {
    assert_eq!(i, a.a);
  } else {
    panic!();
  }
}
source

pub fn iter_group( &self, name: &'static str, ) -> impl Iterator<Item = (NodeIndex, &NodeT)>

Iterate all nodes within the named group

§Example
use ttgraph::*;
#[derive(TypedNode, Debug)]
struct NodeA {
  a: usize,
}
#[derive(TypedNode, Debug)]
struct NodeB {
  b: usize,
}
#[derive(TypedNode, Debug)]
struct NodeC {
  c: usize,
}
#[derive(TypedNode, Debug)]
struct NodeD {
  d: usize,
}

node_enum! {
  #[derive(Debug)]
  enum MultiNodes{
    A(NodeA),
    B(NodeB),
    C(NodeC),
    D(NodeD),
  }
  group!{
    first{A, B},
    second{C, D},
    third{A, D},
    one{B},
    all{A, B, C, D},
  }
}

 let ctx = Context::new();
 let mut graph = Graph::<MultiNodes>::new(&ctx);
 let mut trans = Transaction::new(&ctx);
 let a = trans.insert(MultiNodes::A(NodeA { a: 1 }));
 let b = trans.insert(MultiNodes::B(NodeB { b: 2 }));
 let c = trans.insert(MultiNodes::C(NodeC { c: 3 }));
 let d = trans.insert(MultiNodes::D(NodeD { d: 4 }));
 graph.commit(trans);

 assert_eq!(Vec::from_iter(graph.iter_group("first").map(|(x, _)| x)), vec![a, b]);
 assert_eq!(Vec::from_iter(graph.iter_group("second").map(|(x, _)| x)), vec![c, d]);
 assert_eq!(Vec::from_iter(graph.iter_group("third").map(|(x, _)| x)), vec![a, d]);
 assert_eq!(Vec::from_iter(graph.iter_group("one").map(|(x, _)| x)), vec![b]);
 assert_eq!(Vec::from_iter(graph.iter_group("all").map(|(x, _)| x)), vec![a, b, c, d]);
source

pub fn len(&self) -> usize

Get the number of nodes in a graph

§Example
use ttgraph::*;
#[derive(TypedNode)]
struct NodeA{
  data: usize,
}
node_enum!{
  enum Node{
    A(NodeA)
  }
}

let ctx = Context::new();
let mut graph = Graph::<Node>::new(&ctx);
assert_eq!(graph.len(), 0);
let mut trans = Transaction::new(&ctx);
trans.insert(Node::A(NodeA{data: 1}));
trans.insert(Node::A(NodeA{data: 1}));
trans.insert(Node::A(NodeA{data: 1}));
graph.commit(trans);
assert_eq!(graph.len(), 3);
source

pub fn is_empty(&self) -> bool

Check if the graph has no node

source

pub fn commit(&mut self, t: Transaction<'_, NodeT>)

Commit an Transaction to modify the graph Operation order:

  • Redirect nodes
  • Insert new nodes
  • Modify nodes
  • Update nodes
  • Redirect all nodes
  • Remove nodes
  • Add/Remove links due to bidirectional declaration
  • Check link types
§Panics

Panic if the transaction and the graph have different context

§Example
use ttgraph::*;
#[derive(TypedNode)]
struct NodeA{
  data: usize,
}
node_enum!{
  enum Node{
    A(NodeA)
  }
}

let ctx = Context::new();
let mut graph = Graph::<Node>::new(&ctx);
let mut trans = Transaction::new(&ctx);
trans.insert(Node::A(NodeA{data: 1}));
graph.commit(trans);
source

pub fn commit_checked( &mut self, t: Transaction<'_, NodeT>, checks: &GraphCheck<NodeT>, )

Similar to commit(), but with additional checks on the changed nodes and links. See GraphCheck for more information.

source

pub fn switch_context(self, new_ctx: &Context) -> Self

Switch the context and relabel the node ids.

§Usecase:
  • Useful when there are a lot of removed NodeIndex, and after context switching the indexes will be more concise.
  • Merge two graphs with different context. See merge for example.
§Warning:
  • Please ensure there is no uncommitted transactions!
  • NodeIndex pointing to this graph is useless after context switching!
source

pub fn check_integrity(&self)

Check if all links are internal, just for debug

Trait Implementations§

source§

impl<NodeT: Clone + NodeEnum> Clone for Graph<NodeT>
where NodeT::SourceEnum: Clone,

source§

fn clone(&self) -> Graph<NodeT>

Returns a copy 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<T: NodeEnum + Debug> Debug for Graph<T>

source§

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

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

impl<T: NodeEnum + Display> Display for Graph<T>

source§

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

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

impl<NodeT> From<Graph<NodeT>> for GraphSerializer<NodeT>
where NodeT: NodeEnum,

source§

fn from(value: Graph<NodeT>) -> GraphSerializer<NodeT>

Converts to this type from the input type.
source§

impl<'a, T: NodeEnum + 'a> IntoIterator for &'a Graph<T>

source§

type IntoIter = Iter<'a, NodeIndex, T>

Which kind of iterator are we turning this into?
source§

type Item = (NodeIndex, &'a T)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: NodeEnum> IntoIterator for Graph<T>

source§

type IntoIter = IntoIter<NodeIndex, T>

Which kind of iterator are we turning this into?
source§

type Item = (NodeIndex, T)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<NodeT> Serialize for Graph<NodeT>
where NodeT: NodeEnum + Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<NodeT> Freeze for Graph<NodeT>

§

impl<NodeT> RefUnwindSafe for Graph<NodeT>
where NodeT: RefUnwindSafe, <NodeT as NodeEnum>::SourceEnum: RefUnwindSafe,

§

impl<NodeT> Send for Graph<NodeT>
where NodeT: Send, <NodeT as NodeEnum>::SourceEnum: Send,

§

impl<NodeT> Sync for Graph<NodeT>
where NodeT: Sync, <NodeT as NodeEnum>::SourceEnum: Sync,

§

impl<NodeT> Unpin for Graph<NodeT>

§

impl<NodeT> UnwindSafe for Graph<NodeT>
where NodeT: RefUnwindSafe, <NodeT as NodeEnum>::SourceEnum: RefUnwindSafe,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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