Skip to content

Commit d323da7

Browse files
committed
eq_sorted and check_impl associated functions
1 parent 1612b43 commit d323da7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

webgraph/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Change Log
22

3+
## [0.3.1] -
4+
5+
### New
6+
7+
* `SequentialLabeling::eq_sorted` associated function that checks
8+
equality between labelings.
9+
10+
* `RandomAccessLabeling::check_impl` associated function that checks
11+
that the sequential and random-access implementations return the
12+
same results.
13+
314
## [0.3.0] - 2025-05-23
415

516
### Changed

webgraph/src/traits/labels.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ and nodes identifier are in the interval [0 . . *n*).
2424
2525
*/
2626

27-
use crate::utils::Granularity;
27+
use crate::{
28+
traits::{LenderIntoIter, RandomAccessGraph},
29+
utils::Granularity,
30+
};
2831

2932
use super::{LenderLabel, NodeLabelsLender, ParMapFold};
3033

@@ -193,6 +196,30 @@ pub trait SequentialLabeling {
193196
thread_pool,
194197
)
195198
}
199+
200+
/// Returns true if the two provided sorted labelings are equal.
201+
fn equal_sorted<L0: SequentialLabeling, L1: SequentialLabeling<Label = L0::Label>>(
202+
l0: &L0,
203+
l1: &L1,
204+
) -> bool
205+
where
206+
for<'a> L0::Lender<'a>: SortedLender,
207+
for<'a> L1::Lender<'a>: SortedLender,
208+
for<'a, 'b> LenderIntoIter<'b, L0::Lender<'a>>: SortedIterator,
209+
for<'a, 'b> LenderIntoIter<'b, L0::Lender<'a>>: SortedIterator,
210+
L0::Label: PartialEq,
211+
{
212+
if l0.num_nodes() != l1.num_nodes() {
213+
return false;
214+
}
215+
for_!(((node0, succ0), (node1, succ1)) in l0.iter().zip(l1.iter()) {
216+
debug_assert_eq!(node0, node1);
217+
if !succ0.into_iter().eq(succ1.into_iter()) {
218+
return false;
219+
}
220+
});
221+
true
222+
}
196223
}
197224

198225
/// Convenience type alias for the iterator over the labels of a node
@@ -316,6 +343,27 @@ pub trait RandomAccessLabeling: SequentialLabeling {
316343

317344
/// Returns the number of labels associated with a node.
318345
fn outdegree(&self, node_id: usize) -> usize;
346+
347+
/// Checks the sequential vs. random-access implementation of a sorted
348+
/// random-access labeling.
349+
///
350+
/// Note that this method will check that the sequential and random-access
351+
/// iterators on labels of each node are identical, and that the number of
352+
/// nodes returned by the sequential iterator is the same as the number of
353+
/// nodes returned by [`num_nodes`](SequentialLabeling::num_nodes).
354+
fn check_impl<L: RandomAccessLabeling>(l: L) -> bool
355+
where
356+
L::Label: PartialEq,
357+
{
358+
let mut num_nodes = 0;
359+
for_!((node, succ) in l.iter() {
360+
num_nodes += 1;
361+
if !succ.into_iter().eq(l.labels(node).into_iter()) {
362+
return false;
363+
}
364+
});
365+
num_nodes == l.num_nodes()
366+
}
319367
}
320368

321369
/// A struct used to make it easy to implement sequential access

0 commit comments

Comments
 (0)