swh_graph/views/
transposed.rs

1// Copyright (C) 2023  The Software Heritage developers
2// See the AUTHORS file at the top-level directory of this distribution
3// License: GNU General Public License version 3, or any later version
4// See top-level LICENSE file for more information
5
6use std::collections::HashMap;
7use std::path::Path;
8
9use anyhow::Result;
10
11use crate::graph::*;
12use crate::properties;
13use crate::NodeType;
14
15/// A view over [`SwhGraph`] and related trait, that flips the direction of all arcs
16pub struct Transposed<G: SwhGraph>(pub G);
17
18impl<G: SwhGraph> SwhGraph for Transposed<G> {
19    fn path(&self) -> &Path {
20        self.0.path()
21    }
22    fn is_transposed(&self) -> bool {
23        !self.0.is_transposed()
24    }
25    fn num_nodes(&self) -> usize {
26        self.0.num_nodes()
27    }
28    fn num_arcs(&self) -> u64 {
29        self.0.num_arcs()
30    }
31    fn num_arcs_by_type(&self) -> Result<HashMap<(NodeType, NodeType), usize>> {
32        Ok(self
33            .0
34            .num_arcs_by_type()?
35            .into_iter()
36            .map(|((src_type, dst_type), count)| ((dst_type, src_type), count))
37            .collect())
38    }
39    fn has_node(&self, node_id: NodeId) -> bool {
40        self.0.has_node(node_id)
41    }
42    fn has_arc(&self, src_node_id: NodeId, dst_node_id: NodeId) -> bool {
43        self.0.has_arc(dst_node_id, src_node_id)
44    }
45}
46
47impl<G: SwhBackwardGraph> SwhForwardGraph for Transposed<G> {
48    type Successors<'succ>
49        = <G as SwhBackwardGraph>::Predecessors<'succ>
50    where
51        Self: 'succ;
52
53    fn successors(&self, node_id: NodeId) -> Self::Successors<'_> {
54        self.0.predecessors(node_id)
55    }
56    fn outdegree(&self, node_id: NodeId) -> usize {
57        self.0.indegree(node_id)
58    }
59}
60
61impl<G: SwhLabeledBackwardGraph> SwhLabeledForwardGraph for Transposed<G> {
62    type LabeledArcs<'arc>
63        = <G as SwhLabeledBackwardGraph>::LabeledArcs<'arc>
64    where
65        Self: 'arc;
66    type LabeledSuccessors<'succ>
67        = <G as SwhLabeledBackwardGraph>::LabeledPredecessors<'succ>
68    where
69        Self: 'succ;
70
71    fn untyped_labeled_successors(&self, node_id: NodeId) -> Self::LabeledSuccessors<'_> {
72        self.0.untyped_labeled_predecessors(node_id)
73    }
74}
75
76impl<G: SwhForwardGraph> SwhBackwardGraph for Transposed<G> {
77    type Predecessors<'succ>
78        = <G as SwhForwardGraph>::Successors<'succ>
79    where
80        Self: 'succ;
81
82    fn predecessors(&self, node_id: NodeId) -> Self::Predecessors<'_> {
83        self.0.successors(node_id)
84    }
85    fn indegree(&self, node_id: NodeId) -> usize {
86        self.0.outdegree(node_id)
87    }
88}
89
90impl<G: SwhLabeledForwardGraph> SwhLabeledBackwardGraph for Transposed<G> {
91    type LabeledArcs<'arc>
92        = <G as SwhLabeledForwardGraph>::LabeledArcs<'arc>
93    where
94        Self: 'arc;
95    type LabeledPredecessors<'succ>
96        = <G as SwhLabeledForwardGraph>::LabeledSuccessors<'succ>
97    where
98        Self: 'succ;
99
100    fn untyped_labeled_predecessors(&self, node_id: NodeId) -> Self::LabeledPredecessors<'_> {
101        self.0.untyped_labeled_successors(node_id)
102    }
103}
104
105impl<G: SwhGraphWithProperties> SwhGraphWithProperties for Transposed<G> {
106    type Maps = <G as SwhGraphWithProperties>::Maps;
107    type Timestamps = <G as SwhGraphWithProperties>::Timestamps;
108    type Persons = <G as SwhGraphWithProperties>::Persons;
109    type Contents = <G as SwhGraphWithProperties>::Contents;
110    type Strings = <G as SwhGraphWithProperties>::Strings;
111    type LabelNames = <G as SwhGraphWithProperties>::LabelNames;
112
113    fn properties(
114        &self,
115    ) -> &properties::SwhGraphProperties<
116        Self::Maps,
117        Self::Timestamps,
118        Self::Persons,
119        Self::Contents,
120        Self::Strings,
121        Self::LabelNames,
122    > {
123        self.0.properties()
124    }
125}