Decoding integer ranks in a binary probability tree #
A prefix has a natural-number mass, equal to the sum of its two children's masses. A rank
strictly below the initial mass selects a leaf by comparing it with the left child's mass and
subtracting that mass when taking the right branch. Each leaf has exactly its mass many initial
ranks. In particular, a tree of total mass 2 ^ bits is sampled exactly by bits fair bits.
The decoder is an executable deterministic function. The distribution theorems describe its pushforward of an existing uniform source; they do not assert a machine resource bound for the supplied mass function. Zero-mass nodes need no special case and are never reached from a valid rank. Depth zero and a zero-bit seed are included.
Main definitions #
Cslib.BitString.RankDecoder.decode: follow a rank through a fixed number of binary splits.
Main results #
Cslib.BitString.RankDecoder.length_decode: decoding produces the requested number of bits.Cslib.BitString.RankDecoder.mass_decode_pos: a valid rank reaches a positive-mass leaf.Cslib.BitString.RankDecoder.card_filter_decode: each leaf has exactly its mass many ranks.Cslib.BitString.RankDecoder.uniformFin_decode_apply: the law of a uniform finite rank.Cslib.BitString.RankDecoder.uniformList_decode_apply: the exact law from a fixed fair-bit seed.
Decode remaining bits after pre, querying only the left child's mass at each step.
The return value is the new suffix. The function is total; its probability guarantees apply to
ranks strictly below the initial mass.
A valid rank reaches a positive-mass leaf; zero-mass branches are not sampled.
The number of valid initial ranks producing a word is its leaf mass. Words of a different length have no preimages. This cardinality statement also permits total mass zero.
Decoding a uniform finite rank gives each leaf its integer mass divided by the total mass.
A tree with root mass 2 ^ bits is sampled exactly by bits fair bits followed by the
integer decoder. The statement includes zero bits and depth zero.