webgraph 0.6.1

A Rust port of the WebGraph framework (http://webgraph.di.unimi.it/).
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

/*!

Left and right projections.

The two structures in this module, [`Left`] and [`Right`], provide
projection of a labeling whose labels are pairs. In particular,
`Left(Zip(g,h))` is the same labeling as `g` and
`Right(Zip(g,h))` is the same labeling as `h`.

*/
use crate::prelude::{
    LenderIntoIterator, LenderLabel, NodeLabelsLender, Pair, RandomAccessGraph,
    RandomAccessLabeling, SequentialGraph, SequentialLabeling, SortedIterator, SortedLender,
};
use crate::traits::SplitLabeling;
use lender::{ExactSizeLender, IntoLender, Lend, Lender, Lending, unsafe_assume_covariance};

/// The projection onto the first component of a pair.
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct Left<S: SequentialLabeling>(pub S)
where
    S::Label: Pair;

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct LeftIterator<L>(pub L);

impl<'succ, L> NodeLabelsLender<'succ> for LeftIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    type Label = <LenderLabel<'succ, L> as Pair>::Left;
    type IntoIterator = IntoLeftSucc<<L as NodeLabelsLender<'succ>>::IntoIterator>;
}

impl<'succ, L> Lending<'succ> for LeftIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    type Lend = (usize, LenderIntoIterator<'succ, Self>);
}

impl<L: ExactSizeLender> ExactSizeLender for LeftIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }

    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct IntoLeftSucc<I: IntoIterator>(pub I)
where
    I::Item: Pair;

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct LeftSucc<I: Iterator>(pub I)
where
    I::Item: Pair;

impl<I: Iterator> Iterator for LeftSucc<I>
where
    I::Item: Pair,
{
    type Item = <I::Item as Pair>::Left;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| x.into_pair().0)
    }

    #[inline(always)]
    fn count(self) -> usize {
        self.0.count()
    }
}

impl<I: ExactSizeIterator> ExactSizeIterator for LeftSucc<I>
where
    I::Item: Pair,
{
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<I: DoubleEndedIterator> DoubleEndedIterator for LeftSucc<I>
where
    I::Item: Pair,
{
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| x.into_pair().0)
    }

    #[inline(always)]
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth_back(n).map(|x| x.into_pair().0)
    }
}

impl<I: IntoIterator> IntoIterator for IntoLeftSucc<I>
where
    I::Item: Pair,
{
    type Item = <I::Item as Pair>::Left;
    type IntoIter = LeftSucc<I::IntoIter>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        LeftSucc(self.0.into_iter())
    }
}

impl<L> Lender for LeftIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    // SAFETY: the lend is covariant as it projects the left component from the
    // underlying covariant lender L.
    unsafe_assume_covariance!();

    #[inline(always)]
    fn next(&mut self) -> Option<Lend<'_, Self>> {
        self.0.next().map(|x| {
            let (node, succ) = x.into_pair();
            (node, IntoLeftSucc(succ))
        })
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<'a, S: SequentialLabeling> IntoLender for &'a Left<S>
where
    S::Label: Pair,
{
    type Lender = <Left<S> as SequentialLabeling>::Lender<'a>;

    #[inline(always)]
    fn into_lender(self) -> Self::Lender {
        self.iter()
    }
}

impl<G> SplitLabeling for Left<G>
where
    G: SequentialLabeling + SplitLabeling,
    G::Label: Pair,
{
    type SplitLender<'a>
        = LeftIterator<G::SplitLender<'a>>
    where
        Self: 'a;
    type IntoIterator<'a>
        = core::iter::Map<
        <G::IntoIterator<'a> as IntoIterator>::IntoIter,
        fn(G::SplitLender<'a>) -> Self::SplitLender<'a>,
    >
    where
        Self: 'a;

    fn split_iter(&self, how_many: usize) -> Self::IntoIterator<'_> {
        self.0.split_iter(how_many).into_iter().map(LeftIterator)
    }
}

impl<S: SequentialLabeling> SequentialLabeling for Left<S>
where
    S::Label: Pair,
{
    type Label = <S::Label as Pair>::Left;

    type Lender<'node>
        = LeftIterator<S::Lender<'node>>
    where
        Self: 'node;

    #[inline(always)]
    fn num_nodes(&self) -> usize {
        self.0.num_nodes()
    }

    #[inline(always)]
    fn iter_from(&self, from: usize) -> Self::Lender<'_> {
        LeftIterator(self.0.iter_from(from))
    }

    #[inline(always)]
    fn num_arcs_hint(&self) -> Option<u64> {
        self.0.num_arcs_hint()
    }
}

impl<R: RandomAccessLabeling> RandomAccessLabeling for Left<R>
where
    R::Label: Pair,
{
    type Labels<'succ>
        = IntoLeftSucc<<R as RandomAccessLabeling>::Labels<'succ>>
    where
        Self: 'succ;

    #[inline(always)]
    fn num_arcs(&self) -> u64 {
        self.0.num_arcs()
    }

    #[inline(always)]
    fn labels(&self, node_id: usize) -> <Self as RandomAccessLabeling>::Labels<'_> {
        IntoLeftSucc(self.0.labels(node_id))
    }

    #[inline(always)]
    fn outdegree(&self, _node_id: usize) -> usize {
        self.0.outdegree(_node_id)
    }
}

impl<S: SequentialLabeling> SequentialGraph for Left<S> where S::Label: Pair<Left = usize> {}

