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

vigna / webgraph-rs / 22046400135

16 Feb 2026 12:49AM UTC coverage: 72.401% (+11.3%) from 61.096%
22046400135

push

github

vigna
fmt

6060 of 8370 relevant lines covered (72.4%)

48832055.95 hits per line

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

80.65
/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>`](sux::traits::BitFieldSlice).
18
///
19
/// The purpose of this helper class is to make interoperability with the big
20
/// version of the Java implementation of WebGraph easier. It is a thin wrapper
21
/// around [`MmapHelper`], and its methods are named accordingly.
22
///
23
/// Note that this class is only available on 64-bit platforms.
24
///
25
/// By default it uses an `Arc<Mmap>` so that it can be cloned.
26
#[cfg(target_pointer_width = "64")]
27
#[derive(Clone)]
28
pub struct JavaPermutation<M = ArcMmapHelper<u64>> {
29
    pub perm: M,
30
}
31

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

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

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

71
impl BitWidth<usize> for JavaPermutation {
72
    fn bit_width(&self) -> usize {
8✔
73
        64
8✔
74
    }
75
}
76

77
impl BitWidth<usize> for JavaPermutation<MmapHelper<u64, MmapMut>> {
78
    fn bit_width(&self) -> usize {
8✔
79
        64
8✔
80
    }
81
}
82

83
impl SliceByValue for JavaPermutation {
84
    type Value = usize;
85
    #[inline(always)]
86
    fn len(&self) -> usize {
24✔
87
        self.perm.as_ref().len()
48✔
88
    }
89
    #[inline(always)]
90
    unsafe fn get_value_unchecked(&self, index: usize) -> usize {
48,360,572✔
91
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
145,081,716✔
92
            as usize
48,360,572✔
93
    }
94
}
95

96
impl SliceByValue for JavaPermutation<MmapHelper<u64, MmapMut>> {
97
    type Value = usize;
98
    #[inline(always)]
99
    fn len(&self) -> usize {
2✔
100
        self.perm.as_ref().len()
4✔
101
    }
102
    #[inline(always)]
103
    unsafe fn get_value_unchecked(&self, index: usize) -> usize {
6✔
104
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
18✔
105
            as usize
6✔
106
    }
107
}
108

109
impl SliceByValueMut for JavaPermutation<MmapHelper<u64, MmapMut>> {
110
    #[inline(always)]
111
    unsafe fn set_value_unchecked(&mut self, index: usize, value: usize) {
4✔
112
        unsafe {
113
            *self.perm.as_mut().get_unchecked_mut(index) =
16✔
114
                u64::from_ne_bytes((value as u64).to_be_bytes());
8✔
115
        }
116
    }
117

118
    type ChunksMut<'a>
119
        = std::slice::ChunksMut<'a, usize>
120
    where
121
        Self: 'a;
122

123
    type ChunksMutError = std::convert::Infallible;
124

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

135
impl AsRef<[u64]> for JavaPermutation {
136
    fn as_ref(&self) -> &[u64] {
24✔
137
        self.perm.as_ref()
24✔
138
    }
139
}
140

141
impl AsRef<[u64]> for JavaPermutation<MmapHelper<u64, MmapMut>> {
142
    fn as_ref(&self) -> &[u64] {
8✔
143
        self.perm.as_ref()
8✔
144
    }
145
}
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