Proofs: RRR bit vector
These are the human proofs of RRR bit vector, at full length.
The RRR bit vector is correct and its offsets fit in lg C(n, m) + n/b + 1 bits full
This proof shows: correctness, space.
Write , with for . A block is a word of bits; its class is $c = $ the number of 1s of , and if its 1s are at , its offset is . Lines L1 to L4 compute exactly this sum: when the loop meets the -th 1, at , the counter has just become .
Lemma 1 (range). For a block of class , . More precisely, if all 1s are below position , then .
Proof. By induction on . For , . For , , and the first ones lie below , so by induction their sum is less than , that is at most . Then by Pascal's rule and the monotonicity of .
Lemma 2 (decode). For each block of class , DECODE.
Proof. DECODE scans from down. Let the invariant be: before step , the bits above of the output are those of , the remaining counter is the number of 1s of at positions , and the remaining value is for those 1s. It holds at . At step , if has a 1 at , then and , so line L7 sets the bit and lines L8 restore the invariant. If has no 1 at , then all of the ones are below , and Lemma 1 with gives , or ; so line L7 does not set the bit. After the output equals .
By Lemmas 1 and 2, ENCODE maps the blocks of class injectively into , so it is a bijection, and each offset fits in bits.
The pointers. BUILD appends the offsets of blocks to the stream in order, with lengths . So the offset of block starts at , which is by the definitions of the samples, with . In the same way . Hence line L14 (and L11, L18) rebuilds block exactly, by Lemma 2.
Access and rank. Position is bit of block , so line L11 is correct. For rank, $\text{rank}_1(i) = \text{rank}_1(t b) + $ the number of 1s of block at positions below . If the second term is 0, which is line L13; otherwise it is the POPCOUNT of line L15.
Select. Let . The number of 1s before superblock , , is nondecreasing in and , so the last with exists, and the -th 1 is not in a later superblock, because those start with at least ones before them. In the same way, the binary search of line L17 finds the block of superblock that holds the -th 1: the last block with . The -th 1 of is then the -th 1 of block , which line L19 returns. For the same argument holds with the counts of 0s, ; the bits of the last block word above are not part of and lie after the last real 0, so they are never reached when .
Space of the offsets. Let be the length of block : for each block but the last, and for the last. Let be the class of block , and let be the length of its offset, which suffices by Lemma 1 with . Cutting a vector into its blocks is a bijection between the bit vectors of length and the tuples of blocks of lengths . It maps the vectors whose blocks have the classes onto the tuples whose block has class , and there are of these. Each such vector has ones, so it is one of the vectors with ones, and . Hence