impl<R: RandomAccessLabeling> RandomAccessGraph for Left<R> where R::Label: Pair<Left = usize> {}

unsafe impl<L: SortedLender> SortedLender for LeftIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
}

unsafe impl<I: SortedIterator> SortedIterator for LeftSucc<I> where I::Item: Pair {}

/// The projection onto the second component of a pair.
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct Right<S: SequentialLabeling>(pub S)
where
    S::Label: Pair;

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct RightIterator<L>(pub L);

impl<'succ, L> NodeLabelsLender<'succ> for RightIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    type Label = <LenderLabel<'succ, L> as Pair>::Right;
    type IntoIterator = IntoRightSucc<<L as NodeLabelsLender<'succ>>::IntoIterator>;
}

impl<'succ, L> Lending<'succ> for RightIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    type Lend = (usize, LenderIntoIterator<'succ, Self>);
}

impl<L: ExactSizeLender> ExactSizeLender for RightIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }

    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct IntoRightSucc<I: IntoIterator>(pub I)
where
    I::Item: Pair;

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct RightSucc<I: Iterator>(pub I)
where
    I::Item: Pair;

impl<I: Iterator> Iterator for RightSucc<I>
where
    I::Item: Pair,
{
    type Item = <I::Item as Pair>::Right;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|x| x.into_pair().1)
    }

    #[inline(always)]
    fn count(self) -> usize {
        self.0.count()
    }
}

impl<I: ExactSizeIterator> ExactSizeIterator for RightSucc<I>
where
    I::Item: Pair,
{
    #[inline(always)]
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<I: DoubleEndedIterator> DoubleEndedIterator for RightSucc<I>
where
    I::Item: Pair,
{
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back().map(|x| x.into_pair().1)
    }

    #[inline(always)]
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth_back(n).map(|x| x.into_pair().1)
    }
}

impl<I: IntoIterator> IntoIterator for IntoRightSucc<I>
where
    I::Item: Pair,
{
    type Item = <I::Item as Pair>::Right;
    type IntoIter = RightSucc<I::IntoIter>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        RightSucc(self.0.into_iter())
    }
}

impl<L> Lender for RightIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
    // SAFETY: the lend is covariant as it projects the right component from the
    // underlying covariant lender L.
    unsafe_assume_covariance!();

    #[inline(always)]
    fn next(&mut self) -> Option<Lend<'_, Self>> {
        self.0.next().map(|x| {
            let (node, succ) = x.into_pair();
            (node, IntoRightSucc(succ))
        })
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl<'a, S: SequentialLabeling> IntoLender for &'a Right<S>
where
    S::Label: Pair,
{
    type Lender = <Right<S> as SequentialLabeling>::Lender<'a>;

    #[inline(always)]
    fn into_lender(self) -> Self::Lender {
        self.iter()
    }
}

impl<G> SplitLabeling for Right<G>
where
    G: SequentialLabeling + SplitLabeling,
    G::Label: Pair,
{
    type SplitLender<'a>
        = RightIterator<G::SplitLender<'a>>
    where
        Self: 'a;
    type IntoIterator<'a>
        = core::iter::Map<
        <G::IntoIterator<'a> as IntoIterator>::IntoIter,
        fn(G::SplitLender<'a>) -> Self::SplitLender<'a>,
    >
    where
        Self: 'a;

    fn split_iter(&self, how_many: usize) -> Self::IntoIterator<'_> {
        self.0.split_iter(how_many).into_iter().map(RightIterator)
    }
}

impl<S: SequentialLabeling> SequentialLabeling for Right<S>
where
    S::Label: Pair,
{
    type Label = <S::Label as Pair>::Right;

    type Lender<'node>
        = RightIterator<S::Lender<'node>>
    where
        Self: 'node;

    #[inline(always)]
    fn num_nodes(&self) -> usize {
        self.0.num_nodes()
    }

    #[inline(always)]
    fn num_arcs_hint(&self) -> Option<u64> {
        self.0.num_arcs_hint()
    }

    #[inline(always)]
    fn iter_from(&self, from: usize) -> Self::Lender<'_> {
        RightIterator(self.0.iter_from(from))
    }
}

impl<R: RandomAccessLabeling> RandomAccessLabeling for Right<R>
where
    R::Label: Pair,
{
    type Labels<'succ>
        = IntoRightSucc<<R as RandomAccessLabeling>::Labels<'succ>>
    where
        Self: 'succ;

    #[inline(always)]
    fn num_arcs(&self) -> u64 {
        self.0.num_arcs()
    }

    #[inline(always)]
    fn labels(&self, node_id: usize) -> <Self as RandomAccessLabeling>::Labels<'_> {
        IntoRightSucc(self.0.labels(node_id))
    }

    #[inline(always)]
    fn outdegree(&self, _node_id: usize) -> usize {
        self.0.outdegree(_node_id)
    }
}

impl<S: SequentialLabeling> SequentialGraph for Right<S> where S::Label: Pair<Right = usize> {}

impl<R: RandomAccessLabeling> RandomAccessGraph for Right<R> where R::Label: Pair<Right = usize> {}

unsafe impl<L: SortedLender> SortedLender for RightIterator<L>
where
    L: Lender + for<'next> NodeLabelsLender<'next>,
    for<'next> LenderLabel<'next, L>: Pair,
{
}

unsafe impl<I: SortedIterator> SortedIterator for RightSucc<I> where I::Item: Pair {}