dsi_bitstream/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 [1 . . 2⁶⁴). Note that calling any method
23//! with *u* = 0 will cause an arithmetic error.
24//!
25//! See the [codes module documentation](crate::codes) for some elaboration on
26//! the difference between the big-endian and little-endian versions of the
27//! codes.
28
29use crate::traits::*;
30
31/// Returns the length of the minimal binary code for `n` with upper bound `u`.
32#[must_use]
33#[inline(always)]
34pub const fn len_minimal_binary(n: u64, u: u64) -> usize {
35 debug_assert!(n < u);
36 let l = u.ilog2();
37 let limit = ((1_u64 << l) << 1).wrapping_sub(u);
38 let mut result = l as usize;
39 if n >= limit {
40 result += 1;
41 }
42 result
43}
44
45/// Trait for reading minimal binary codes.
46pub trait MinimalBinaryRead<E: Endianness>: BitRead<E> {
47 #[inline(always)]
48 fn read_minimal_binary(&mut self, u: u64) -> Result<u64, Self::Error> {
49 let l = u.ilog2();
50 let mut prefix = self.read_bits(l as _)?;
51 let limit = ((1_u64 << l) << 1).wrapping_sub(u);
52
53 Ok(if prefix < limit {
54 prefix
55 } else {
56 prefix <<= 1;
57 prefix |= self.read_bits(1)?;
58 prefix - limit
59 })
60 }
61}
62
63/// Trait for writing minimal binary codes.
64pub trait MinimalBinaryWrite<E: Endianness>: BitWrite<E> {
65 #[inline(always)]
66 fn write_minimal_binary(&mut self, n: u64, u: u64) -> Result<usize, Self::Error> {
67 debug_assert!(n < u);
68 let l = u.ilog2();
69 let limit = ((1_u64 << l) << 1).wrapping_sub(u);
70
71 if n < limit {
72 self.write_bits(n, l as _)?;
73 Ok(l as usize)
74 } else {
75 let to_write = n + limit;
76 self.write_bits(to_write >> 1, l as _)?;
77 self.write_bits(to_write & 1, 1)?;
78 Ok((l + 1) as usize)
79 }
80 }
81}
82
83impl<E: Endianness, B: BitRead<E>> MinimalBinaryRead<E> for B {}
84impl<E: Endianness, B: BitWrite<E>> MinimalBinaryWrite<E> for B {}