Digits

Struct Digits 

Source
pub struct Digits { /* private fields */ }
Expand description

This struct acts similar to a full number with a custom numeric character base which is provided and mapped via a BaseCustom instance.

The underlying implementation for Digits is a linked list where all the methods recurse as far as need to to implement the operations.

Implementations§

Source§

impl Digits

Source

pub fn add(&self, other: Self) -> Self

Add two Digits instances together.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let eleven = Digits::new(base10.clone(), "11".to_string());
let two = Digits::new(base10, "2".to_string());

assert_eq!(eleven.add(two).to_s(), "13");
§Output
"13"

This will panic if numeric bases are not the same.

Source

pub fn as_mapping_vec(&self) -> Vec<u64>

Returns a vector of each characters position mapping

Source

pub fn base(&self) -> usize

Make numeric base size publicly available on Digits

Source

pub fn gen<T>(&self, other: T) -> Self
where Self: From<(BaseCustom<char>, T)>,

Allows you to generate/encode a Digits from a u64 or other Digits even if they are of a different numeric base.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let two = Digits::new(base10, "2".to_string());
let three = two.gen(3_u64);

assert_eq!(three.to_s(), "3");
Source

pub fn is_valid_adjacent(&self, adjacent: usize) -> bool

Returns true of false based on whether the limit of allowed adjacents is not exceeded. Early termination result when false.

Same as being a more efficient self.max_adjacent <= allowed_adjacent.

Source

pub fn is_compat(&self, other: &Self) -> bool

Returns whether the two Digits instances have the same numeric base and character mapping.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let two = Digits::new(base10.clone(), "2".to_string());
let three = Digits::new(base10, "3".to_string());

assert!(two.is_compat(&three));
Source

pub fn is_one(&self) -> bool

Returns bool value of if the number is one.

Source

pub fn is_zero(&self) -> bool

Returns bool value of if the number is zero.

Source

pub fn length(&self) -> usize

Returns a usize of the total linked list length.

Source

pub fn max_adjacent(&self) -> usize

Give the count for the maximum of the same adjacent characters for this digit.

Note that adjacent is a non-inclusive count. So for 7 numbers it’s 1 adjacent to 6 which will return 6.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let num = Digits::new(base10, "557771".to_string());

assert_eq!(num.max_adjacent(), 2);

The above example demonstrates that there are 2 adjacent 7s next to a 7 and that is the biggest adjacent set of numbers.

Source

pub fn mul(&self, other: Self) -> Self

Multiply two Digits instances together.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let eleven = Digits::new(base10.clone(), "11".to_string());
let two = Digits::new(base10, "2".to_string());

assert_eq!(eleven.mul(two).to_s(), "22");
§Output
"22"

This will panic if numeric bases are not the same.

Source

pub fn mut_add(&mut self, other: Self) -> Self

Add two Digits instances together.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let mut eleven = Digits::new(base10.clone(), "11".to_string());
let two = Digits::new(base10, "2".to_string());

assert_eq!(eleven.mut_add(two).to_s(), "13");
§Output
"13"

This will panic if numeric bases are not the same.

Source

pub fn mut_mul(&mut self, other: Self) -> Self

Multiply two Digits instances together.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let mut eleven = Digits::new(base10.clone(), "11".to_string());
let two = Digits::new(base10, "2".to_string());

assert_eq!(eleven.mut_mul(two).to_s(), "22");
§Output
"22"

This will panic if numeric bases are not the same.

Source

pub fn new<S>(mapping: BaseCustom<char>, number: S) -> Digits
where S: Into<String>,

Creates a new Digits instance with the provided character set and value.

The first parameter must be a BaseCustom object which defines and maps all values. The second parameter is a string value with all valid characters from the BaseCustom set.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let nine = Digits::new(base10, "9".to_string());

assert_eq!(nine.to_s(), "9");
Source

pub fn new_mapped(&self, places: &[u64]) -> Result<Self, &'static str>

