zawgl_core/graph/
container.rs

1// MIT License
2// Copyright (c) 2022 Alexandre RICCIARDI
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use super::*;
22use super::traits::*;
23
24pub struct GraphContainer<NODE: Clone, RELATIONSHIP: Clone> {
25    graph: Graph<NODE, RELATIONSHIP>,
26}
27
28impl <N: Clone, R: Clone> Clone for GraphContainer<N, R> {
29    fn clone(&self) -> Self {
30        Self { graph: self.graph.clone() }
31    }
32}
33
34impl <NODE: Clone, RELATIONSHIP: Clone> GraphContainer<NODE, RELATIONSHIP> {
35
36    pub fn get_node_mut(&mut self, id: &NodeIndex) -> &mut NODE {
37        &mut self.graph.vertices[id.get_index()].node
38    }
39
40    pub fn get_relationship_mut(&mut self, id: &EdgeIndex) -> &mut RELATIONSHIP {
41        &mut self.graph.edges[id.get_index()].relationship
42    }
43
44    pub fn get_node_ref(&self, id: &NodeIndex) -> &NODE {
45        &self.graph.vertices[id.get_index()].node
46    }
47
48    pub fn get_relationship_ref(&self, id: &EdgeIndex) -> &RELATIONSHIP {
49        &self.graph.edges[id.get_index()].relationship
50    }
51}
52
53impl <NODE: Clone, RELATIONSHIP: Clone> GraphContainer<NODE, RELATIONSHIP> {
54    
55    pub fn out_edges(&self, source: &NodeIndex) -> OutEdges<'_, RELATIONSHIP> {
56        self.get_inner_graph().out_edges(source)
57    }
58
59    pub fn in_edges(&self, target: &NodeIndex) -> InEdges<'_, RELATIONSHIP> {
60        self.get_inner_graph().in_edges(target)
61    }
62    pub fn in_degree(&self, node: &NodeIndex) -> usize {
63        self.in_edges(node).count()
64    }
65    pub fn out_degree(&self, node: &NodeIndex) -> usize {
66        self.out_edges(node).count()
67    }
68}
69
70impl <NODE: Clone, RELATIONSHIP: Clone> GraphContainer<NODE, RELATIONSHIP> {
71
72    pub fn get_source_index(&self, edge_index: &EdgeIndex) -> NodeIndex {
73        self.get_inner_graph().get_source_index(edge_index)
74    }
75    pub fn get_target_index(&self, edge_index: &EdgeIndex) -> NodeIndex {
76        self.get_inner_graph().get_target_index(edge_index)
77    }
78    pub fn nodes_len(&self) -> usize {
79        self.graph.vertices.len()
80    }
81    pub fn edges_len(&self) -> usize {
82        self.graph.edges_len()
83    }
84    pub fn get_nodes_ids(&self) -> Vec<NodeIndex> {
85        (0..self.nodes_len()).map(NodeIndex::new).collect()
86    }
87}
88impl<NODE: Clone, RELATIONSHIP: Clone> Default for GraphContainer<NODE, RELATIONSHIP> {
89    fn default() -> Self {
90        Self::new()
91    }
92}
93impl <NODE: Clone, RELATIONSHIP: Clone> GraphContainer<NODE, RELATIONSHIP> {
94    pub fn new() -> Self {
95        GraphContainer {graph: Graph::new()}
96    }
97    pub fn add_node(&mut self, node: NODE) -> NodeIndex {
98        self.graph.add_vertex(node)
99    }
100
101    pub fn add_relationship(&mut self, rel: RELATIONSHIP, source: NodeIndex, target: NodeIndex) -> EdgeIndex {
102        self.graph.add_edge(rel, source, target)
103    }
104    
105    pub fn insert_node(&mut self, node: NODE, edge_index: &EdgeIndex) -> (EdgeIndex, EdgeIndex) {
106        self.graph.insert_vertex(node, edge_index)
107    }
108
109    pub fn get_inner_graph(&self) -> &Graph<NODE, RELATIONSHIP> {
110        &self.graph
111    }
112
113    pub fn get_relationships_and_edges(&self) -> Vec<&EdgeData<NodeIndex, EdgeIndex, RELATIONSHIP>> {
114        self.get_edges()
115    }
116
117    pub fn get_nodes_with_ids(&self) -> Vec<(&NODE, NodeIndex)> {
118        self.graph.vertices.iter().map(|v| &v.node).zip(self.graph.get_nodes_ids()).collect()
119    }
120
121    pub fn get_relationships(&self) -> Vec<&RELATIONSHIP> {
122        self.graph.edges.iter().map(|e| &e.relationship).collect()
123    }
124
125    pub fn get_relationships_mut(&mut self) -> Vec<&mut RELATIONSHIP> {
126        self.graph.edges.iter_mut().map(|e| &mut e.relationship).collect()
127    }
128
129    pub fn get_edges(&self) -> Vec<&EdgeData<NodeIndex, EdgeIndex, RELATIONSHIP>> {
130        self.graph.edges.iter().filter(|e| !e.discard).collect::<Vec<&EdgeData<NodeIndex, EdgeIndex, RELATIONSHIP>>>()
131    }
132
133    pub fn get_edges_mut(&mut self) -> &mut Vec<EdgeData<NodeIndex, EdgeIndex, RELATIONSHIP>> {
134        &mut self.graph.edges
135    }
136    pub fn get_nodes(&self) -> Vec<&NODE> {
137        self.graph.vertices.iter().map(|v| &v.node).collect()
138    }
139
140    pub fn get_nodes_mut(&mut self) -> Vec<&mut NODE> {
141        self.graph.vertices.iter_mut().map(|v| &mut v.node).collect()
142    }
143}