1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
use gt_graph::{Graph, Node}; use std::fmt::{Debug, Error, Formatter}; #[derive(Clone)] pub struct GraphPath<'a> { graph: &'a dyn Graph, path: Vec<Node>, } impl<'a> PartialEq for GraphPath<'a> { fn eq(&self, other: &Self) -> bool { self.path == other.path } } impl<'a> GraphPath<'a> { #[inline(always)] pub fn new(graph: &'a dyn Graph) -> Self { Self { graph, path: Vec::new(), } } #[inline(always)] pub fn new_with_initial_size(graph: &'a dyn Graph, size: usize) -> Self { Self { graph, path: Vec::with_capacity(size), } } pub fn from_vec(graph: &'a dyn Graph, src: Vec<Node>) -> Self { Self { graph, path: src } } #[inline(always)] pub fn push_back(&mut self, n: Node) { self.path.push(n); } pub fn inner_path(&self) -> &Vec<Node> { &self.path } pub fn inner_path_mut(&mut self) -> &mut Vec<Node> { &mut self.path } pub fn is_valid(&self) -> bool { self.path .iter() .take(self.path.len() - 1) .zip(self.path.iter().skip(1)) .all(|(&first, &second)| { (1..=self.graph.dimension()).any(|n| self.graph.phi(n, first) == second) }) } } impl<'a> Debug for GraphPath<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { let mut s = String::new(); if !self.path.is_empty() { s.push_str(&format!( "{:0dim$b}", self.path[0], dim = self.graph.dimension() as usize )); for p in self.path.iter().skip(1) { s.push_str(&format!( " -> {:0dim$b}", p, dim = self.graph.dimension() as usize )); } } write!(f, "NodePath {{ {} }}", s) } }