Trait graaf::gen::path::Path

source ·
pub trait Path {
    // Required method
    fn path(order: usize) -> Self;
}
Expand description

Generate path digraphs.

A path digraph is an arc chain that connects vertices in a linear sequence.

§Implementing Path for a custom type

Provide an implementation of path that generates a path digraph of a given order OR implement AddArc and Empty.

use {
    graaf::{
        AddArc,
        Empty,
        Path,
    },
    std::collections::BTreeSet,
};

struct AdjacencyList {
    arcs: Vec<BTreeSet<usize>>,
}

impl AddArc for AdjacencyList {
    fn add_arc(&mut self, u: usize, v: usize) {
        self.arcs[u].insert(v);
    }
}

impl Empty for AdjacencyList {
    fn empty(order: usize) -> Self {
        Self {
            arcs: vec![BTreeSet::new(); order],
        }
    }
}

let digraph = AdjacencyList::path(3);

assert!(digraph.arcs.iter().eq(&[
    BTreeSet::from([1]),
    BTreeSet::from([2]),
    BTreeSet::new()
]));

Required Methods§

source

fn path(order: usize) -> Self

Generate a path digraph.

§Arguments
  • order - The number of vertices in the digraph.
§Examples
§Order 2

Generate a path digraph of order 2.

Path digraph of order 2

use graaf::{
    AdjacencyList,
    Arcs,
    Path,
};

assert!(AdjacencyList::path(2).arcs().eq([(0, 1)]));
§Order 3

Generate a path digraph of order 3.

Path digraph of order 3

use graaf::{
    AdjacencyList,
    Arcs,
    Path,
};

assert!(AdjacencyList::path(3).arcs().eq([(0, 1), (1, 2)]));
§Order 4

Generate a path digraph of order 4.

Path digraph of order 4

use graaf::{
    AdjacencyList,
    Arcs,
    Path,
};

assert!(AdjacencyList::path(4).arcs().eq([(0, 1), (1, 2), (2, 3)]));

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<D> Path for D
where D: AddArc + Empty,