Eliminate clippy warnings
Some checks failed
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit
Some checks failed
timw4mail/rusty-numbers/pipeline/head There was a failure building this commit
This commit is contained in:
parent
9f8e57f3d3
commit
65ffb15c1d
@ -14,7 +14,7 @@ use alloc::vec::*;
|
||||
use std::prelude::v1::*;
|
||||
|
||||
use core::cmp::{Ordering, PartialEq, PartialOrd};
|
||||
use core::convert::*;
|
||||
use core::convert::TryInto;
|
||||
use core::mem::replace;
|
||||
use core::ops::{
|
||||
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
|
||||
@ -27,7 +27,7 @@ pub struct BigInt {
|
||||
sign: Sign,
|
||||
}
|
||||
|
||||
/// Create a [BigInt](bigint/struct.BigInt.html) type with signed or unsigned number literals
|
||||
/// Create a [`BigInt`](bigint/struct.BigInt.html) type with signed or unsigned number literals
|
||||
#[macro_export]
|
||||
macro_rules! big_int {
|
||||
($w:literal) => {
|
||||
@ -64,7 +64,7 @@ impl From<&str> for BigInt {
|
||||
|
||||
impl From<String> for BigInt {
|
||||
fn from(s: String) -> Self {
|
||||
Self::from_str_radix(s, 10)
|
||||
Self::from_str_radix(&s, 10)
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,6 +72,7 @@ impl BigInt {
|
||||
/// Create a new Bigint, of value 0
|
||||
///
|
||||
/// The various `From` implementations are more useful in most cases
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@ -83,7 +84,7 @@ impl BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new BigInt, with the specified inner capacity
|
||||
/// Create a new `BigInt`, with the specified inner capacity
|
||||
#[must_use]
|
||||
pub fn with_capacity(size: usize) -> Self {
|
||||
Self {
|
||||
@ -101,7 +102,7 @@ impl BigInt {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut trailing_zeros = 0usize;
|
||||
let mut trailing_zeros = 0_usize;
|
||||
for val in self.inner.iter().rev() {
|
||||
if *val != 0 {
|
||||
break;
|
||||
@ -135,7 +136,12 @@ impl BigInt {
|
||||
///
|
||||
/// Only alphanumeric characters are considered, so exponents and
|
||||
/// other forms are not parsed
|
||||
pub fn from_str_radix<T: ToString>(s: T, radix: usize) -> BigInt {
|
||||
///
|
||||
/// # Panics
|
||||
/// * If radix is not between 1 and 36 inclusive
|
||||
/// * Some branches are not yet implemented
|
||||
pub fn from_str_radix<T: ToString + ?Sized>(s: &T, radix: usize) -> BigInt {
|
||||
// Two lines due to borrow checker complaints
|
||||
let input = s.to_string().to_ascii_uppercase();
|
||||
let input = input.trim();
|
||||
|
||||
@ -159,9 +165,8 @@ impl BigInt {
|
||||
}
|
||||
|
||||
// If the number fits in a usize, try the easy way
|
||||
let easy_res = usize::from_str_radix(input, radix as u32);
|
||||
if easy_res.is_ok() {
|
||||
return BigInt::from(easy_res.unwrap());
|
||||
if let Ok(easy_res) = usize::from_str_radix(input, radix.try_into().unwrap()) {
|
||||
return BigInt::from(easy_res);
|
||||
}
|
||||
|
||||
// TODO: consider parsing out the error, to tell if the
|
||||
@ -170,6 +175,7 @@ impl BigInt {
|
||||
// Convert each digit to it's decimal representation
|
||||
let mut raw_digits: Vec<usize> = Vec::with_capacity(input.len());
|
||||
for maybe_digit in input.chars() {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
match maybe_digit.to_digit(radix as u32) {
|
||||
Some(d) => raw_digits.push(d as usize),
|
||||
None => continue,
|
||||
@ -197,7 +203,7 @@ impl BigInt {
|
||||
}
|
||||
|
||||
/// Determine the output sign given the two input signs and operation
|
||||
fn get_sign(a: Self, b: Self, op: FracOp) -> Sign {
|
||||
fn get_sign(a: &Self, b: &Self, op: FracOp) -> Sign {
|
||||
// -a + -b = -c
|
||||
if op == FracOp::Addition && a.sign == Negative && b.sign == Negative {
|
||||
return Negative;
|
||||
@ -208,21 +214,21 @@ impl BigInt {
|
||||
return Positive;
|
||||
}
|
||||
|
||||
if a.sign != b.sign {
|
||||
Negative
|
||||
} else {
|
||||
if a.sign == b.sign {
|
||||
Positive
|
||||
} else {
|
||||
Negative
|
||||
}
|
||||
}
|
||||
|
||||
/// Normal primitive multiplication
|
||||
fn prim_mul(self, rhs: Self, digits: usize) -> Self {
|
||||
fn prim_mul(self, rhs: &Self, digits: usize) -> Self {
|
||||
let mut out = BigInt::with_capacity(digits);
|
||||
|
||||
let mut carry = 0usize;
|
||||
let mut carry = 0_usize;
|
||||
for i in 0..digits {
|
||||
let a = *self.inner.get(i).unwrap_or(&0usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0usize);
|
||||
let a = *self.inner.get(i).unwrap_or(&0_usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0_usize);
|
||||
|
||||
if a == 0 || b == 0 {
|
||||
out.inner.push(0);
|
||||
@ -241,7 +247,7 @@ impl BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
out.sign = Self::get_sign(self, rhs, FracOp::Other);
|
||||
out.sign = Self::get_sign(&self, rhs, FracOp::Other);
|
||||
out.shrink_to_fit();
|
||||
|
||||
out
|
||||
@ -264,10 +270,10 @@ impl Add for BigInt {
|
||||
let digits = Self::get_ceil_digit_count(&self, &rhs) + 1;
|
||||
let mut out = BigInt::with_capacity(digits);
|
||||
|
||||
let mut carry = 0usize;
|
||||
let mut carry = 0_usize;
|
||||
for i in 0..digits {
|
||||
let a = *self.inner.get(i).unwrap_or(&0usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0usize);
|
||||
let a = *self.inner.get(i).unwrap_or(&0_usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0_usize);
|
||||
|
||||
let (res, overflowed) = a.overflowing_add(b);
|
||||
if res == 0 && !overflowed {
|
||||
@ -283,12 +289,12 @@ impl Add for BigInt {
|
||||
out.inner.push(res + carry);
|
||||
carry = 0;
|
||||
} else {
|
||||
out.inner.push(0usize);
|
||||
out.inner.push(0_usize);
|
||||
carry = 1;
|
||||
}
|
||||
}
|
||||
|
||||
out.sign = Self::get_sign(self, rhs, FracOp::Addition);
|
||||
out.sign = Self::get_sign(&self, &rhs, FracOp::Addition);
|
||||
|
||||
out.trim_zeros();
|
||||
|
||||
@ -311,10 +317,10 @@ impl Sub for BigInt {
|
||||
return -(rhs + -self);
|
||||
}
|
||||
|
||||
let mut borrow = 0usize;
|
||||
let mut borrow = 0_usize;
|
||||
for i in 0..digits {
|
||||
let a = *self.inner.get(i).unwrap_or(&0usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0usize);
|
||||
let a = *self.inner.get(i).unwrap_or(&0_usize);
|
||||
let b = *rhs.inner.get(i).unwrap_or(&0_usize);
|
||||
|
||||
if a >= borrow && (a - borrow) >= b {
|
||||
// This is the easy way, no additional borrowing or underflow
|
||||
@ -336,7 +342,7 @@ impl Sub for BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
out.sign = Self::get_sign(self, rhs, FracOp::Subtraction);
|
||||
out.sign = Self::get_sign(&self, &rhs, FracOp::Subtraction);
|
||||
|
||||
out.trim_zeros();
|
||||
|
||||
@ -354,7 +360,7 @@ impl Mul for BigInt {
|
||||
// Multiplication can result in twice the number of digits
|
||||
let out_digits = Self::get_ceil_digit_count(&self, &rhs) * 2;
|
||||
|
||||
self.prim_mul(rhs, out_digits)
|
||||
self.prim_mul(&rhs, out_digits)
|
||||
}
|
||||
}
|
||||
|
||||
@ -472,11 +478,10 @@ impl PartialOrd for BigInt {
|
||||
// At this point the sign is the same, and the number of place values is equal,
|
||||
// so compare the individual place values (from greatest to least) until they
|
||||
// are different. At this point, the digits can not all be equal.
|
||||
for i in (0usize..self.inner.len()).rev() {
|
||||
if self.inner[i] < other.inner[i] {
|
||||
return Some(Ordering::Less);
|
||||
} else if self.inner[i] > other.inner[i] {
|
||||
return Some(Ordering::Greater);
|
||||
for i in (0_usize..self.inner.len()).rev() {
|
||||
let compare = self.inner[i].cmp(&other.inner[i]);
|
||||
if compare != Ordering::Equal {
|
||||
return Some(compare);
|
||||
}
|
||||
}
|
||||
|
||||
@ -693,7 +698,7 @@ mod tests {
|
||||
let a = BigInt::from(core::usize::MAX);
|
||||
let b = BigInt::from(1usize);
|
||||
let sum = a + b;
|
||||
assert_eq!(sum.inner[0], 0usize);
|
||||
assert_eq!(sum.inner[0], 0_usize);
|
||||
assert_eq!(sum.inner[1], 1usize);
|
||||
|
||||
let a = BigInt::from(10);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![no_std]
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
#![allow(clippy::similar_names)]
|
||||
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
#[macro_use]
|
||||
|
@ -97,7 +97,11 @@ impl<T: Unsigned> Frac<T> {
|
||||
let numerator = n.to_unsigned();
|
||||
let denominator = d.to_unsigned();
|
||||
|
||||
Frac { numerator, denominator, sign }
|
||||
Frac {
|
||||
numerator,
|
||||
denominator,
|
||||
sign,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new rational from all the raw parts
|
||||
@ -129,10 +133,10 @@ impl<T: Unsigned> Frac<T> {
|
||||
return Positive;
|
||||
}
|
||||
|
||||
if a.sign != b.sign {
|
||||
Negative
|
||||
} else {
|
||||
if a.sign == b.sign {
|
||||
Positive
|
||||
} else {
|
||||
Negative
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,7 +281,8 @@ impl<T: Unsigned> Sub for Frac<T> {
|
||||
}
|
||||
|
||||
if a.denominator != b.denominator {
|
||||
let (numerator, overflowed) = (a.numerator * b.denominator).left_overflowing_sub(b.numerator * a.denominator);
|
||||
let (numerator, overflowed) =
|
||||
(a.numerator * b.denominator).left_overflowing_sub(b.numerator * a.denominator);
|
||||
|
||||
let denominator = a.denominator * b.denominator;
|
||||
let sign = Self::get_sign(a, b, FracOp::Subtraction);
|
||||
|
Loading…
Reference in New Issue
Block a user