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

vigna / webgraph-rs / 14196927616

01 Apr 2025 01:28PM UTC coverage: 56.787% (+7.4%) from 49.345%
14196927616

Pull #122

github

web-flow
Merge 706583c76 into 5b5393a19
Pull Request #122: Refactored argmin and argmax into an extension trait for Iterator and moved argmin_filtered and argmax_filtered into compute.rs

1279 of 1630 new or added lines in 34 files covered. (78.47%)

3736 of 6579 relevant lines covered (56.79%)

13936227.79 hits per line

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

27.78
/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

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

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

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

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

70
impl BitFieldSliceCore<usize> for JavaPermutation {
71
    fn bit_width(&self) -> usize {
×
72
        64
×
73
    }
74

75
    fn len(&self) -> usize {
24,180,278✔
76
        self.perm.as_ref().len()
24,180,278✔
77
    }
78
}
79

80
impl BitFieldSliceCore<usize> for JavaPermutation<MmapHelper<u64, MmapMut>> {
81
    fn bit_width(&self) -> usize {
×
82
        64
×
83
    }
84

85
    fn len(&self) -> usize {
×
86
        self.perm.as_ref().len()
×
87
    }
88
}
89

90
impl BitFieldSlice<usize> for JavaPermutation {
91
    #[inline(always)]
92
    unsafe fn get_unchecked(&self, index: usize) -> usize {
24,180,276✔
93
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
24,180,276✔
94
            as usize
24,180,276✔
95
    }
96
}
97

98
impl BitFieldSlice<usize> for JavaPermutation<MmapHelper<u64, MmapMut>> {
99
    #[inline(always)]
100
    unsafe fn get_unchecked(&self, index: usize) -> usize {
×
NEW
101
        u64::from_be_bytes(unsafe { self.perm.as_ref().get_unchecked(index).to_ne_bytes() })
×
NEW
102
            as usize
×
103
    }
104
}
105

106
impl BitFieldSliceMut<usize> for JavaPermutation<MmapHelper<u64, MmapMut>> {
107
    #[inline(always)]
108
    unsafe fn set_unchecked(&mut self, index: usize, value: usize) {
×
109
        unsafe {
NEW
110
            *self.perm.as_mut().get_unchecked_mut(index) = value as u64;
×
111
        }
112
    }
113

114
    #[inline(always)]
115
    fn reset(&mut self) {
×
116
        self.perm.as_mut().reset();
×
117
    }
118

119
    fn par_reset(&mut self) {
×
120
        self.perm.as_mut().par_reset();
×
121
    }
122

123
    fn as_mut_slice(&mut self) -> &mut [usize] {
×
124
        unimplemented!(
125
            "This method is not implemented for JavaPermutation<MmapHelper<u64, MmapMut>>"
126
        );
127
    }
128

129
    type ChunksMut<'a>
130
        = std::slice::ChunksMut<'a, usize>
131
    where
132
        Self: 'a;
133

134
    fn try_chunks_mut(&mut self, _chunk_size: usize) -> Result<Self::ChunksMut<'_>, ()> {
×
135
        // Unsupported
136
        Err(())
×
137
    }
138
}
139

140
impl AsRef<[u64]> for JavaPermutation {
141
    fn as_ref(&self) -> &[u64] {
10✔
142
        self.perm.as_ref()
10✔
143
    }
144
}
145

146
impl AsRef<[u64]> for JavaPermutation<MmapHelper<u64, MmapMut>> {
147
    fn as_ref(&self) -> &[u64] {
×
148
        self.perm.as_ref()
×
149
    }
150
}
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