graph_types/edges/
mod.rs

1use std::{
2    cmp::{max, min},
3    ops::Range,
4};
5
6use std::fmt::{Display, Formatter};
7
8pub mod actions;
9pub mod typed_edges;
10
11mod simple;
12
13pub mod mut_iter;
14
15/// used to determine the direction of an edge
16pub type EdgeID = usize;
17
18/// Marker trait for edges
19///
20/// # Examples
21///
22/// ```
23/// use graph_theory::GraphEngine;
24/// ```
25pub trait Edge: Display {
26    /// Whether the edge is bidirectional
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// use graph_theory::DirectedEdge;
32    /// ```
33    fn direction(&self) -> EdgeDirection;
34    /// The left side node id of the edge
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// use graph_theory::GraphEngine;
40    /// ```
41    fn lhs(&self) -> usize;
42    /// The right side node id of the edge
43    ///
44    /// # Examples
45    ///
46    /// ```
47    /// use graph_theory::GraphEngine;
48    /// ```
49    fn rhs(&self) -> usize;
50    /// The smaller of the two indices.
51    ///
52    /// # Examples
53    ///
54    /// ```
55    /// use graph_theory::{Edge, UndirectedEdge};
56    /// assert_eq!(UndirectedEdge::new(1, 2).max_index(), 2);
57    /// assert_eq!(UndirectedEdge::new(2, 1).max_index(), 2);
58    /// ```
59    fn max_index(&self) -> usize {
60        max(self.lhs(), self.rhs())
61    }
62    /// The smaller of the two indices.
63    ///
64    /// # Examples
65    ///
66    /// ```
67    /// use graph_theory::{Edge, UndirectedEdge};
68    /// assert_eq!(UndirectedEdge::new(1, 2).min_index(), 1);
69    /// assert_eq!(UndirectedEdge::new(2, 1).min_index(), 1);
70    /// ```
71    fn min_index(&self) -> usize {
72        min(self.lhs(), self.rhs())
73    }
74
75    /// The smaller of the two indices.
76    ///
77    /// # Examples
78    ///
79    /// ```
80    /// use graph_theory::{Edge, UndirectedEdge};
81    /// assert_eq!(UndirectedEdge::new(1, 3).delta_index(), 2);
82    /// assert_eq!(UndirectedEdge::new(3, 1).delta_index(), 2);
83    /// ```
84    fn delta_index(&self) -> usize {
85        self.max_index() - self.min_index()
86    }
87}
88
89/// Determines the direction between two nodes
90///
91/// ```
92/// use graph_theory::GraphEngine;
93/// ```
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub enum EdgeDirection {
96    /// Two nodes that are not connected.
97    Disconnect,
98    /// [EdgeDirection::Forward] in a directed graph, [EdgeDirection::TwoWay] in an undirected graph
99    Indeterminate,
100    /// This edge is bidirectional
101    TwoWay,
102    /// This edge is unidirectional
103    Forward,
104    /// This edge is unidirectional and goes in the opposite direction
105    Reverse,
106}