1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* SPDX-FileCopyrightText: 2023 Sebastiano Vigna
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
//! Golomb codes.
//!
//! Given a modulus *b* ≥ 1, the Golomb code of a natural number *x* is given by
//! ⌊*x* / *b*⌋ in [unary code] followed by the [minimal binary code] of *x* mod
//! *b*.
//!
//! Let *r* be the root of order *b* of 2: then, the implied distribution of the
//! Golomb code with modulus *b* is ≈ 1/*rˣ*.
//!
//! Note that the Golomb code for *b* = 1 is exactly the unary code.
//!
//! For natural numbers distributed with a geometric distribution with base *p*,
//! the optimal code is a Golomb code with [*b* = ⌈−log(2 − *p*) / log(1 −
//! *p*)⌉].
//!
//! For a faster, less precise alternative, see [Rice codes].
//!
//! The supported range is [0 . . 2⁶⁴) for *b* in [1 . . 2⁶⁴), but writing
//! 2⁶⁴ – 1 when *b* = 1 requires writing the unary code for 2⁶⁴ – 1, which
//! might not be possible depending on the [`BitWrite`]
//! implementation (and would require writing 2⁶⁴ bits anyway).
//!
//! # References
//!
//! Solomon W. Golomb, “[Run-length encodings (Corresp.)]”. IEEE Transactions on
//! Information Theory, 12(3):399–401, July 1966.
//!
//! Robert G. Gallager and David C. Van Voorhis, “[Optimal source codes for
//! geometrically distributed integer alphabets (Corresp.)]”. IEEE Transactions
//! on Information Theory, 21(2):228–230, March 1975.
//!
//! [unary code]: BitRead::read_unary
//! [minimal binary code]: super::minimal_binary
//! [*b* = ⌈−log(2 − *p*) / log(1 − *p*)⌉]: b
//! [Rice codes]: super::rice
//! [Run-length encodings (Corresp.)]: https://doi.org/10.1109/TIT.1966.1053907
//! [Optimal source codes for geometrically distributed integer alphabets (Corresp.)]: https://doi.org/10.1109/TIT.1975.1055357
use ;
use crate*;
/// Returns the length of the Golomb code for `n` with modulus `b`.
pub const
/// Returns the optimal value of *b* for a geometric distribution of base *p*,
/// that is, ⌈−log(2 − *p*) / log(1 − *p*)⌉.
/// Trait for reading Golomb codes.
/// Trait for writing Golomb codes.