Struct PredecessorTree

Source
#[non_exhaustive]
pub struct PredecessorTree { pub pred: Vec<Option<usize>>, }
Expand description

A predecessor tree.

A PredecessorTree contains each vertex’s predecessor on the shortest path from the source vertex.

§Examples

This PredecessorTree is generated by BfsPred::predecessors.

The path from vertex 0 is red. The dashed arcs mark the PredecessorTree.

A digraph and the predecessor tree generated by breadth-first search

use {
    graaf::{
        AddArc,
        AdjacencyList,
        BfsPred,
        Empty,
        PredecessorTree,
    },
    std::iter::once,
};

let mut digraph = AdjacencyList::empty(6);

digraph.add_arc(0, 1);
digraph.add_arc(1, 2);
digraph.add_arc(1, 4);
digraph.add_arc(2, 5);

assert!(BfsPred::new(&digraph, once(0))
    .predecessors()
    .into_iter()
    .eq([None, Some(0), Some(1), None, Some(1), Some(2)]));

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§pred: Vec<Option<usize>>

The predecessors of each vertex.

Implementations§

Source§

impl PredecessorTree

Source

pub fn new(order: usize) -> Self

Construct a PredecessorTree with a given order.

§Arguments
§Panics

Panics if order is zero.

§Examples
use graaf::PredecessorTree;

assert!(PredecessorTree::new(4)
    .into_iter()
    .eq([None, None, None, None]));
Source

pub fn search(&self, s: usize, t: usize) -> Option<Vec<usize>>

Search a PredecessorTree for a path from a source vertex to a target vertex.

§Arguments
  • s: The source vertex.
  • t: The target vertex.
§Returns

If a path from s to t is found, the function returns it. Otherwise, returns None.

§Examples
use graaf::PredecessorTree;

let pred = PredecessorTree::from(vec![Some(1), Some(2), Some(3), None]);

assert!(pred.search(0, 3).into_iter().eq(Some(vec![0, 1, 2, 3])));
Source

pub fn search_by<F>(&self, s: usize, is_target: F) -> Option<Vec<usize>>
where F: Fn(&usize, &Option<usize>) -> bool,

Search a PredecessorTree for a path from a source vertex to a vertex that satisfies a predicate.

§Arguments
  • s: The source vertex.
  • is_target: A predicate determining if a vertex is the target.
§Returns

If it finds a target, it returns the path from the source to the target. Otherwise, it returns None.

§Panics

Panics if s isn’t in the path.

§Examples
use graaf::PredecessorTree;

let pred = PredecessorTree::from(vec![Some(1), Some(2), Some(3), None]);

assert!(pred
    .search_by(0, |&v, _| v > 1)
    .into_iter()
    .eq(Some(vec![0, 1, 2])));

let pred =
    PredecessorTree::from(vec![Some(1), Some(2), Some(3), None, Some(0)]);

assert!(pred
    .search_by(0, |_, v| v.is_none())
    .into_iter()
    .eq(Some(vec![0, 1, 2, 3])));

Trait Implementations§

Source§

impl Clone for PredecessorTree

Source§

fn clone(&self) -> PredecessorTree

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 Debug for PredecessorTree

Source§

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

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

impl From<Vec<Option<usize>>> for PredecessorTree

Source§

fn from(pred: Vec<Option<usize>>) -> Self

Converts to this type from the input type.
Source§

impl Hash for PredecessorTree

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for PredecessorTree

Source§

type Output = Option<usize>

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for PredecessorTree

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IntoIterator for PredecessorTree

Source§

type IntoIter = IntoIter<<PredecessorTree as IntoIterator>::Item>

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

type Item = Option<usize>

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 Ord for PredecessorTree

Source§

fn cmp(&self, other: &PredecessorTree) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for PredecessorTree

Source§

fn eq(&self, other: &PredecessorTree) -> 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 PartialOrd for PredecessorTree

Source§

fn partial_cmp(&self, other: &PredecessorTree) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for PredecessorTree

Source§

impl StructuralPartialEq for PredecessorTree

Auto Trait Implementations§

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> 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.