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

vigna / dsi-bitstream-rs / 12468275135

23 Dec 2024 02:21PM UTC coverage: 57.076% (-1.8%) from 58.86%
12468275135

push

github

zommiommy
Fixed issue #18 and applied clippy fixes from new version

3 of 4 new or added lines in 3 files covered. (75.0%)

77 existing lines in 10 files now uncovered.

1835 of 3215 relevant lines covered (57.08%)

2945514.46 hits per line

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

85.71
/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 distribution.
14
//!
15
//! There are several such prefix-free codes, and the one implemented here is
16
//! defined as follows: if `s = ⌊log₂u⌋`, then the first `2^(s+1) - u` codewords are
17
//! the first binary numbers of length `s – 1`, and the remaining codewords
18
//! are the last `2u - 2^(s+1)` binary numbers of length `s`.
19

20
use crate::traits::*;
21

22
/// Returns the length of the minimal binary code for `n` with upper bound `max`.
23
#[must_use]
24
#[inline]
25
pub fn len_minimal_binary(n: u64, max: u64) -> usize {
3,554✔
26
    if max == 0 {
3,554✔
27
        return 0;
×
28
    }
29
    let l = max.ilog2();
3,554✔
30
    let limit = (1 << (l + 1)) - max;
3,554✔
31
    let mut result = l as usize;
3,554✔
32
    if n >= limit {
7,018✔
33
        result += 1;
3,464✔
34
    }
UNCOV
35
    result
×
36
}
37

38
/// Trait for reading minimal binary codes.
39
pub trait MinimalBinaryRead<E: Endianness>: BitRead<E> {
40
    #[inline(always)]
41
    fn read_minimal_binary(&mut self, max: u64) -> Result<u64, Self::Error> {
248,943✔
42
        let l = max.ilog2();
248,943✔
43
        let mut prefix = self.read_bits(l as _)?;
497,886✔
44
        let limit = (1 << (l + 1)) - max;
×
45

46
        Ok(if prefix < limit {
×
47
            prefix
145,515✔
48
        } else {
49
            prefix <<= 1;
103,428✔
50
            prefix |= self.read_bits(1)?;
103,428✔
51
            prefix - limit
103,428✔
52
        })
53
    }
54
}
55

56
/// Trait for writing minimal binary codes.
57
pub trait MinimalBinaryWrite<E: Endianness>: BitWrite<E> {
58
    #[inline]
59
    fn write_minimal_binary(&mut self, n: u64, max: u64) -> Result<usize, Self::Error> {
248,061✔
60
        let l = max.ilog2();
248,061✔
61
        let limit = (1 << (l + 1)) - max;
248,061✔
62

63
        if n < limit {
248,061✔
64
            self.write_bits(n, l as _)?;
145,501✔
65
            Ok(l as usize)
145,501✔
66
        } else {
67
            let to_write = n + limit;
102,560✔
68
            self.write_bits(to_write >> 1, l as _)?;
102,560✔
69
            self.write_bits(to_write & 1, 1)?;
102,560✔
70
            Ok((l + 1) as usize)
102,560✔
71
        }
72
    }
73
}
74

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