swh-graph-stdlib 13.0.0

Library of algorithms and data structures for swh-graph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (C) 2026  The Software Heritage developers
// See the AUTHORS file at the top-level directory of this distribution
// License: GNU General Public License version 3, or any later version
// See top-level LICENSE file for more information

//! Associate information to a subset of nodes

use std::borrow::{Borrow, BorrowMut};
use std::ops::Range;

use bytemuck::{AnyBitPattern, TransparentWrapper};
use rapidhash::RapidHashMap;
use rayon::prelude::*;
use sux::bits::BitVec;
use sux::traits::{BitVecOps, BitVecOpsMut};

use crate::NodeId;

pub trait Labels {
    type Label: ToOwned + ?Sized;
    type Config;

    fn new(num_nodes: usize, config: Self::Config) -> Self;
    fn insert(
        &mut self,
        node: NodeId,
        label: <Self::Label as ToOwned>::Owned,
    ) -> Option<<Self::Label as ToOwned>::Owned>;
    fn get(&self, node: NodeId) -> Option<&Self::Label>;
    fn contains_key(&self, node: NodeId) -> bool;
    fn remove(&mut self, node: NodeId) -> Option<<Self::Label as ToOwned>::Owned>;
    fn is_empty(&self) -> bool;
}

pub struct SparseLabels<Label: ToOwned + Sized> {
    labels: RapidHashMap<NodeId, <Label as ToOwned>::Owned>,
}

impl<Label: ToOwned> Labels for SparseLabels<Label> {
    type Label = Label;
    type Config = ();

    fn new(_num_nodes: usize, _config: Self::Config) -> Self {
        Self {
            labels: RapidHashMap::default(),
        }
    }
    fn insert(
        &mut self,
        node: NodeId,
        label: <Self::Label as ToOwned>::Owned,
    ) -> Option<<Self::Label as ToOwned>::Owned> {
        self.labels.insert(node, label)
    }
    fn get(&self, node: NodeId) -> Option<&Self::Label> {
        self.labels.get(&node).map(Borrow::borrow)
    }
    fn contains_key(&self, node: NodeId) -> bool {
        self.labels.contains_key(&node)
    }
    fn remove(&mut self, node: NodeId) -> Option<<Self::Label as ToOwned>::Owned> {
        self.labels.remove(&node)
    }
    fn is_empty(&self) -> bool {
        self.labels.is_empty()
    }
}

impl<Label: ToOwned> SparseLabels<Label> {
    /// Returns an iterator with each labeled node
    pub fn iter_labeled(&self) -> impl Iterator<Item = (NodeId, &Label)> {
        self.labels
            .iter()
            .map(|(&node, label)| (node, label.borrow()))
    }

    /// Returns a parallel iterator with each labeled node
    pub fn into_iter_labeled(self) -> impl Iterator<Item = (NodeId, <Label as ToOwned>::Owned)> {
        self.labels.into_iter()
    }
}

pub struct DenseLabels<Label: Sized> {
    labels: Box<[Label]>,
    /// which labels are actually set, and which are the default value
    is_set: BitVec,
    /// number of labels actually set
    len: usize,
}

impl<Label: Default + Clone> Labels for DenseLabels<Label> {
    type Label = Label;
    type Config = ();

