ddn.encoding.zigzag

ddn.encoding.zigzag — the ZigZag signed-integer transform.

ZigZag maps signed integers onto unsigned ones so that small magnitudes — positive or negative — stay small: 0 ↔ 0, -1 ↔ 1, 1 ↔ 2, -2 ↔ 3, 2 ↔ 4, … It is a **total bijection on every bit pattern** of the given width, including both signed extrema, so encode/decode are plain value-returning functions with no error path (design record doc/DDN-ENCODING.md D2).

The transform is the one used by protocol buffers (sint32/ sint64 wire values) and Parquet/Thrift compact signed fields. Pair it with ddn.encoding.varint for the complete signed-varint composition (see the N009 notes in the design record).

All functions are @nogc nothrow @safe pure and CTFE-capable.

Example:

import ddn.encoding.zigzag;

assert(zigzag.encode32(0) == 0);
assert(zigzag.encode32(-1) == 1);
assert(zigzag.encode32(1) == 2);
assert(zigzag.decode32(zigzag.encode32(-123)) == -123);

Functions 4

fnuint encode32(int n) @safe pure nothrow @nogcZigZag-encodes a signed 32-bit integer.
fnint decode32(uint u) @safe pure nothrow @nogcInverts encode32.
fnulong encode64(long n) @safe pure nothrow @nogcZigZag-encodes a signed 64-bit integer.
fnlong decode64(ulong u) @safe pure nothrow @nogcInverts encode64.