Trait common_traits::FastRange
source · pub trait FastRange: Integer + Sized {
type MaskType: Integer + Copy;
// Required methods
fn fast_range(&self, d: Self) -> Self;
fn fast_div_mask(&self, mask: Self::MaskType) -> Self;
fn fast_mod_mask(&self, d: Self, mask: Self::MaskType) -> Self;
fn compute_mask_fast(&self) -> Self::MaskType;
fn fast_is_divisible_mask(&self, mask: Self::MaskType) -> bool;
// Provided methods
fn fast_div(&self, d: Self) -> Self { ... }
fn fast_mod(&self, d: Self) -> Self { ... }
fn fast_is_divisible(&self, d: Self) -> bool { ... }
}Expand description
Fast division, modulo reduction, and an alternative operation that maps a number between 0 and n.
§Reference
https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ https://github.com/lemire/fastmod https://github.com/lemire/fastrange
Required Associated Types§
Required Methods§
sourcefn fast_range(&self, d: Self) -> Self
fn fast_range(&self, d: Self) -> Self
Given a value “UnsignedInt”, produces an integer in [0,p) without division. The function is as fair as possible in the sense that if you iterate through all possible values of “UnsignedInt”, then you will generate all possible outputs as uniformly as possible.
This is equivalent to computing ⌊n * (UnsignedInt / 2^w)⌋ in fixed comma
fn fast_div_mask(&self, mask: Self::MaskType) -> Self
sourcefn fast_mod_mask(&self, d: Self, mask: Self::MaskType) -> Self
fn fast_mod_mask(&self, d: Self, mask: Self::MaskType) -> Self
fastmod computes (a % d) given precomputed M,
sourcefn compute_mask_fast(&self) -> Self::MaskType
fn compute_mask_fast(&self) -> Self::MaskType
Compute the mask needed by fast_div_mask and fast_mod_mask
M = floor( (1<<64) / d ) + 1
sourcefn fast_is_divisible_mask(&self, mask: Self::MaskType) -> bool
fn fast_is_divisible_mask(&self, mask: Self::MaskType) -> bool
fastmod computes (a % d) == 0 given precomputed M,
Provided Methods§
fn fast_div(&self, d: Self) -> Self
sourcefn fast_is_divisible(&self, d: Self) -> bool
fn fast_is_divisible(&self, d: Self) -> bool
checks whether n % d == 0