    fn new(num_nodes: usize, _config: Self::Config) -> Self {
        Self {
            labels: vec![Label::default(); num_nodes].into(),
            is_set: BitVec::new(num_nodes),
            len: 0,
        }
    }
    fn insert(
        &mut self,
        node: NodeId,
        mut label: <Self::Label as ToOwned>::Owned,
    ) -> Option<Self::Label> {
        let was_set = self.contains_key(node);
        std::mem::swap(&mut self.labels[node], &mut label);
        self.is_set.set(node, true);
        if was_set {
            Some(label)
        } else {
            self.len += 1;
            None
        }
    }
    fn get(&self, node: NodeId) -> Option<&Self::Label> {
        if self.contains_key(node) {
            Some(&self.labels[node])
        } else {
            None
        }
    }
    fn contains_key(&self, node: NodeId) -> bool {
        self.is_set.get(node)
    }
    fn remove(&mut self, node: NodeId) -> Option<Self::Label> {
        if self.contains_key(node) {
            self.len -= 1;
            self.is_set.set(node, false);
            let mut label = Label::default();
            std::mem::swap(self.labels.get_mut(node)?, &mut label);
            Some(label)
        } else {
            None
        }
    }
    fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl<Label: Default + Clone> DenseLabels<Label> {
    /// Returns an iterator with the label of each node, if any
    pub fn iter(&self) -> impl Iterator<Item = Option<&Label>> {
        self.is_set
            .iter()
            .zip(self.labels.iter())
            .map(|(is_set, value)| if is_set { Some(value) } else { None })
    }

    /// Returns an iterator with each labeled node
    pub fn iter_labeled(&self) -> impl Iterator<Item = (NodeId, &Label)> {
        self.is_set
            .iter()
            .zip(self.labels.iter())
            .enumerate()
            .filter_map(|(node, (is_set, value))| if is_set { Some((node, value)) } else { None })
    }

    /// Returns a parallel iterator with the label of each node, if any
    pub fn par_iter(&self) -> impl ParallelIterator<Item = Option<&Label>>
    where
        Label: Sync,
    {
        self.labels
            .par_iter()
            .enumerate()
            .map(move |(node, value)| {
                if self.is_set.get(node) {
                    Some(value)
                } else {
                    None
                }
            })
    }

    /// Returns a parallel iterator with each labeled node
    pub fn par_iter_labeled(&self) -> impl ParallelIterator<Item = (NodeId, &Label)>
    where
        Label: Sync,
    {
        self.labels
            .par_iter()
            .enumerate()
            .filter_map(move |(node, value)| {
                if self.is_set.get(node) {
                    Some((node, value))
                } else {
                    None
                }
            })
    }

    /// Returns a parallel iterator with the label of each node, if any
    pub fn into_par_iter(self) -> impl ParallelIterator<Item = Option<Label>>
    where
        Label: Send,
    {
        let Self { labels, is_set, .. } = self;
        Vec::from(labels)
            .into_par_iter()
            .enumerate()
            .map(
                move |(node, value)| {
                    if is_set.get(node) { Some(value) } else { None }
                },
            )
    }

    /// Returns a parallel iterator with each labeled node
    pub fn into_par_iter_labeled(&self) -> impl ParallelIterator<Item = (NodeId, &Label)>
    where
        Label: Sync,
    {
        let Self { labels, is_set, .. } = self;
        labels
            .into_par_iter()
            .enumerate()
            .filter_map(move |(node, value)| {
                if is_set.get(node) {
                    Some((node, value))
                } else {
                    None
                }
            })
    }
}

/// A [`Label`](super::MapReducer::Label) that is a view over a slice of an array
pub trait StridableLabel: ToOwned<Owned: BorrowMut<Self>> {
    type Word: Default + Copy;

    fn from_stride(stride: &[Self::Word]) -> &Self;
    fn swap_with_stride(&mut self, stride: &mut [Self::Word]);
}

/// Implementation of [`StridableLabel`] that simply wraps a slice.
#[derive(Debug, PartialEq, Eq, TransparentWrapper, derive_more::AsRef, derive_more::AsMut)]
#[repr(transparent)]
pub struct SliceLabel<Word: AnyBitPattern>(pub [Word]);

impl<Word: AnyBitPattern> ToOwned for SliceLabel<Word> {
    type Owned = BoxLabel<Word>;

    fn to_owned(&self) -> Self::Owned {
        BoxLabel(self.0.to_owned().into())
    }
}

/// Helper for [`SliceLabel`]
#[derive(Debug, PartialEq, Eq, Default, TransparentWrapper)]
#[repr(transparent)]
pub struct BoxLabel<Word: AnyBitPattern>(pub Box<[Word]>);

impl<Word: AnyBitPattern> Borrow<SliceLabel<Word>> for BoxLabel<Word> {
    fn borrow(&self) -> &SliceLabel<Word> {
        SliceLabel::wrap_ref(Self::peel_ref(self))
    }
}

impl<Word: AnyBitPattern> BorrowMut<SliceLabel<Word>> for BoxLabel<Word> {
    fn borrow_mut(&mut self) -> &mut SliceLabel<Word> {
        SliceLabel::wrap_mut(Self::peel_mut(self))
    }
}

impl<Word: AnyBitPattern + Default> StridableLabel for SliceLabel<Word> {
    type Word = Word;

