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

vigna / webgraph-rs / 16005644811

01 Jul 2025 05:04PM UTC coverage: 50.317% (-0.04%) from 50.358%
16005644811

push

github

zommiommy
Fix tests

3888 of 7727 relevant lines covered (50.32%)

23067755.99 hits per line

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

22.64
/webgraph/src/utils/mod.rs
1
/*
2
 * SPDX-FileCopyrightText: 2023 Inria
3
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
4
 *
5
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6
 */
7

8
//! Miscellaneous utilities.
9

10
use rand::Rng;
11
use std::path::PathBuf;
12

13
/// Creates a new random dir inside the given folder
14
pub fn temp_dir<P: AsRef<std::path::Path>>(base: P) -> anyhow::Result<PathBuf> {
9✔
15
    let mut base = base.as_ref().to_owned();
27✔
16
    const ALPHABET: &[u8] = b"0123456789abcdef";
17
    let mut rnd = rand::rng();
18✔
18
    let mut random_str = String::new();
18✔
19
    loop {
×
20
        random_str.clear();
18✔
21
        for _ in 0..16 {
297✔
22
            let idx = rnd.random_range(0..ALPHABET.len());
144✔
23
            random_str.push(ALPHABET[idx] as char);
144✔
24
        }
25
        base.push(&random_str);
27✔
26

27
        if !base.exists() {
9✔
28
            std::fs::create_dir(&base)?;
18✔
29
            return Ok(base);
9✔
30
        }
31
        base.pop();
×
32
    }
33
}
34

35
mod circular_buffer;
36
pub(crate) use circular_buffer::*;
37

38
mod mmap_helper;
39
pub use mmap_helper::*;
40

41
mod java_perm;
42
pub use java_perm::*;
43

44
mod granularity;
45
pub use granularity::*;
46

47
pub mod sort_pairs;
48
pub use sort_pairs::SortPairs;
49

50
use crate::graphs::bvgraph::{Decode, Encode};
51

52
/// A decoder that encodes the read values using the given encoder.
53
/// This is commonly used to change the codes of a graph without decoding and
54
/// re-encoding it but by changing the codes.
55
pub struct Converter<D: Decode, E: Encode> {
56
    pub decoder: D,
57
    pub encoder: E,
58
    pub offset: usize,
59
}
60

61
impl<D: Decode, E: Encode> Decode for Converter<D, E> {
62
    // TODO: implement correctly start_node/end_node
63
    #[inline(always)]
64
    fn read_outdegree(&mut self) -> u64 {
×
65
        let res = self.decoder.read_outdegree();
×
66
        self.offset += self.encoder.write_outdegree(res).unwrap();
×
67
        res
×
68
    }
69
    #[inline(always)]
70
    fn read_reference_offset(&mut self) -> u64 {
×
71
        let res = self.decoder.read_reference_offset();
×
72
        self.offset += self.encoder.write_reference_offset(res).unwrap();
×
73
        res
×
74
    }
75
    #[inline(always)]
76
    fn read_block_count(&mut self) -> u64 {
×
77
        let res = self.decoder.read_block_count();
×
78
        self.offset += self.encoder.write_block_count(res).unwrap();
×
79
        res
×
80
    }
81
    #[inline(always)]
82
    fn read_block(&mut self) -> u64 {
×
83
        let res = self.decoder.read_block();
×
84
        self.offset += self.encoder.write_block(res).unwrap();
×
85
        res
×
86
    }
87
    #[inline(always)]
88
    fn read_interval_count(&mut self) -> u64 {
×
89
        let res = self.decoder.read_interval_count();
×
90
        self.offset += self.encoder.write_interval_count(res).unwrap();
×
91
        res
×
92
    }
93
    #[inline(always)]
94
    fn read_interval_start(&mut self) -> u64 {
×
95
        let res = self.decoder.read_interval_start();
×
96
        self.offset += self.encoder.write_interval_start(res).unwrap();
×
97
        res
×
98
    }
99
    #[inline(always)]
100
    fn read_interval_len(&mut self) -> u64 {
×
101
        let res = self.decoder.read_interval_len();
×
102
        self.offset += self.encoder.write_interval_len(res).unwrap();
×
103
        res
×
104
    }
105
    #[inline(always)]
106
    fn read_first_residual(&mut self) -> u64 {
×
107
        let res = self.decoder.read_first_residual();
×
108
        self.offset += self.encoder.write_first_residual(res).unwrap();
×
109
        res
×
110
    }
111
    #[inline(always)]
112
    fn read_residual(&mut self) -> u64 {
×
113
        let res = self.decoder.read_residual();
×
114
        self.offset += self.encoder.write_residual(res).unwrap();
×
115
        res
×
116
    }
117
}
118

119
/// Utility macro to create [`thread_pools`](`rayon::ThreadPool`).
120
///
121
/// There are two forms of this macro:
122
/// * Create a [`ThreadPool`](rayon::ThreadPool) with the default settings:
123
/// ```
124
/// # use webgraph::thread_pool;
125
/// let t: rayon::ThreadPool = thread_pool![];
126
/// ```
127
/// * Create a [`ThreadPool`](rayon::ThreadPool) with a given number of threads:
128
/// ```
129
/// # use webgraph::thread_pool;
130
/// let t: rayon::ThreadPool = thread_pool![7];
131
/// assert_eq!(t.current_num_threads(), 7);
132
/// ```
133
#[macro_export]
134
macro_rules! thread_pool {
135
    () => {
136
        rayon::ThreadPoolBuilder::new()
137
            .build()
138
            .expect("Cannot build a ThreadPool with default parameters")
139
    };
140
    ($num_threads:expr) => {
141
        rayon::ThreadPoolBuilder::new()
142
            .num_threads($num_threads)
143
            .build()
144
            .unwrap_or_else(|_| {
×
145
                panic!(
×
146
                    "Cannot build a ThreadPool with default parameters and {} threads",
×
147
                    $num_threads,
148
                )
149
            })
150
    };
151
}
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