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

vigna / webgraph-rs / 23365769388

20 Mar 2026 10:52PM UTC coverage: 68.228% (-3.0%) from 71.245%
23365769388

push

github

vigna
No le_bins,be_bins for webgraph

6655 of 9754 relevant lines covered (68.23%)

46582760.24 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>`](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
/// By default it uses an `Arc<Mmap>` so that it can be cloned.
24
#[derive(Clone)]
25
pub struct JavaPermutation<M = ArcMmapHelper<u64>> {
26
    pub perm: M,
27
}
28

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

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

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

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

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

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

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

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

116
    type ChunksMutError = std::convert::Infallible;
117

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

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

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