Skip to main content

swh_graph/views/contiguous_subgraph/
maps.rs

1// Copyright (C) 2025-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
6//! Implementation of [`properties::Maps`] for [`ContiguousSubgraph`]
7
8use super::*;
9
10impl<
11        G: SwhGraphWithProperties<Maps: properties::Maps>,
12        N: ContractionBackend,
13        TIMESTAMPS: properties::MaybeTimestamps,
14        PERSONS: properties::MaybePersons,
15        CONTENTS: properties::MaybeContents,
16        STRINGS: properties::MaybeStrings,
17        LABELNAMES: properties::MaybeLabelNames,
18    >
19    ContiguousSubgraph<G, N, properties::NoMaps, TIMESTAMPS, PERSONS, CONTENTS, STRINGS, LABELNAMES>
20{
21    /// Makes [`properties::Maps`] available on this [`ContiguousSubgraph`].
22    ///
23    /// Requires the underlying graph to implement [`properties::Maps`].
24    pub fn with_maps(
25        self,
26    ) -> ContiguousSubgraph<
27        G,
28        N,
29        ContiguousSubgraphMaps<G, N>,
30        TIMESTAMPS,
31        PERSONS,
32        CONTENTS,
33        STRINGS,
34        LABELNAMES,
35    > {
36        let ContiguousSubgraph {
37            properties:
38                properties::SwhGraphProperties {
39                    path,
40                    num_nodes,
41                    maps: properties::NoMaps,
42                    timestamps,
43                    persons,
44                    contents,
45                    strings,
46                    label_names,
47                    label_names_are_in_base64_order,
48                },
49            inner,
50        } = self;
51
52        ContiguousSubgraph {
53            properties: properties::SwhGraphProperties {
54                path,
55                num_nodes,
56                maps: ContiguousSubgraphMaps {
57                    graph: Arc::clone(&inner),
58                    mphf: ContiguousSubgraphMphf {
59                        graph: Arc::clone(&inner),
60                    },
61                },
62                timestamps,
63                persons,
64                contents,
65                strings,
66                label_names,
67                label_names_are_in_base64_order,
68            },
69            inner,
70        }
71    }
72}
73
74/// View for [`MaybeMaps`] that renumbers nodes, as part of [`ContiguousSubgraph`
75pub struct ContiguousSubgraphMaps<
76    G: SwhGraphWithProperties<Maps: properties::Maps>,
77    N: ContractionBackend,
78> {
79    graph: Arc<ContiguousSubgraphInner<G, N>>,
80    mphf: ContiguousSubgraphMphf<G, N>,
81}
82
83impl<G: SwhGraphWithProperties<Maps: properties::Maps>, N: ContractionBackend> properties::Maps
84    for ContiguousSubgraphMaps<G, N>
85{
86    type MPHF = ContiguousSubgraphMphf<G, N>;
87
88    #[inline(always)]
89    fn mphf(&self) -> &Self::MPHF {
90        &self.mphf
91    }
92    #[inline(always)]
93    fn node2swhid(&self, node: NodeId) -> Result<SWHID, OutOfBoundError> {
94        self.graph
95            .underlying_graph
96            .properties()
97            .maps
98            .node2swhid(self.graph.contraction.underlying_node_id(node))
99    }
100    #[inline(always)]
101    fn node2type(&self, node: NodeId) -> Result<NodeType, OutOfBoundError> {
102        self.graph
103            .underlying_graph
104            .properties()
105            .maps
106            .node2type(self.graph.contraction.underlying_node_id(node))
107    }
108}
109
110/// View for [`SwhidMphf`] that renumbers nodes, as part of [`ContiguousSubgraph`]
111pub struct ContiguousSubgraphMphf<
112    G: SwhGraphWithProperties<Maps: properties::Maps>,
113    N: ContractionBackend,
114> {
115    graph: Arc<ContiguousSubgraphInner<G, N>>,
116}
117
118impl<G: SwhGraphWithProperties<Maps: properties::Maps>, N: ContractionBackend> SwhidMphf
119    for ContiguousSubgraphMphf<G, N>
120{
121    #[inline(always)]
122    fn num_keys(&self) -> usize {
123        self.graph.contraction.num_nodes()
124    }
125
126    #[inline(always)]
127    fn hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
128        use properties::Maps;
129
130        self.graph
131            .underlying_graph
132            .properties()
133            .maps
134            .mphf()
135            .hash_array(swhid)
136            .and_then(|underlying_node| {
137                self.graph
138                    .contraction
139                    .node_id_from_underlying(underlying_node)
140            })
141    }
142
143    /// Hashes a SWHID's textual representation
144    #[inline(always)]
145    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
146        use properties::Maps;
147
148        self.graph
149            .underlying_graph
150            .properties()
151            .maps
152            .mphf()
153            .hash_str(swhid)
154            .and_then(|underlying_node| {
155                self.graph
156                    .contraction
157                    .node_id_from_underlying(underlying_node)
158            })
159    }
160
161    /// Hashes a SWHID's textual representation
162    #[inline(always)]
163    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
164        use properties::Maps;
165
166        self.graph
167            .underlying_graph
168            .properties()
169            .maps
170            .mphf()
171            .hash_str_array(swhid)
172            .and_then(|underlying_node| {
173                self.graph
174                    .contraction
175                    .node_id_from_underlying(underlying_node)
176            })
177    }
178
179    /// Hashes a [`SWHID`]
180    #[inline(always)]
181    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
182        use properties::Maps;
183
184        self.graph
185            .underlying_graph
186            .properties()
187            .maps
188            .mphf()
189            .hash_swhid(swhid)
190            .and_then(|underlying_node| {
191                self.graph
192                    .contraction
193                    .node_id_from_underlying(underlying_node)
194            })
195    }
196}