Trait ToInt

Source
pub trait ToInt: UnsignedInt + AsBytes {
    // Provided method
    fn to_int(self) -> Self::SignedInt { ... }
}
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 much likely in other software, for the same purpose. Note that the compression standards H.264/H.265 uses 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 is just based on the traits UnsignedInt and AsBytes. We provide blanket implementations for all primitive unsigned integer types, but it can be used with any type implementing those traits.

Provided Methods§

Source

fn to_int(self) -> Self::SignedInt

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToInt for u8

Source§

impl ToInt for u16

Source§

impl ToInt for u32

Source§

impl ToInt for u64

Source§

impl ToInt for u128

Source§

impl ToInt for usize

Implementors§