Add more mysteries
This commit is contained in:
parent
4d15858e45
commit
421d548082
@ -1,44 +1,51 @@
|
||||
//! Arbitrarily large integers
|
||||
use crate::Unsigned;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum BigIntSign {
|
||||
Positive,
|
||||
Negative
|
||||
}
|
||||
//!
|
||||
//! Traits to implement:
|
||||
//! * Add
|
||||
//! * AddAssign
|
||||
//! * Div
|
||||
//! * DivAssign
|
||||
//! * Mul
|
||||
//! * MulAssign
|
||||
//! * Neg
|
||||
//! * Rem
|
||||
//! * RemAssign
|
||||
//! * Sub
|
||||
//! * SubAssign
|
||||
use crate::{Sign, Unsigned};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BigInt {
|
||||
inner: Vec<usize>,
|
||||
sign: BigIntSign,
|
||||
sign: Sign,
|
||||
}
|
||||
|
||||
impl Default for BigInt {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: vec![],
|
||||
sign: BigIntSign::Positive,
|
||||
sign: Sign::Positive,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<T: Unsigned> for BigInt {
|
||||
impl<T: Unsigned> From<T> for BigInt {
|
||||
fn from(n: T) -> Self {
|
||||
let mut new = Self::default();
|
||||
|
||||
if n > usize::max_value() {
|
||||
new.inner = Self.split(n);
|
||||
if n > T::max_value() {
|
||||
new.inner = BigInt::split(n);
|
||||
}
|
||||
|
||||
new
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Unsigned> BigInt {
|
||||
impl BigInt {
|
||||
/// Split an unsigned number into BigInt parts
|
||||
fn split(num: T) -> Vec<usize> {
|
||||
fn split<T: Unsigned>(num: T) -> Vec<usize> {
|
||||
// Pretty easy if you don't actually need to split the value!
|
||||
if num < usize::max_value() {
|
||||
if num < T::max_value() {
|
||||
return vec![T::into()];
|
||||
}
|
||||
|
||||
|
22
src/lib.rs
22
src/lib.rs
@ -1,11 +1,13 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use std::ops::Not;
|
||||
|
||||
pub mod bigint;
|
||||
pub mod rational;
|
||||
pub mod seq;
|
||||
|
||||
/// Dummy trait for implementing generics on unsigned number types
|
||||
pub trait Unsigned {}
|
||||
pub trait Unsigned: PartialEq + PartialOrd {}
|
||||
|
||||
impl Unsigned for u8 {}
|
||||
impl Unsigned for u16 {}
|
||||
@ -14,6 +16,24 @@ impl Unsigned for u64 {}
|
||||
impl Unsigned for usize {}
|
||||
impl Unsigned for u128 {}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum Sign {
|
||||
Positive,
|
||||
Negative
|
||||
}
|
||||
|
||||
impl Not for Sign {
|
||||
type Output = Sign;
|
||||
|
||||
fn not(self) -> Self::Output {
|
||||
match self {
|
||||
Self::Positive => Self::Negative,
|
||||
Self::Negative => Self::Positive,
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
|
@ -1,13 +1,30 @@
|
||||
use crate::Unsigned;
|
||||
//! # Rational Numbers (fractions)
|
||||
//!
|
||||
//! Traits to implement:
|
||||
//! * Add
|
||||
//! * AddAssign
|
||||
//! * Div
|
||||
//! * DivAssign
|
||||
//! * Mul
|
||||
//! * MulAssign
|
||||
//! * Neg
|
||||
//! * Sub
|
||||
//! * SubAssign
|
||||
|
||||
use crate::{Sign, Unsigned};
|
||||
use std::ops::Neg;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum FracType<T: Unsigned = usize> {
|
||||
Proper(T, Frac),
|
||||
Improper(Frac),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Frac<T: Unsigned = usize> {
|
||||
numer: T,
|
||||
denom: T,
|
||||
sign: Sign,
|
||||
}
|
||||
|
||||
impl<T: Unsigned> Frac<T> {
|
||||
@ -16,10 +33,22 @@ impl<T: Unsigned> Frac<T> {
|
||||
Frac {
|
||||
numer: n,
|
||||
denom: d,
|
||||
sign: Sign::Positive,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Unsigned> Neg for Frac<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
let mut out = self.clone();
|
||||
out.sign = !self.sign;
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user