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