Packed integers
Packed(n) is the integer array that FOR, PATCHED and the other integer codecs use for their numbers. It stores n integers as a minimum plus n fixed-width offsets. It has no codec id byte and no count of its own: the reader knows n from the codec that contains it.
Layout
Section titled “Layout”| Field | Encoding | Meaning |
|---|---|---|
| min | zvarint | the smallest value, see zvarint |
| width | u8 | bits per value, at most 64 |
| bits | ceil(n x width / 8) bytes | value - min for each value, packed |
The bit stream is little-endian and filled least significant bit first. Value 0 occupies bits 0 to width - 1 of the stream, value 1 the next width bits, and so on. Bit k of the stream is bit k mod 8 of byte k div 8. Unused bits at the end of the last byte are padding.
Width 0 means every value equals min, and the bit area is empty (ceil(n x 0 / 8) is 0 bytes).
How to decode
Section titled “How to decode”- Read
minas a zvarint. - Read
widthas one byte. A width above 64 isCorrupt. - Compute
ceil(n x width / 8)with checked arithmetic and take that many bytes. - For each value
i, readwidthbits starting at biti x width, least significant bit first. The value ismin + offset.
Illustration
Section titled “Illustration”This example is derived by hand from the rules and is not taken from a real file. Take n = 4, min = 10 and width = 3.
The encoded min is zvarint of 10: z = 20 = 0x14. The offsets are 0, 3, 5, 7, which is 000, 011, 101, 111 in 3 bits. Written least significant bit first, the 12 bits of the stream are:
bit 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11val 0 0 0 | 1 1 0 | 1 0 1 | 1 1 1Bits 0 to 7 give 0b01011000 = 0x58. Bits 8 to 11 give 0b00001111 = 0x0F.
14 03 58 0F min 10, width 3, bits for offsets 0 3 5 7The decoded values are 10, 13, 15, 17.
Checks
Section titled “Checks”widthis at most 64.- The byte count
ceil(n x width / 8)must be computed with checked arithmetic and the input must contain that many bytes. ncomes from the enclosing codec and was checked against its limit there.
Related
Section titled “Related”- Column codecs overview and column container types
- FOR, PATCHED
- zvarint and varint