Struct big_int::BigInt

source ·
pub struct BigInt<const BASE: usize>(/* private fields */);
Expand description

BigInt: represents an arbitrary-size integer in base BASE.

BASE may be anywhere from 2-256. If you would like to be able to represent a larger base than 256, then increase Digit and DoubleDigit as needed, as high as u64 + u128.

Implementations§

source§

impl<const BASE: usize> BigInt<BASE>

source

pub unsafe fn from_raw_parts(digits: Vec<Digit>) -> Self

Create a new BigInt directly from a Vec of individual digits.

Ensure the resulting int is properly normalized, and that no digits are greater than or equal to the base, to preserve soundness.

To construct a negative BigInt from raw parts, simply apply the negation operator (-) afterwards.

use big_int::*;

assert_eq!(
    unsafe { -BigInt::<10>::from_raw_parts(vec![1, 5]) },
    (-15).into()
);
source

pub fn zero() -> Self

The constant zero represented as a BigInt.

source

pub fn display(&self, alphabet: &str) -> Result<String, BigIntError>

Convert a BigInt to a printable string using the provided alphabet alphabet. Display uses this method with the default alphabet STANDARD_ALPHABET.

use big_int::*;

assert_eq!(
    BigInt::<10>::from(6012).display(STANDARD_ALPHABET).unwrap(),
    "6012".to_string()
);
source

pub fn normalized(self) -> Self

Return a normalized version of the BigInt. Remove trailing zeros, and disable the parity flag if the resulting number is zero.

use big_int::*;

let n = unsafe { BigInt::<10>::from_raw_parts(vec![0, 0, 8, 3]) };
assert_eq!(n.normalized(), 83.into());
source

pub fn normalize(&mut self)

Normalize a BigInt in place. Remove trailing zeros, and disable the parity flag if the resulting number is zero.

use big_int::*;

let mut n = unsafe { BigInt::<10>::from_raw_parts(vec![0, 0, 8, 3]) };
n.normalize();
assert_eq!(n, 83.into());
source

pub fn parse(value: &str, alphabet: &str) -> Result<Self, ParseError>

Parse a BigInt from a value: &str, referencing the provided alphabet to determine what characters represent which digits. FromStr uses this method with the default alphabet STANDARD_ALPHABET.

use big_int::*;

assert_eq!(BigInt::parse("125", STANDARD_ALPHABET), Ok(BigInt::<10>::from(125)));
source

pub fn div_rem(self, other: Self) -> Result<(Self, Self), BigIntError>

Divide one BigInt by another, returning the quotient & remainder as a pair, or an error if dividing by zero. This algorithm has a different time complexity than BigInt::div_rem_lowmem which makes it faster for most use cases, but also uses more memory.

b - base
d - number of digits in quotient
Time complexity: O(d * log(b))
Memory complexity: O(d^2)

use big_int::*;

let a: BigInt<10> = 999_999_999.into();
let b = 56_789.into();
assert_eq!(a.div_rem(b), Ok((17_609.into(), 2_498.into())));
source

pub fn div_rem_lowmem(self, other: Self) -> Result<(Self, Self), BigIntError>

Divide one BigInt by another, returning the quotient & remainder as a pair, or an error if dividing by zero.

b - base
d - number of digits in quotient
Time complexity: O(d * b)
Memory complexity: O(d)

use big_int::*;

let a: BigInt<10> = 999_999_999.into();
let b = 56_789.into();
assert_eq!(a.div_rem_lowmem(b), Ok((17_609.into(), 2_498.into())));
source

pub fn convert<const TO: usize>(self) -> BigInt<TO>

Convert a BigInt from its own base to another target base.

use big_int::*;

assert_eq!(
    BigInt::<10>::from(99825).convert(),
    BigInt::<16>::from(99825)
);
source

pub fn convert_lowmem<const TO: usize>(self) -> BigInt<TO>

Convert a BigInt from its own base to another target base using BigInt::div_rem_lowmem. Has lower memory usage, but greater time complexity.

use big_int::*;

assert_eq!(
    BigInt::<10>::from(99825).convert_lowmem(),
    BigInt::<16>::from(99825)
);

Trait Implementations§

