swh_graph/views/contiguous_subgraph/
maps.rs

1// Copyright (C) 2025  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 hash_array(&self, swhid: &[u8; SWHID::BYTES_SIZE]) -> Option<NodeId> {
123        use properties::Maps;
124
125        self.graph
126            .underlying_graph
127            .properties()
128            .maps
129            .mphf()
130            .hash_array(swhid)
131            .and_then(|underlying_node| {
132                self.graph
133                    .contraction
134                    .node_id_from_underlying(underlying_node)
135            })
136    }
137
138    /// Hashes a SWHID's textual representation
139    #[inline(always)]
140    fn hash_str(&self, swhid: impl AsRef<str>) -> Option<NodeId> {
141        use properties::Maps;
142
143        self.graph
144            .underlying_graph
145            .properties()
146            .maps
147            .mphf()
148            .hash_str(swhid)
149            .and_then(|underlying_node| {
150                self.graph
151                    .contraction
152                    .node_id_from_underlying(underlying_node)
153            })
154    }
155
156    /// Hashes a SWHID's textual representation
157    #[inline(always)]
158    fn hash_str_array(&self, swhid: &[u8; 50]) -> Option<NodeId> {
159        use properties::Maps;
160
161        self.graph
162            .underlying_graph
163            .properties()
164            .maps
165            .mphf()
166            .hash_str_array(swhid)
167            .and_then(|underlying_node| {
168                self.graph
169                    .contraction
170                    .node_id_from_underlying(underlying_node)
171            })
172    }
173
174    /// Hashes a [`SWHID`]
175    #[inline(always)]
176    fn hash_swhid(&self, swhid: &SWHID) -> Option<NodeId> {
177        use properties::Maps;
178
179        self.graph
180            .underlying_graph
181            .properties()
182            .maps
183            .mphf()
184            .hash_swhid(swhid)
185            .and_then(|underlying_node| {
186                self.graph
187                    .contraction
188                    .node_id_from_underlying(underlying_node)
189            })
190    }
191}