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

vigna / dsi-bitstream-rs / 18145139209

30 Sep 2025 10:26PM UTC coverage: 43.572% (-5.2%) from 48.731%
18145139209

push

github

vigna
Unified test

7 of 11 new or added lines in 2 files covered. (63.64%)

323 existing lines in 12 files now uncovered.

1559 of 3578 relevant lines covered (43.57%)

2347492.34 hits per line

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

70.0
/src/codes/minimal_binary.rs
1
/*
2
 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
3
 * SPDX-FileCopyrightText: 2023 Inria
4
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
5
 *
6
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
7
 */
8

9
//! Minimal binary codes.
10
//!
11
//! A minimal binary code with upper bound *u* > 0 (AKA [truncated binary
12
//! encoding](https://en.wikipedia.org/wiki/Truncated_binary_encoding)) is an
13
//! optimal prefix-free code for the first *u* natural numbers with uniform
14
//! distribution.
15
//!
16
//! There are several such codes, and the one implemented here is defined as
17
//! follows: let *s* = ⌈log₂*u*⌉; then, given *x* < *u*, if *x* <
18
//! 2*ˢ* − *u* then *x* is coded as the binary representation of *x*
19
//! in *s* − 1 bits; otherwise, *x* is coded as the binary representation of *x*
20
//! − *u* + 2*ˢ* in *s* bits.
21
//!
22
//! The supported range for *u* is [0 . . 2⁶⁴).
23
//!
24
//! See the [codes module documentation](crate::codes) for some elaboration on
25
//! the difference between the big-endian and little-endian versions of the
26
//! codes.
27

28
use crate::traits::*;
29

30
/// Returns the length of the minimal binary code for `n` with upper bound `u`.
31
#[must_use]
32
#[inline(always)]
33
pub fn len_minimal_binary(n: u64, u: u64) -> usize {
6,743✔
34
    debug_assert!(n < u);
13,486✔
35
    if u == 0 {
6,743✔
36
        return 0;
×
37
    }
UNCOV
38
    let l = u.ilog2();
×
UNCOV
39
    let limit = ((1_u64 << l) << 1).wrapping_sub(u);
×
40
    let mut result = l as usize;
×
41
    if n >= limit {
5,275✔
42
        result += 1;
5,275✔
43
    }
UNCOV
44
    result
×
45
}
46

47
/// Trait for reading minimal binary codes.
48
pub trait MinimalBinaryRead<E: Endianness>: BitRead<E> {
49
    #[inline(always)]
50
    fn read_minimal_binary(&mut self, u: u64) -> Result<u64, Self::Error> {
76,057✔
51
        let l = u.ilog2();
228,171✔
52
        let mut prefix = self.read_bits(l as _)?;
304,228✔
UNCOV
53
        let limit = ((1_u64 << l) << 1).wrapping_sub(u);
×
54

UNCOV
55
        Ok(if prefix < limit {
×
56
            prefix
38,745✔
57
        } else {
58
            prefix <<= 1;
37,312✔
UNCOV
59
            prefix |= self.read_bits(1)?;
×
60
            prefix - limit
37,312✔
61
        })
62
    }
63
}
64

65
/// Trait for writing minimal binary codes.
66
pub trait MinimalBinaryWrite<E: Endianness>: BitWrite<E> {
67
    #[inline(always)]
68
    fn write_minimal_binary(&mut self, n: u64, u: u64) -> Result<usize, Self::Error> {
68,962✔
69
        debug_assert!(n < u);
137,924✔
70
        let l = u.ilog2();
206,886✔
71
        let limit = ((1_u64 << l) << 1).wrapping_sub(u);
275,848✔
72

73
        if n < limit {
68,962✔
74
            self.write_bits(n, l as _)?;
130,320✔
75
            Ok(l as usize)
32,580✔
76
        } else {
77
            let to_write = n + limit;
36,382✔
UNCOV
78
            self.write_bits(to_write >> 1, l as _)?;
×
79
            self.write_bits(to_write & 1, 1)?;
36,382✔
80
            Ok((l + 1) as usize)
36,382✔
81
        }
82
    }
83
}
84

85
impl<E: Endianness, B: BitRead<E>> MinimalBinaryRead<E> for B {}
86
impl<E: Endianness, B: BitWrite<E>> MinimalBinaryWrite<E> for B {}
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