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

vigna / webgraph-rs / 24296210049

12 Apr 2026 01:49AM UTC coverage: 66.827% (-0.05%) from 66.877%
24296210049

push

github

vigna
Clippy hapy

6674 of 9987 relevant lines covered (66.83%)

47792805.58 hits per line

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

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

8
use crate::utils::{ArcMmapHelper, MmapHelper};
9
use anyhow::Result;
10
use mmap_rs::{MmapFlags, MmapMut};
11
use std::path::Path;
12
use std::sync::Arc;
13
use sux::traits::*;
14
use value_traits::slices::{SliceByValue, SliceByValueMut};
15

16
/// Maps into memory a file of big-endian 64-bit values, making it accessible as
17
/// a [`BitFieldSlice<usize>`].
18
///
19
/// [`BitFieldSlice<usize>`]: sux::traits::BitFieldSlice
20
///
21
/// The purpose of this helper class is to make interoperability with the big
22
/// version of the Java implementation of WebGraph easier. It is a thin wrapper
23
/// around [`MmapHelper`], and its methods are named accordingly.
24
///
25
/// By default it uses an `Arc<Mmap>` so that it can be cloned.
26
#[derive(Clone)]
27
pub struct JavaPermutation<M = ArcMmapHelper<u64>> {
28
    pub perm: M,
29
}
30

31
impl JavaPermutation<MmapHelper<u64, MmapMut>> {
32
    /// Creates and map a permutation into memory (read/write), overwriting it if it exists.
33
    ///
34
    /// # Arguments
35
    /// - `path` - The path to the permutation.
36
    /// - `flags` - The flags to use for the memory mapping.
37
    /// - `len` - The length of the permutation (number of 64-bit unsigned values).
38
    pub fn new(path: impl AsRef<Path>, flags: MmapFlags, len: usize) -> Result<Self> {
×
39
        Ok(Self {
×
40
            perm: MmapHelper::new(path, flags, len)?,
×
41
        })
42
    }
43

44
    /// Maps a permutation into memory (read/write).
45
    ///
46
    /// # Arguments
47
    /// - `path` - The path to the permutation.
48
    /// - `flags` - The flags to use for the memory mapping.
49
    pub fn mmap_mut(path: impl AsRef<Path>, flags: MmapFlags) -> Result<Self> {
2✔
50
        Ok(Self {
×
51
            perm: MmapHelper::mmap_mut(path, flags)?,
6✔
52
        })
53
    }
54
}
55

56
impl JavaPermutation {
57
    /// Maps a permutation into memory (read-only).
58
    ///
59
    /// # Arguments
60
    /// - `path` - The path to the permutation.
61
    /// - `flags` - The flags to use for the memory mapping.
62
    pub fn mmap(path: impl AsRef<Path>, flags: MmapFlags) -> Result<Self> {
12✔
63
        Ok(Self {
×
64
            perm: ArcMmapHelper(Arc::new(MmapHelper::mmap(path, flags)?)),
48✔
65
        })
66
    }
67
}
68

69
impl<M> BitWidth for JavaPermutation<M> {
70
    fn bit_width(&self) -> usize {
2✔
71
        64
2✔
72
    }
73
}
74

75
impl SliceByValue for JavaPermutation {
76
    type Value = usize;
77
    #[inline(always)]
78
    fn len(&self) -> usize {
60✔
79
        self.perm.as_ref().len()
120✔
80
    }
81
    #[inline(always)]
82
    unsafe fn get_value_unchecked(&self, index: usize) -> usize {
50✔
83
        // SAFETY: the caller guarantees that index is within bounds.
84
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
150✔
85
            as usize
50✔
86
    }
87
}
88

89
impl SliceByValue for JavaPermutation<MmapHelper<u64, MmapMut>> {
90
    type Value = usize;
91
    #[inline(always)]
92
    fn len(&self) -> usize {
1✔
93
        self.perm.as_ref().len()
2✔
94
    }
95
    #[inline(always)]
96
    unsafe fn get_value_unchecked(&self, index: usize) -> usize {
2✔
97
        // SAFETY: the caller guarantees that index is within bounds.
98
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
6✔
99
            as usize
2✔
100
    }
101
}
102

103
impl SliceByValueMut for JavaPermutation<MmapHelper<u64, MmapMut>> {
104
    #[inline(always)]
105
    unsafe fn set_value_unchecked(&mut self, index: usize, value: usize) {
3✔
106
        // SAFETY: the caller guarantees that index is within bounds.
107
        unsafe {
108
            *self.perm.as_mut().get_unchecked_mut(index) =
12✔
109
                u64::from_ne_bytes((value as u64).to_be_bytes());
6✔
110
        }
111
    }
112

113
    type ChunksMut<'a>
114
        = std::slice::ChunksMut<'a, usize>
115
    where
116
        Self: 'a;
117

118
    type ChunksMutError = std::convert::Infallible;
119

120
    fn try_chunks_mut(
×
121
        &mut self,
122
        _chunk_size: usize,
123
    ) -> Result<Self::ChunksMut<'_>, std::convert::Infallible> {
124
        unimplemented!(
125
            "This method is not implemented for JavaPermutation<MmapHelper<u64, MmapMut>>"
126
        );
127
    }
128
}
129

130
impl AsRef<[u64]> for JavaPermutation {
131
    fn as_ref(&self) -> &[u64] {
1✔
132
        self.perm.as_ref()
1✔
133
    }
134
}
135

136
impl AsRef<[u64]> for JavaPermutation<MmapHelper<u64, MmapMut>> {
137
    fn as_ref(&self) -> &[u64] {
2✔
138
        self.perm.as_ref()
2✔
139
    }
140
}
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