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

vigna / dsi-bitstream-rs / 18131186772

30 Sep 2025 01:16PM UTC coverage: 48.731% (-3.0%) from 51.712%
18131186772

push

github

vigna
Better error message

0 of 2 new or added lines in 1 file covered. (0.0%)

114 existing lines in 12 files now uncovered.

1728 of 3546 relevant lines covered (48.73%)

1985043.52 hits per line

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

67.86
/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
//! See the [codes module documentation](crate::codes) for some elaboration on
23
//! the difference between the big-endian and little-endian versions of the
24
//! codes.
25

26
use crate::traits::*;
27

28
/// Returns the length of the minimal binary code for `n` with upper bound `max`.
29
#[must_use]
30
#[inline(always)]
31
pub fn len_minimal_binary(n: u64, max: u64) -> usize {
71,929✔
32
    if max == 0 {
71,929✔
33
        return 0;
×
34
    }
UNCOV
35
    let l = max.ilog2();
×
UNCOV
36
    let limit = ((1_u64 << l) << 1).wrapping_sub(max);
×
UNCOV
37
    let mut result = l as usize;
×
38
    if n >= limit {
39,264✔
39
        result += 1;
39,264✔
40
    }
41
    result
×
42
}
43

44
/// Trait for reading minimal binary codes.
45
pub trait MinimalBinaryRead<E: Endianness>: BitRead<E> {
46
    #[inline(always)]
47
    fn read_minimal_binary(&mut self, max: u64) -> Result<u64, Self::Error> {
39,003✔
48
        let l = max.ilog2();
117,009✔
49
        let mut prefix = self.read_bits(l as _)?;
156,012✔
50
        let limit = ((1_u64 << l) << 1).wrapping_sub(max);
×
51

52
        Ok(if prefix < limit {
×
53
            prefix
19,383✔
54
        } else {
55
            prefix <<= 1;
19,620✔
UNCOV
56
            prefix |= self.read_bits(1)?;
×
57
            prefix - limit
19,620✔
58
        })
59
    }
60
}
61

62
/// Trait for writing minimal binary codes.
63
pub trait MinimalBinaryWrite<E: Endianness>: BitWrite<E> {
64
    #[inline(always)]
65
    fn write_minimal_binary(&mut self, n: u64, max: u64) -> Result<usize, Self::Error> {
35,114✔
66
        let l = max.ilog2();
105,342✔
67
        let limit = ((1_u64 << l) << 1).wrapping_sub(max);
140,456✔
68

69
        if n < limit {
35,114✔
70
            self.write_bits(n, l as _)?;
65,688✔
71
            Ok(l as usize)
16,422✔
72
        } else {
73
            let to_write = n + limit;
18,692✔
UNCOV
74
            self.write_bits(to_write >> 1, l as _)?;
×
75
            self.write_bits(to_write & 1, 1)?;
18,692✔
76
            Ok((l + 1) as usize)
18,692✔
77
        }
78
    }
79
}
80

81
impl<E: Endianness, B: BitRead<E>> MinimalBinaryRead<E> for B {}
82
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