Skip to main content

ToInt

Trait ToInt 

Source
pub trait ToInt {
    type Signed;

    // Required method
    fn to_int(self) -> Self::Signed;
}
Expand description

Extension trait mapping natural numbers bijectively to integers.

The method to_int will map a natural number x to x / 2 if x is even, and to −(x + 1) / 2 if x is odd. The inverse transformation is provided by the ToNat 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 unsigned integer types.

Required Associated Types§

Required Methods§

Source

fn to_int(self) -> Self::Signed

Implementors§