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