2020-02-14 10:14:22 -05:00
|
|
|
//! Numeric Helper Traits
|
2020-02-19 21:52:00 -05:00
|
|
|
//!
|
|
|
|
//! Home to the numeric trait chain of doom, aka `Unsigned`
|
2020-02-14 23:41:14 -05:00
|
|
|
#![allow(unused_comparisons)]
|
|
|
|
use core::convert::TryFrom;
|
|
|
|
use core::fmt::Debug;
|
|
|
|
use core::ops::{
|
2020-02-14 10:14:22 -05:00
|
|
|
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
|
|
|
|
Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
|
|
|
|
};
|
|
|
|
|
2020-02-19 16:22:01 -05:00
|
|
|
/// Represents the sign of a rational number
|
2020-02-18 16:38:26 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2020-02-19 21:10:53 -05:00
|
|
|
#[repr(u8)]
|
2020-02-18 16:38:26 -05:00
|
|
|
pub enum Sign {
|
|
|
|
/// Greater than zero, or zero
|
|
|
|
Positive,
|
|
|
|
|
|
|
|
/// Less than zero
|
|
|
|
Negative,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Sign {
|
|
|
|
fn default() -> Self {
|
|
|
|
Sign::Positive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Not for Sign {
|
|
|
|
type Output = Sign;
|
|
|
|
|
|
|
|
fn not(self) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
Self::Positive => Self::Negative,
|
|
|
|
Self::Negative => Self::Positive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
/// Native number type
|
|
|
|
pub trait Num:
|
2020-02-14 23:41:14 -05:00
|
|
|
Add
|
|
|
|
+ AddAssign
|
|
|
|
+ Debug
|
|
|
|
+ Div
|
|
|
|
+ DivAssign
|
|
|
|
+ Mul
|
|
|
|
+ MulAssign
|
|
|
|
+ Rem
|
|
|
|
+ RemAssign
|
|
|
|
+ PartialOrd
|
|
|
|
+ PartialEq
|
|
|
|
+ Copy
|
|
|
|
+ Sub
|
|
|
|
+ SubAssign
|
2020-02-14 10:14:22 -05:00
|
|
|
{
|
2020-02-19 21:52:00 -05:00
|
|
|
/// Implement absolute value function for unsigned numbers
|
2020-02-19 21:10:53 -05:00
|
|
|
fn abs(self) -> Self {
|
|
|
|
self
|
2020-02-14 23:41:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
/// Integer primitive
|
|
|
|
pub trait Int:
|
|
|
|
Num
|
|
|
|
+ BitAnd
|
|
|
|
+ BitAndAssign
|
|
|
|
+ BitOr
|
|
|
|
+ BitOrAssign
|
|
|
|
+ BitXor
|
|
|
|
+ BitXorAssign
|
|
|
|
+ Eq
|
|
|
|
+ Ord
|
|
|
|
+ Not
|
|
|
|
+ Shl
|
|
|
|
+ Shr
|
|
|
|
+ ShlAssign
|
|
|
|
+ ShrAssign
|
|
|
|
{
|
2020-02-18 16:38:26 -05:00
|
|
|
/// Associated type for unsigned conversion
|
|
|
|
type Un;
|
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
/// The maximum value of the type
|
|
|
|
fn max_value() -> Self;
|
|
|
|
|
|
|
|
/// Is this a zero value?
|
|
|
|
fn is_zero(self) -> bool;
|
2020-02-14 23:41:14 -05:00
|
|
|
|
|
|
|
/// Is this number less than zero?
|
|
|
|
fn is_neg(self) -> bool;
|
2020-02-18 16:38:26 -05:00
|
|
|
|
2020-02-19 14:08:43 -05:00
|
|
|
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic
|
|
|
|
/// overflow would occur. If an overflow would have occurred then the wrapped value is returned.
|
|
|
|
fn left_overflowing_sub(self, rhs: Self) -> (Self, bool);
|
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
/// Convert to an unsigned number
|
|
|
|
///
|
|
|
|
/// A meaningless operation when implemented on an
|
|
|
|
/// unsigned type, but the interface consistency solves
|
|
|
|
/// other issues
|
|
|
|
fn to_unsigned(self) -> Self::Un;
|
2020-02-14 10:14:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A Trait representing unsigned integer primitives
|
2020-02-19 21:23:07 -05:00
|
|
|
pub trait Unsigned:
|
|
|
|
Int + Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>
|
|
|
|
{
|
2020-02-14 10:14:22 -05:00
|
|
|
/// Find the greatest common denominator of two numbers
|
|
|
|
fn gcd(a: Self, b: Self) -> Self;
|
|
|
|
|
|
|
|
/// Find the least common multiple of two numbers
|
|
|
|
fn lcm(a: Self, b: Self) -> Self;
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
/// A Trait representing signed integer primitives
|
|
|
|
pub trait Signed: Int {}
|
2020-02-14 10:14:22 -05:00
|
|
|
|
2020-02-19 21:52:00 -05:00
|
|
|
macro_rules! impl_empty {
|
|
|
|
($Ident: ty, ($( $Type: ty ),*) ) => {
|
2020-02-14 10:14:22 -05:00
|
|
|
$(
|
2020-02-19 21:52:00 -05:00
|
|
|
impl $Ident for $Type {}
|
2020-02-14 10:14:22 -05:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_int {
|
2020-02-18 16:38:26 -05:00
|
|
|
($(($type: ty, $un_type: ty)),* ) => {
|
2020-02-14 10:14:22 -05:00
|
|
|
$(
|
2020-02-18 16:38:26 -05:00
|
|
|
impl Int for $type {
|
|
|
|
|
|
|
|
type Un = $un_type;
|
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
fn is_zero(self) -> bool {
|
|
|
|
self == 0
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:38:26 -05:00
|
|
|
fn max_value() -> $type {
|
|
|
|
<$type>::max_value()
|
2020-02-14 10:14:22 -05:00
|
|
|
}
|
2020-02-14 23:41:14 -05:00
|
|
|
|
|
|
|
fn is_neg(self) -> bool {
|
2020-02-19 21:10:53 -05:00
|
|
|
self < 0
|
2020-02-14 23:41:14 -05:00
|
|
|
}
|
2020-02-18 16:38:26 -05:00
|
|
|
|
|
|
|
fn to_unsigned(self) -> $un_type {
|
2020-02-19 21:10:53 -05:00
|
|
|
let abs = <$type>::abs(self);
|
2020-02-18 16:38:26 -05:00
|
|
|
// Converting from signed to unsigned should always be safe
|
|
|
|
// when using the absolute value, especially since I'm converting
|
|
|
|
// between the same bit size
|
2020-02-19 21:10:53 -05:00
|
|
|
<$un_type>::try_from(abs).unwrap()
|
2020-02-19 14:08:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn left_overflowing_sub(self, rhs: Self) -> (Self, bool) {
|
|
|
|
let (res, overflow) = <$type>::overflowing_sub(self, rhs);
|
|
|
|
let res = if overflow {
|
|
|
|
<$type>::max_value() - res + 1
|
|
|
|
} else {
|
|
|
|
res
|
|
|
|
};
|
|
|
|
|
|
|
|
(res, overflow)
|
|
|
|
}
|
2020-02-14 10:14:22 -05:00
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_unsigned {
|
|
|
|
($($Type: ty),* ) => {
|
|
|
|
$(
|
|
|
|
impl Unsigned for $Type {
|
2020-02-18 16:38:26 -05:00
|
|
|
/// Implementation based on
|
2020-02-19 21:52:00 -05:00
|
|
|
/// [Binary GCD algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm)
|
2020-02-14 10:14:22 -05:00
|
|
|
fn gcd(a: $Type, b: $Type) -> $Type {
|
|
|
|
if a == b {
|
|
|
|
return a;
|
|
|
|
} else if a == 0 {
|
|
|
|
return b;
|
|
|
|
} else if b == 0 {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
let a_even = a % 2 == 0;
|
|
|
|
let b_even = b % 2 == 0;
|
|
|
|
|
|
|
|
if a_even {
|
|
|
|
if b_even {
|
|
|
|
// Both a & b are even
|
|
|
|
return Self::gcd(a >> 1, b >> 1) << 1;
|
|
|
|
} else if !b_even {
|
|
|
|
// b is odd
|
|
|
|
return Self::gcd(a >> 1, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a is odd, b is even
|
|
|
|
if (!a_even) && b_even {
|
|
|
|
return Self::gcd(a, b >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if a > b {
|
|
|
|
return Self::gcd((a - b) >> 1, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::gcd((b - a) >> 1, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lcm(a: $Type, b: $Type) -> $Type {
|
|
|
|
if (a == 0 && b == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
a * b / Self::gcd(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:52:00 -05:00
|
|
|
impl_empty!(Num, (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize));
|
2020-02-18 16:38:26 -05:00
|
|
|
impl_int!(
|
2020-02-14 23:41:14 -05:00
|
|
|
(i8, u8),
|
2020-02-18 16:38:26 -05:00
|
|
|
(u8, u8),
|
2020-02-14 23:41:14 -05:00
|
|
|
(i16, u16),
|
2020-02-18 16:38:26 -05:00
|
|
|
(u16, u16),
|
2020-02-14 23:41:14 -05:00
|
|
|
(i32, u32),
|
2020-02-18 16:38:26 -05:00
|
|
|
(u32, u32),
|
2020-02-14 23:41:14 -05:00
|
|
|
(i64, u64),
|
2020-02-18 16:38:26 -05:00
|
|
|
(u64, u64),
|
2020-02-14 23:41:14 -05:00
|
|
|
(i128, u128),
|
2020-02-18 16:38:26 -05:00
|
|
|
(u128, u128),
|
|
|
|
(isize, usize),
|
|
|
|
(usize, usize)
|
2020-02-14 23:41:14 -05:00
|
|
|
);
|
2020-02-18 16:38:26 -05:00
|
|
|
impl_unsigned!(u8, u16, u32, u64, u128, usize);
|
2020-02-19 21:52:00 -05:00
|
|
|
impl_empty!(Signed, (i8, i16, i32, i64, i128, isize));
|
2020-02-14 10:14:22 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-02-19 16:22:01 -05:00
|
|
|
#[test]
|
|
|
|
fn test_los() {
|
|
|
|
assert_eq!(4u8.left_overflowing_sub(2).0, 2u8);
|
|
|
|
assert_eq!(0u8.left_overflowing_sub(2).0, 2u8);
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:14:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_gcd() {
|
2020-02-19 09:39:19 -05:00
|
|
|
assert_eq!(u8::gcd(2, 3), 1);
|
2020-02-14 10:14:22 -05:00
|
|
|
assert_eq!(u8::gcd(2, 2), 2);
|
2020-02-18 21:29:40 -05:00
|
|
|
assert_eq!(u8::gcd(2, 8), 2);
|
2020-02-14 10:14:22 -05:00
|
|
|
assert_eq!(u16::gcd(36, 48), 12);
|
|
|
|
}
|
2020-02-14 12:11:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lcm() {
|
2020-02-19 09:39:19 -05:00
|
|
|
assert_eq!(u32::lcm(2, 8), 8);
|
|
|
|
assert_eq!(u16::lcm(2, 3), 6);
|
2020-02-14 12:11:57 -05:00
|
|
|
assert_eq!(usize::lcm(15, 30), 30);
|
|
|
|
assert_eq!(u128::lcm(1, 5), 5);
|
2020-02-19 21:10:53 -05:00
|
|
|
assert_eq!(0u8, u8::lcm(0, 0));
|
2020-02-14 12:11:57 -05:00
|
|
|
}
|
2020-02-14 10:14:22 -05:00
|
|
|
}
|