Skip to main content

swh_graph/views/
transposed.rs

1// Copyright (C) 2023-2026  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
16#[derive(Clone, Debug)]
17pub struct Transposed<G: SwhGraph>(pub G);
18
19impl<G: SwhGraph> SwhGraph for Transposed<G> {
20    #[inline(always)]
21    fn path(&self) -> &Path {
22        self.0.path()
23    }
24    #[inline(always)]
25    fn is_transposed(&self) -> bool {
26        !self.0.is_transposed()
27    }
28    #[inline(always)]
29    fn num_nodes(&self) -> usize {
30        self.0.num_nodes()
31    }
32    #[inline(always)]
33    fn num_arcs(&self) -> u64 {
34        self.0.num_arcs()
35    }
36    fn num_arcs_by_type(&self) -> Result<HashMap<(NodeType, NodeType), usize>> {
37        Ok(self
38            .0
39            .num_arcs_by_type()?
40            .into_iter()
41            .map(|((src_type, dst_type), count)| ((dst_type, src_type), count))
42            .collect())
43    }
44    #[inline(always)]
45    fn has_node(&self, node_id: NodeId) -> bool {
46        self.0.has_node(node_id)
47    }
48    #[inline(always)]
49    fn has_arc(&self, src_node_id: NodeId, dst_node_id: NodeId) -> bool {
50        self.0.has_arc(dst_node_id, src_node_id)
51    }
52}
53
54impl<G: SwhBackwardGraph> SwhForwardGraph for Transposed<G> {
55    type Successors<'succ>
56        = <G as SwhBackwardGraph>::Predecessors<'succ>
57    where
58        Self: 'succ;
59
60    #[inline(always)]
61    fn successors(&self, node_id: NodeId) -> Self::Successors<'_> {
62        self.0.predecessors(node_id)
63    }
64    #[inline(always)]
65    fn outdegree(&self, node_id: NodeId) -> usize {
66        self.0.indegree(node_id)
67    }
68}
69
70impl<G: SwhLabeledBackwardGraph> SwhLabeledForwardGraph for Transposed<G> {
71    type LabeledArcs<'arc>
72        = <G as SwhLabeledBackwardGraph>::LabeledArcs<'arc>
73    where
74        Self: 'arc;
75    type LabeledSuccessors<'succ>
76        = <G as SwhLabeledBackwardGraph>::LabeledPredecessors<'succ>
77    where
78        Self: 'succ;
79
80    #[inline(always)]
81    fn untyped_labeled_successors(&self, node_id: NodeId) -> Self::LabeledSuccessors<'_> {
82        self.0.untyped_labeled_predecessors(node_id)
83    }
84}
85
86impl<G: SwhForwardGraph> SwhBackwardGraph for Transposed<G> {
87    type Predecessors<'succ>
88        = <G as SwhForwardGraph>::Successors<'succ>
89    where
90        Self: 'succ;
91
92    #[inline(always)]
93    fn predecessors(&self, node_id: NodeId) -> Self::Predecessors<'_> {
94        self.0.successors(node_id)
95    }
96    #[inline(always)]
97    fn indegree(&self, node_id: NodeId) -> usize {
98        self.0.outdegree(node_id)
99    }
100}
101
102impl<G: SwhLabeledForwardGraph> SwhLabeledBackwardGraph for Transposed<G> {
103    type LabeledArcs<'arc>
104        = <G as SwhLabeledForwardGraph>::LabeledArcs<'arc>
105    where
106        Self: 'arc;
107    type LabeledPredecessors<'succ>
108        = <G as SwhLabeledForwardGraph>::LabeledSuccessors<'succ>
109    where
110        Self: 'succ;
111
112    #[inline(always)]
113    fn untyped_labeled_predecessors(&self, node_id: NodeId) -> Self::LabeledPredecessors<'_> {
114        self.0.untyped_labeled_successors(node_id)
115    }
116}
117
118impl<G: SwhGraphWithProperties> SwhGraphWithProperties for Transposed<G> {
119    type Maps = <G as SwhGraphWithProperties>::Maps;
120    type Timestamps = <G as SwhGraphWithProperties>::Timestamps;
121    type Persons = <G as SwhGraphWithProperties>::Persons;
122    type Contents = <G as SwhGraphWithProperties>::Contents;
123    type Strings = <G as SwhGraphWithProperties>::Strings;
124    type LabelNames = <G as SwhGraphWithProperties>::LabelNames;
125
126    #[inline(always)]
127    fn properties(
128        &self,
129    ) -> &properties::SwhGraphProperties<
130        Self::Maps,
131        Self::Timestamps,
132        Self::Persons,
133        Self::Contents,
134        Self::Strings,
135        Self::LabelNames,
136    > {
137        self.0.properties()
138    }
139}