pub trait ToNat {
type Unsigned;
// Required method
fn to_nat(self) -> Self::Unsigned;
}Expand description
Extension trait mapping signed integers bijectively to natural numbers.
The method to_nat will map a nonnegative integer x
to 2x and a negative integer x to −2x − 1. The inverse transformation
is provided by the ToInt trait.
This pair of bijections makes it possible to use instantaneous codes for signed integers by mapping them to natural numbers and back.
This bijection is best known as the “ZigZag” transformation in Google’s
Protocol Buffers, albeit it has been used by
WebGraph since 2003, and most likely in
other software, for the same purpose. Note that the compression standards
H.264/H.265 use a different transformation for exponential Golomb codes,
mapping a positive integer x to 2x − 1 and a zero or negative integer
x to −2x.
The implementation uses a blanket implementation for all primitive signed integer types.