• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vigna / webgraph-rs / 13547061064

26 Feb 2025 03:09PM UTC coverage: 50.268% (-2.8%) from 53.021%
13547061064

push

github

zommiommy
fixing fuzz test

1 of 1 new or added line in 1 file covered. (100.0%)

324 existing lines in 29 files now uncovered.

2345 of 4665 relevant lines covered (50.27%)

19769434.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.81
/src/transform/simplify.rs
1
/*
2
 * SPDX-FileCopyrightText: 2023 Inria
3
 *
4
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
5
 */
6

7
use crate::graphs::{
8
    arc_list_graph, no_selfloops_graph::NoSelfLoopsGraph, union_graph::UnionGraph,
9
};
10
use crate::labels::Left;
11
use crate::traits::{LenderIntoIter, SequentialGraph, SortedIterator, SortedLender, SplitLabeling};
12
use crate::utils::sort_pairs::{BatchIterator, KMergeIters, SortPairs};
13
use anyhow::{Context, Result};
14
use dsi_progress_logger::prelude::*;
15
use itertools::{Dedup, Itertools};
16
use lender::*;
17
use rayon::ThreadPool;
18
use tempfile::Builder;
19

20
use super::transpose;
21

22
/// Return a simplified (i.e., undirected and loopless) version of the provided
23
/// sorted (both on nodes and successors) graph as a [sequential
24
/// graph](crate::traits::SequentialGraph).
25
///
26
/// This method exploits the fact that the input graph is already sorted,
27
/// sorting half the number of arcs of
28
/// [`simplify`](crate::transform::simplify::simplify).
29
#[allow(clippy::type_complexity)]
30
pub fn simplify_sorted<G: SequentialGraph>(
1✔
31
    graph: G,
32
    batch_size: usize,
33
) -> Result<
34
    NoSelfLoopsGraph<
35
        UnionGraph<G, Left<arc_list_graph::ArcListGraph<KMergeIters<BatchIterator<()>, ()>>>>,
36
    >,
37
>
38
where
39
    for<'a> G::Lender<'a>: SortedLender,
40
    for<'a, 'b> LenderIntoIter<'a, G::Lender<'b>>: SortedIterator,
41
{
42
    let transpose = transpose(&graph, batch_size).context("Could not transpose the graph")?;
2✔
43
    Ok(NoSelfLoopsGraph(UnionGraph(graph, transpose)))
×
44
}
45

46
/// Return a simplified (i.e., undirected and loopless) version of the provided
47
/// graph as a [sequential graph](crate::traits::SequentialGraph).
48
///
49
/// Note that if the graph is sorted (both on nodes and successors), it is
50
/// recommended to use [`simplify_sorted`](crate::transform::simplify::simplify_sorted).
51
///
52
/// For the meaning of the additional parameter, see
53
/// [`SortPairs`](crate::prelude::sort_pairs::SortPairs).
54
#[allow(clippy::type_complexity)]
55
pub fn simplify(
2✔
56
    graph: &impl SequentialGraph,
57
    batch_size: usize,
58
) -> Result<
59
    Left<
60
        arc_list_graph::ArcListGraph<
61
            std::iter::Map<
62
                Dedup<
63
                    core::iter::Filter<
64
                        core::iter::Map<
65
                            KMergeIters<BatchIterator<()>>,
66
                            fn((usize, usize, ())) -> (usize, usize),
67
                        >,
68
                        fn(&(usize, usize)) -> bool,
69
                    >,
70
                >,
71
                fn((usize, usize)) -> (usize, usize, ()),
72
            >,
73
        >,
74
    >,
