grapes/graph/
edge_mut.rs

1//! Mutable references to edges
2
3use crate::graph::{Edge, EdgeId, Graph, NodeMut, NodeRef};
4
5/// Mutable reference to an edge in a [Graph]
6///
7/// The library guarantees that this references a valid edge
8#[derive(Debug)]
9pub struct EdgeMut<'a, NodeData: Clone, EdgeData: Clone> {
10    graph: &'a mut Graph<NodeData, EdgeData>,
11    index: EdgeId,
12}
13
14impl<'a, NodeData: Clone, EdgeData: Clone> EdgeMut<'a, NodeData, EdgeData> {
15    #[must_use]
16    pub(crate) fn new(graph: &'a mut Graph<NodeData, EdgeData>, index: EdgeId) -> Self {
17        Self { graph, index }
18    }
19
20    /// Get the Edge this [EdgeMut] is pointing to
21    ///
22    /// (internal convenience method)
23    #[must_use]
24    fn edge(&self) -> &Edge<EdgeData> {
25        self.graph
26            .edges
27            .get(self.index.0)
28            .expect("invariant violation: EdgeMut points to invalid edge")
29    }
30
31    /// Get a mutable reference to the Edge this [EdgeMut] is pointing to
32    ///
33    /// (internal convenience method)
34    #[must_use]
35    fn edge_mut(&mut self) -> &mut Edge<EdgeData> {
36        self.graph
37            .edges
38            .get_mut(self.index.0)
39            .expect("invariant violation: EdgeMut points to invalid edge")
40    }
41
42    /// The data associated with this edge
43    ///
44    /// (aka the edge weight)
45    #[must_use]
46    pub fn data(&self) -> &EdgeData {
47        &self.edge().data
48    }
49
50    /// A mutable reference to data associated with this edge
51    ///
52    /// (aka the edge weight)
53    #[must_use]
54    pub fn data_mut(&mut self) -> &mut EdgeData {
55        &mut self.edge_mut().data
56    }
57
58    /// The edge's identifier
59    ///
60    /// You can use this to later reference the edge in the graph (if it still exists)
61    #[must_use]
62    pub fn id(&self) -> EdgeId {
63        self.index
64    }
65
66    /// Reference to the Node from which the edge starts
67    ///
68    /// (aka the tail of the arc)
69    #[must_use]
70    pub fn get_from_node(&self) -> NodeRef<NodeData, EdgeData> {
71        NodeRef::new(self.graph, self.edge().from)
72    }
73
74    /// Mutable reference to the Node from which the edge starts
75    ///
76    /// (aka the tail of the arc)
77    #[must_use]
78    pub fn get_from_node_mut(&mut self) -> NodeMut<NodeData, EdgeData> {
79        NodeMut::new(self.graph, self.edge().from)
80    }
81
82    /// Reference to the Node the edge points to
83    ///
84    /// (aka the head of the arc)
85    #[must_use]
86    pub fn get_to_node(&self) -> NodeRef<NodeData, EdgeData> {
87        NodeRef::new(self.graph, self.edge().to)
88    }
89
90    /// Mutable reference to the Node the edge points to
91    ///
92    /// (aka the head of the arc)
93    #[must_use]
94    pub fn get_to_node_mut(&mut self) -> NodeMut<NodeData, EdgeData> {
95        NodeMut::new(self.graph, self.edge().to)
96    }
97
98    /// Remove this edge from the graph
99    ///
100    /// Returns the data of the removed edge
101    ///
102    /// Note: there may be other edges with the same source and target nodes
103    #[allow(clippy::must_use_candidate)]
104    pub fn remove(self) -> EdgeData {
105        let removed = self
106            .graph
107            .edges
108            .remove(self.index.0)
109            .expect("invariant violation: EdgeMut points to invalid edge");
110
111        self.graph.detach_edge_from_from_list(&removed);
112        self.graph.detach_edge_from_to_list(&removed);
113
114        removed.data
115    }
116}