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

vigna / webgraph-rs / 14130263389

28 Mar 2025 01:40PM UTC coverage: 49.654% (-0.1%) from 49.798%
14130263389

push

github

zommiommy
fixed llp wrong combine types

22 of 41 new or added lines in 4 files covered. (53.66%)

1019 existing lines in 41 files now uncovered.

2437 of 4908 relevant lines covered (49.65%)

18919274.03 hits per line

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

49.12
/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
/// Returns 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
/// Returns 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)]
UNCOV
55
pub fn simplify(
×
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
> {
UNCOV
76
    let dir = Builder::new().prefix("simplify_").tempdir()?;
×
UNCOV
77
    let mut sorted = SortPairs::new(batch_size, dir.path())?;
×
78

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

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

104
/// Returns 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>(
2✔
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();
2✔
121
    let (tx, rx) = std::sync::mpsc::channel();
2✔
122

123
    let mut dirs = vec![];
2✔
124

125
    threads.in_place_scope(|scope| {
4✔
126
        let mut thread_id = 0;
2✔
127
        #[allow(clippy::explicit_counter_loop)] // enumerate requires some extra bounds here
128
        for iter in graph.split_iter(num_threads) {
10✔
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 |_| {
8✔
137
                log::debug!("Spawned thread {}", thread_id);
8✔
138
                let mut sorted = SortPairs::new(batch_size / num_threads, dir_path).unwrap();
8✔
139
                for_!( (src, succ) in iter {
651,122✔
140
                    for dst in succ {
13,515,722✔
141
                        if src != dst {
6,257,420✔
142
                            sorted.push(src, dst).unwrap();
6,257,420✔
143
                            sorted.push(dst, src).unwrap();
6,257,420✔
144
                        }
145
                    }
146
                });
147
                let result = sorted.iter().context("Could not read arcs").unwrap();
8✔
148
                tx.send(result).expect("Could not send the sorted pairs");
8✔
149
                log::debug!("Thread {} finished", thread_id);
8✔
150
            });
151
            thread_id += 1;
×
152
        }
153
    });
154
    drop(tx);
2✔
155

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

163
    drop(dirs);
2✔
164
    Ok(Left(sorted))
2✔
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