75
> {
76
    let dir = Builder::new().prefix("simplify_").tempdir()?;
4✔
77
    let mut sorted = SortPairs::new(batch_size, dir.path())?;
2✔
78

UNCOV
79
    let mut pl = ProgressLogger::default();
×
UNCOV
80
    pl.item_name("node")
×
UNCOV
81
        .expected_updates(Some(graph.num_nodes()));
×
UNCOV
82
    pl.start("Creating batches...");
×
83
    // create batches of sorted edges
UNCOV
84
    let mut iter = graph.iter();
×
85
    while let Some((src, succ)) = iter.next() {
1,302,230✔
86
        for dst in succ {
13,515,722✔
UNCOV
87
            if src != dst {
×
88
                sorted.push(src, dst)?;
6,257,420✔
89
                sorted.push(dst, src)?;
6,257,420✔
90
            }
91
        }
92
        pl.light_update();
651,114✔
93
    }
94
    // merge the batches
95
    let map: fn((usize, usize, ())) -> (usize, usize) = |(src, dst, _)| (src, dst);
22,472,232✔
96
    let filter: fn(&(usize, usize)) -> bool = |(src, dst)| src != dst;
22,472,232✔
97
    let iter = Itertools::dedup(sorted.iter()?.map(map).filter(filter));
4✔
UNCOV
98
    let sorted = arc_list_graph::ArcListGraph::new(graph.num_nodes(), iter);
×
UNCOV
99
    pl.done();
×
100

UNCOV
101
    Ok(Left(sorted))
×
102
}
103

104
/// Return a simplified (i.e., undirected and loopless) version of the provided
105
/// graph as a [sequential graph](crate::traits::SequentialGraph).
106
///
107
/// This method uses splitting to sort in parallel different parts of the graph.
108
///
109
/// For the meaning of the additional parameter, see
110
/// [`SortPairs`](crate::prelude::sort_pairs::SortPairs).
111
#[allow(clippy::type_complexity)]
112
pub fn simplify_split<S>(
×
113
    graph: &S,
114
    batch_size: usize,
115
    threads: &ThreadPool,
116
) -> Result<Left<arc_list_graph::ArcListGraph<itertools::Dedup<KMergeIters<BatchIterator<()>, ()>>>>>
117
where
118
    S: SequentialGraph + SplitLabeling,
119
{
120
    let num_threads = threads.current_num_threads();
×
121
    let (tx, rx) = std::sync::mpsc::channel();
×
122

123
    let mut dirs = vec![];
×
124

125
    threads.in_place_scope(|scope| {
×
126
        let mut thread_id = 0;
×
127
        #[allow(clippy::explicit_counter_loop)] // enumerate requires some extra bounds here
128
        for iter in graph.split_iter(num_threads) {
×
129
            let tx = tx.clone();
×
130
            let dir = Builder::new()
×
131
                .prefix(&format!("simplify_split_{}_", thread_id))
×
132
                .tempdir()
×
133
                .expect("Could not create a temporary directory");
×
134
            let dir_path = dir.path().to_path_buf();
×
135
            dirs.push(dir);
×
136
            scope.spawn(move |_| {
×
137
                log::debug!("Spawned thread {}", thread_id);
×
138
                let mut sorted = SortPairs::new(batch_size / num_threads, dir_path).unwrap();
×
139
                for_!( (src, succ) in iter {
×
140
                    for dst in succ {
×
141
                        if src != dst {
×
142
                            sorted.push(src, dst).unwrap();
×
143
                            sorted.push(dst, src).unwrap();
×
144
                        }
145
                    }
146
                });
147
                let result = sorted.iter().context("Could not read arcs").unwrap();
×
148
                tx.send(result).expect("Could not send the sorted pairs");
×
149
                log::debug!("Thread {} finished", thread_id);
×
150
            });
151
            thread_id += 1;
×
152
        }
153
    });
154
    drop(tx);
×
155

156
    // get a graph on the sorted data
157
    log::debug!("Waiting for threads to finish");
×
158
    let edges: KMergeIters<BatchIterator> = rx.iter().sum();
×
159
    let edges = edges.dedup();
×
160
    log::debug!("All threads finished");
×
161
    let sorted = arc_list_graph::ArcListGraph::new_labeled(graph.num_nodes(), edges);
×
162

163
    drop(dirs);
×
164
    Ok(Left(sorted))
×
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc