Skip to content

PATCHED stores most values as a Packed array and fixes up a few outliers from an exception list. It is codec id 5 in Flat and in IntCol, with the same layout in both.

Field Encoding Meaning
count varint n, the number of values
codec u8 5
base Packed(n) the base values, one slot per value
e varint number of exceptions
positions Packed(e) exception positions, ascending
values Packed(e) exception values, in the same order as the positions
  1. Read count, the codec byte (5) and the base Packed(n).
  2. Read e, then the positions Packed(e) and the exception values Packed(e).
  3. For each slot i, if its base value equals base.min, look i up in the positions. When it is listed, the value is the matching exception value. Otherwise the value is the base value.
  4. A slot whose base value is above base.min is never an exception.

Slots that are exceptions hold base.min in the base array, which is why only min-valued slots need to be looked up.

Derived by hand, not from a real file. The values are 0, 1, 2, 3, 1000. The writer stores the base as 0, 1, 2, 3, 0 (min 0, width 2) and slot 4 as one exception with value 1000.

05 05 count 5, codec PATCHED
00 02 E4 00 base: min 0, width 2, offsets 0 1 2 3 0
01 e = 1
08 00 positions: min 4 (zvarint 8), width 0
D0 0F 00 values: min 1000 (zvarint 2000), width 0

Slots 0 and 4 hold base.min. Slot 0 is not in the positions and stays 0. Slot 4 is, so it becomes 1000. The result is 0, 1, 2, 3, 1000.

  • Everything that Packed checks, three times.
  • count and e are within their limits.
  • Positions are ascending when written. A decoder rejects a position at or above n, and point lookups binary-search the positions, so out-of-order positions give wrong results.

See How writers choose a codec.