Create a Digits from a Vector of from zero positional mappings for custom Digits numeric base.

§Example
use digits::prelude::*;

let base16 = BaseCustom::<char>::new("0123456789abcdef".chars().collect());
let builder = Digits::new(base16, "".to_string());
let num = builder.new_mapped(&vec![1,0,2,1]).ok().unwrap();

assert_eq!(num.to_s(), "1021");

If zero had been Z in the example above the same vector vec![1,0,2,1] would have produced a Digits instance of a Hex value of “1Z21”. The vector is the litteral positional map of the character(s) via an index from zero regardless of numeric base.

If a number provided within the vector is higher than the numeric base size then the method will return an Err(&'static str) Result.

Source

pub fn new_one(mapping: BaseCustom<char>) -> Self

Creates a new Digits instance with value of one and the provided character mapping.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let one = Digits::new_one(base10);

assert_eq!(one.to_s(), "1");
Source

pub fn new_zero(mapping: BaseCustom<char>) -> Self

Creates a new Digits instance with value of zero and uses the provided character mapping.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let zero = Digits::new_zero(base10);

assert_eq!(zero.to_s(), "0");
Source

pub fn next_non_adjacent(&mut self, adjacent: usize) -> Self

Returns the next Digits in incrementing that only allows the given number of adjacent number duplicates.

This will panic! if numeric base is less than 4.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let mut num = Digits::new(base10, "98".to_string());

assert_eq!(num.next_non_adjacent(0).to_s(), "101");
Source

pub fn one(&self) -> Self

Creates a new Digits instance with value of one and uses the current character mapping.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let nine = Digits::new(base10, "9".to_string());
let one = nine.one();

assert_eq!(one.to_s(), "1");
Source

pub fn pinky(&self) -> char

The “pinky” is the smallest digit a.k.a. current digit in the linked list a.k.a. the right most digit. This will be a char value for that digit.

Source

pub fn pow(&mut self, pwr: Self) -> Self

Multiplies self times the power-of given Digits parameter.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());

let mut eleven = Digits::new(base10.clone(), "11".to_string());
let two = Digits::new(base10, "2".to_string());

assert_eq!(eleven.pow(two).to_s(), "121");
§Output
"121"
Source

pub fn pred_till_zero(&mut self) -> Self

Minuses one unless it’s zero, then it just returns a Digits instance of zero.

Source

pub fn prep_non_adjacent(&mut self, adjacent: usize) -> Self

Sometimes given starting Digits have more adjacent characters than is desired when proceeding with non-adjacent steps. This method provides a valid initial state for step_non_adjacent’s algorithm to not miss any initial steps.

_This method is used internally for next_non_adjacent.

This will panic! if numeric base is less than 4.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let mut num = Digits::new(base10, "0003".to_string());

assert_eq!(num.prep_non_adjacent(1).to_s(), "0009");

In the example above the prep moves to a valid state of “0010” and then minuses one to “0009” so that step_non_adjacent will add 1 and return to the valid state of “0010” for this one-adjacent scenario.

For performance in your own applications use this method once and continue iterating with step_non_adjacent.

For convenience you may just use next_non_adjacent instead of prep and step.

Source

pub fn propagate<S>(&self, number: S) -> Self
where S: Into<String>,

Creates a new Digits instance with the internal character set and given value.

The parameter is a string value with all valid characters from the BaseCustom set.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let nine = Digits::new(base10, "9".to_string());
let forty_two = nine.propagate("42".to_string());

assert_eq!(forty_two.to_s(), "42");
Source

pub fn rcount(&self, character_index: u8) -> usize

Right count of digits character index.

Returns a usize of how many Digits values from the right match the BaseCustom index given for number.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("ABC3456789".chars().collect());
let num = Digits::new(base10, "34BBB".to_string());

assert_eq!(num.rcount(1), 3);
§Output
3
Source

pub fn replicate(self) -> Self

An alias for clone. Useful for unboxing.

