swh_graph/views/
transposed.rs1use std::collections::HashMap;
7use std::path::Path;
8
9use anyhow::Result;
10
11use crate::graph::*;
12use crate::properties;
13use crate::NodeType;
14
15pub 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_arc(&self, src_node_id: NodeId, dst_node_id: NodeId) -> bool {
40 self.0.has_arc(dst_node_id, src_node_id)
41 }
42}
43
44impl<G: SwhBackwardGraph> SwhForwardGraph for Transposed<G> {
45 type Successors<'succ>
46 = <G as SwhBackwardGraph>::Predecessors<'succ>
47 where
48 Self: 'succ;
49
50 fn successors(&self, node_id: NodeId) -> Self::Successors<'_> {
51 self.0.predecessors(node_id)
52 }
53 fn outdegree(&self, node_id: NodeId) -> usize {
54 self.0.indegree(node_id)
55 }
56}
57
58impl<G: SwhLabeledBackwardGraph> SwhLabeledForwardGraph for Transposed<G> {
59 type LabeledArcs<'arc>
60 = <G as SwhLabeledBackwardGraph>::LabeledArcs<'arc>
61 where
62 Self: 'arc;
63 type LabeledSuccessors<'succ>
64 = <G as SwhLabeledBackwardGraph>::LabeledPredecessors<'succ>
65 where
66 Self: 'succ;
67
68 fn untyped_labeled_successors(&self, node_id: NodeId) -> Self::LabeledSuccessors<'_> {
69 self.0.untyped_labeled_predecessors(node_id)
70 }
71}
72
73impl<G: SwhForwardGraph> SwhBackwardGraph for Transposed<G> {
74 type Predecessors<'succ>
75 = <G as SwhForwardGraph>::Successors<'succ>
76 where
77 Self: 'succ;
78
79 fn predecessors(&self, node_id: NodeId) -> Self::Predecessors<'_> {
80 self.0.successors(node_id)
81 }
82 fn indegree(&self, node_id: NodeId) -> usize {
83 self.0.outdegree(node_id)
84 }
85}
86
87impl<G: SwhLabeledForwardGraph> SwhLabeledBackwardGraph for Transposed<G> {
88 type LabeledArcs<'arc>
89 = <G as SwhLabeledForwardGraph>::LabeledArcs<'arc>
90 where
91 Self: 'arc;
92 type LabeledPredecessors<'succ>
93 = <G as SwhLabeledForwardGraph>::LabeledSuccessors<'succ>
94 where
95 Self: 'succ;
96
97 fn untyped_labeled_predecessors(&self, node_id: NodeId) -> Self::LabeledPredecessors<'_> {
98 self.0.untyped_labeled_successors(node_id)
99 }
100}
101
102impl<G: SwhGraphWithProperties> SwhGraphWithProperties for Transposed<G> {
103 type Maps = <G as SwhGraphWithProperties>::Maps;
104 type Timestamps = <G as SwhGraphWithProperties>::Timestamps;
105 type Persons = <G as SwhGraphWithProperties>::Persons;
106 type Contents = <G as SwhGraphWithProperties>::Contents;
107 type Strings = <G as SwhGraphWithProperties>::Strings;
108 type LabelNames = <G as SwhGraphWithProperties>::LabelNames;
109
110 fn properties(
111 &self,
112 ) -> &properties::SwhGraphProperties<
113 Self::Maps,
114 Self::Timestamps,
115 Self::Persons,
116 Self::Contents,
117 Self::Strings,
118 Self::LabelNames,
119 > {
120 self.0.properties()
121 }
122}