source§

impl<const BASE: usize> Add for BigInt<BASE>

§

type Output = BigInt<BASE>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<const BASE: usize> AddAssign for BigInt<BASE>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<const BASE: usize> Clone for BigInt<BASE>

source§

fn clone(&self) -> BigInt<BASE>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const BASE: usize> Debug for BigInt<BASE>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const BASE: usize> Default for BigInt<BASE>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<const BASE: usize> Display for BigInt<BASE>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<const BASE: usize> Div for BigInt<BASE>

§

type Output = BigInt<BASE>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
source§

impl<const BASE: usize> DivAssign for BigInt<BASE>

source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
source§

impl<const BASE: usize> From<BigInt<BASE>> for i128

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for i16

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for i32

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for i64

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for i8

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for isize

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for u128

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for u16

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for u32

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for u64

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for u8

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<BigInt<BASE>> for usize

source§

fn from(value: BigInt<BASE>) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<i128> for BigInt<BASE>

source§

fn from(value: i128) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<i16> for BigInt<BASE>

source§

fn from(value: i16) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<i32> for BigInt<BASE>

source§

fn from(value: i32) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<i64> for BigInt<BASE>

source§

fn from(value: i64) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<i8> for BigInt<BASE>

source§

fn from(value: i8) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<isize> for BigInt<BASE>

source§

fn from(value: isize) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<u128> for BigInt<BASE>

source§

fn from(value: u128) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<u16> for BigInt<BASE>

source§

fn from(value: u16) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<u32> for BigInt<BASE>

source§

fn from(value: u32) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<u64> for BigInt<BASE>

source§

fn from(value: u64) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<u8> for BigInt<BASE>

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> From<usize> for BigInt<BASE>

source§

fn from(value: usize) -> Self

Converts to this type from the input type.
source§

impl<const BASE: usize> FromStr for BigInt<BASE>

§

type Err = BigIntError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<const BASE: usize> Mul for BigInt<BASE>

§

type Output = BigInt<BASE>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<const BASE: usize> MulAssign for BigInt<BASE>

source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
source§

impl<const BASE: usize> Neg for BigInt<BASE>

§

type Output = BigInt<BASE>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<const BASE: usize> Ord for BigInt<BASE>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<const BASE: usize> PartialEq for BigInt<BASE>

source§

fn eq(&self, other: &BigInt<BASE>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const BASE: usize> PartialOrd for BigInt<BASE>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<const BASE: usize> Shl for BigInt<BASE>

source§

fn shl(self, rhs: Self) -> Self::Output

Shifts a BigInt left by multiples of its BASE (not by 2).

§

type Output = BigInt<BASE>

The resulting type after applying the << operator.
source§

impl<const BASE: usize> ShlAssign for BigInt<BASE>

source§

fn shl_assign(&mut self, rhs: Self)

Shifts a BigInt left by multiples of its BASE (not by 2).

source§

impl<const BASE: usize> Shr for BigInt<BASE>

source§

fn shr(self, rhs: Self) -> Self::Output

Shifts a BigInt right by multiples of its BASE (not by 2).

§

type Output = BigInt<BASE>

The resulting type after applying the >> operator.
source§

impl<const BASE: usize> ShrAssign for BigInt<BASE>

source§

fn shr_assign(&mut self, rhs: Self)

Shifts a BigInt right by multiples of its BASE (not by 2).

source§

impl<const BASE: usize> Sub for BigInt<BASE>

§

type Output = BigInt<BASE>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<const BASE: usize> SubAssign for BigInt<BASE>

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl<const BASE: usize> Eq for BigInt<BASE>

source§

impl<const BASE: usize> StructuralEq for BigInt<BASE>

source§

impl<const BASE: usize> StructuralPartialEq for BigInt<BASE>

Auto Trait Implementations§

§

impl<const BASE: usize> RefUnwindSafe for BigInt<BASE>

§

impl<const BASE: usize> Send for BigInt<BASE>

§

impl<const BASE: usize> Sync for BigInt<BASE>

§

impl<const BASE: usize> Unpin for BigInt<BASE>

§

impl<const BASE: usize> UnwindSafe for BigInt<BASE>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.