Source

pub fn step_non_adjacent(&mut self, adjacent: usize) -> Self

Returns the next Digits in incrementing that only allows the given number of adjacent number duplicates.

This will panic! if numeric base is less than 4.

NOTE: This assumes the starting state is valid for given non adjacent characters. If you want to ensure this please use prep_adjacent before this, or just use next_non_adjacent to handle them both.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let mut num = Digits::new(base10, "98".to_string());

assert_eq!(num.step_non_adjacent(0).to_s(), "101");
Source

pub fn succ(&mut self) -> Self

Plus one.

Source

pub fn to_s(&self) -> String

Gives the full value of all digits within the linked list as a String.

Source

pub fn to_string(&self) -> String

Gives the full value of all digits within the linked list as a String.

Source

pub fn zero(&self) -> Self

Creates a new Digits instance with value of zero and the current character mapping.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let nine = Digits::new(base10, "9".to_string());
let zero = nine.zero();

assert_eq!(zero.to_s(), "0");
Source

pub fn zero_fill(&mut self, length: usize)

Zero fills the left of the current number up to a total character length.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let mut nine = Digits::new(base10, "9".to_string());
nine.zero_fill(4);

assert_eq!(nine.to_s(), "0009");
Source

pub fn zero_trim(&mut self)

Zero trims the left of the current number.

§Example
use digits::prelude::*;

let base10 = BaseCustom::<char>::new("0123456789".chars().collect());
let mut nine = Digits::new(base10, "0009".to_string());
nine.zero_trim();

assert_eq!(nine.to_s(), "9");

Trait Implementations§

Source§

impl Add for Digits

Source§

type Output = Digits

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Digits

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl BitXor for Digits

Source§

type Output = Digits

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: Self) -> Self

Performs the ^ operation. Read more
Source§

impl BitXorAssign for Digits

Source§

fn bitxor_assign(&mut self, other: Self)

Performs the ^= operation. Read more
Source§

impl Clone for Digits

Source§

fn clone(&self) -> Digits

Returns a duplicate 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 Debug for Digits

Source§

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

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

impl Default for Digits

Source§

fn default() -> Digits

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

impl Display for Digits

Source§

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

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

impl From<(BaseCustom<char>, Digits)> for Digits

Source§

fn from(d: (BaseCustom<char>, Digits)) -> Digits

Converts to this type from the input type.
Source§

impl From<(BaseCustom<char>, u64)> for Digits

Source§

fn from(d: (BaseCustom<char>, u64)) -> Digits

Converts to this type from the input type.
Source§

impl From<(Digits, Digits)> for Digits

Source§

fn from(d: (Digits, Digits)) -> Digits

Converts to this type from the input type.
Source§

impl From<Digits> for String

Source§

fn from(d: Digits) -> String

Converts to this type from the input type.
Source§

impl Into<String> for Digits

Source§

fn into(self) -> String

Source§

impl Mul for Digits

Source§

type Output = Digits

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign for Digits

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl PartialEq for Digits

Source§

fn eq(&self, other: &Digits) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Digits

Source§

fn partial_cmp(&self, other: &Digits) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Radix for Digits

Source§

fn binary(&self) -> Digits

Convert current Digits to binary
Source§

fn octal(&self) -> Digits

Convert current Digits to octal
Source§

fn decimal(&self) -> Digits

Convert current Digits to decimal
Source§

fn hex(&self) -> Digits

Convert current Digits to hexadecimal
Source§

fn hexl(&self) -> Digits

Convert current Digits to lowercase hexadecimal
Source§

impl Reverse for Digits

Source§

fn reverse(&mut self)

Example Read more

Auto Trait Implementations§

§

impl Freeze for Digits

§

impl RefUnwindSafe for Digits

§

impl Send for Digits

§

impl Sync for Digits

§

impl Unpin for Digits

§

impl UnwindSafe for Digits

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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§

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>,

Source§

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>,

Source§

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.