    fn from_stride(stride: &[Self::Word]) -> &Self {
        Self::wrap_ref(stride)
    }
    fn swap_with_stride(&mut self, stride: &mut [Self::Word]) {
        stride.swap_with_slice(TransparentWrapper::peel_mut(self))
    }
}

pub struct StriddenLabelsConfig {
    pub num_words: usize,
}

pub struct StriddenLabels<Label: StridableLabel + ?Sized> {
    labels: Box<[Label::Word]>,
    /// How many `Label::Word`s are in a label
    num_words: usize,
    /// which labels are actually set, and which are the default value
    is_set: BitVec,
    /// number of labels actually set
    len: usize,
}

impl<Label: StridableLabel + ?Sized> Labels for StriddenLabels<Label> {
    type Label = Label;
    type Config = StriddenLabelsConfig;

    fn new(num_nodes: usize, StriddenLabelsConfig { num_words }: Self::Config) -> Self {
        Self {
            labels: vec![Label::Word::default(); num_nodes * num_words].into(),
            num_words,
            is_set: BitVec::new(num_nodes),
            len: 0,
        }
    }
    fn insert(
        &mut self,
        node: NodeId,
        mut label: <Self::Label as ToOwned>::Owned,
    ) -> Option<<Self::Label as ToOwned>::Owned> {
        let was_set = self.contains_key(node);
        let range = self.stride_range(node);
        label.borrow_mut().swap_with_stride(&mut self.labels[range]);
        self.is_set.set(node, true);
        if was_set {
            Some(label)
        } else {
            self.len += 1;
            None
        }
    }
    fn get(&self, node: NodeId) -> Option<&Self::Label> {
        if self.contains_key(node) {
            Some(Self::Label::from_stride(
                &self.labels[self.stride_range(node)],
            ))
        } else {
            None
        }
    }
    fn contains_key(&self, node: NodeId) -> bool {
        self.is_set.get(node)
    }
    fn remove(&mut self, node: NodeId) -> Option<<Self::Label as ToOwned>::Owned> {
        if self.contains_key(node) {
            self.len -= 1;
            self.is_set.set(node, false);
            let range = self.stride_range(node);
            let label = Label::from_stride(&self.labels[range.clone()]).to_owned();
            self.labels[range].fill(Label::Word::default());
            Some(label)
        } else {
            None
        }
    }
    fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl<Label: StridableLabel + ?Sized> StriddenLabels<Label> {
    fn stride_range(&self, node: NodeId) -> Range<usize> {
        (node * self.num_words)..(node + 1) * self.num_words
    }
}

impl<Label: StridableLabel + ?Sized> StriddenLabels<Label> {
    /// Returns an iterator with the label of each node, if any
    pub fn iter(&self) -> impl Iterator<Item = Option<&Label>> {
        self.is_set
            .iter()
            .zip(self.labels.chunks(self.num_words))
            .map(|(is_set, value)| {
                if is_set {
                    Some(Label::from_stride(value))
                } else {
                    None
                }
            })
    }

    /// Returns an iterator with each labeled node
    pub fn iter_labeled(&self) -> impl Iterator<Item = (NodeId, &Label)> {
        self.is_set
            .iter()
            .zip(self.labels.chunks(self.num_words))
            .enumerate()
            .filter_map(|(node, (is_set, value))| {
                if is_set {
                    Some((node, Label::from_stride(value)))
                } else {
                    None
                }
            })
    }

    /// Returns a parallel iterator with the label of each node, if any
    pub fn par_iter(&self) -> impl ParallelIterator<Item = Option<&Label>>
    where
        Label: StridableLabel<Word: Sync> + Sync,
    {
        self.labels
            .par_chunks(self.num_words)
            .enumerate()
            .map(move |(node, value)| {
                if self.is_set.get(node) {
                    Some(Label::from_stride(value))
                } else {
                    None
                }
            })
    }

    /// Returns a parallel iterator with each labeled node
    pub fn par_iter_labeled(&self) -> impl ParallelIterator<Item = (NodeId, &Label)>
    where
        Label: StridableLabel<Word: Sync> + Sync,
    {
        self.labels
            .par_chunks(self.num_words)
            .enumerate()
            .filter_map(move |(node, value)| {
                if self.is_set.get(node) {
                    Some((node, Label::from_stride(value)))
                } else {
                    None
                }
            })
    }
}