Ternary

Struct Ternary 

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

Represents a balanced ternary number using a sequence of Digits.

Provides functions for creating, parsing, converting, and manipulating balanced ternary numbers.

Implementations§

Source§

impl Ternary

Source

pub fn new(digits: Vec<Digit>) -> Ternary

Creates a new balanced ternary number from a vector of Digits.

Source

pub fn log(&self) -> usize

Returns the number of digits (length) of the balanced ternary number.

Source

pub fn to_digit_slice(&self) -> &[Digit]

Retrieves a slice containing the digits of the Ternary.

§Returns

A slice referencing the digits vec of the Ternary.

This function allows access to the raw representation of the balanced ternary number as a slice of Digit values.

Source

pub fn iter(&self) -> Iter<'_, Digit>

Returns an iterator over the digits from most significant to least significant.

Source

pub fn get_digit(&self, index: usize) -> Option<&Digit>

Returns a reference to the Digit indexed by index if it exists.

Digits are indexed from the right:

use balanced_ternary::Ternary;

// Indexes :
//                              32
//                             4||1
//                            5||||0
//                            ||||||
//                            vvvvvv
let ternary = Ternary::parse("+++--+");
assert_eq!(ternary.get_digit(1).unwrap().to_char(), '-')
Source

pub fn parse(str: &str) -> Self

Parses a string representation of a balanced ternary number into a Ternary object.

Each character in the string must be one of +, 0, or -.

§Example
use balanced_ternary::Ternary;

let ternary = "+-0".parse::<Ternary>().unwrap();
assert_eq!(ternary.to_string(), "+-0");
Source

pub fn to_dec(&self) -> i64

Converts the Ternary object to its integer (decimal) representation.

Calculates the sum of each digit’s value multiplied by the appropriate power of 3.

Source

pub fn from_dec(dec: i64) -> Self

Creates a balanced ternary number from a decimal integer.

The input number is converted into its balanced ternary representation, with digits represented as Digits.

Source

pub fn to_unbalanced(&self) -> String

Converts the balanced ternary number to its unbalanced representation as a string.

The unbalanced representation treats the digits as standard ternary (0, 1, 2), instead of balanced ternary (-1, 0, +1). Negative digits are handled by calculating the decimal value of the balanced ternary number and converting it back to an unbalanced ternary string.

Returns:

  • String - The unbalanced ternary representation of the number, where each digit is one of 0, 1, or 2.

Example:

use balanced_ternary::Ternary;

let repr = Ternary::parse("+--");
assert_eq!(repr.to_unbalanced(), "12");
assert_eq!(repr.to_dec(), 5);
let repr = Ternary::parse("-++");
assert_eq!(repr.to_unbalanced(), "-12");
assert_eq!(repr.to_dec(), -5);
Source

pub fn from_unbalanced(unbalanced: &str) -> Self

Parses a string representation of an unbalanced ternary number into a Ternary object.

The string must only contain characters valid in the unbalanced ternary numeral system (0, 1, or 2). Each character is directly converted into its decimal value and then interpreted as a balanced ternary number.

§Arguments
  • unbalanced - A string slice representing the unbalanced ternary number.
§Returns

A Ternary object representing the same value as the input string in balanced ternary form.

§Panics

This function will panic if the string is not a valid unbalanced ternary number. For instance, if it contains characters other than 0, 1, or 2.

§Examples
use balanced_ternary::Ternary;

let ternary = Ternary::from_unbalanced("-12");
assert_eq!(ternary.to_string(), "-++");
assert_eq!(ternary.to_dec(), -5);
Source

pub fn trim(&self) -> Self

Removes leading Zero digits from the Ternary number, effectively trimming it down to its simplest representation. The resulting Ternary number will still represent the same value.

§Returns
  • Self - A new Ternary object, trimmed of leading zeros.
§Examples
use balanced_ternary::{ Neg, Pos, Ternary, Zero};

let ternary = Ternary::new(vec![Zero, Zero, Pos, Neg]);
let trimmed = ternary.trim();
assert_eq!(trimmed.to_string(), "+-");
§Notes

This method does not mutate the original Ternary object but returns a new representation.

Source

pub fn with_length(&self, length: usize) -> Self

Adjusts the representation of the Ternary number to have a fixed number of digits.

If the current Ternary has fewer digits than the specified length, leading zero digits will be added to the Ternary to match the desired length. If the current Ternary has more digits than the specified length, it will be returned unmodified.

§Arguments
  • length - The desired length of the Ternary number.
§Returns
  • Self - A new Ternary object with the specified fixed length.
§Notes

If length is smaller than the existing number of digits, the function does not truncate the number but instead returns the original Ternary unchanged.

§Examples
use balanced_ternary::{Ternary, Zero, Pos};

let ternary = Ternary::new(vec![Pos]);
let fixed = ternary.with_length(5);
assert_eq!(fixed.to_string(), "0000+");

let fixed = ternary.with_length(1);
assert_eq!(fixed.to_string(), "+");
Source

pub fn to_string_repr<F: Fn(&Digit) -> char>(&self, transform: F) -> String

Converts the Ternary number into a string representation by applying a given transformation function to each digit of the ternary number.

§Arguments
  • transform - A function or closure that takes a Digit and returns a char, representing the digit.
§Returns

A String-based representation of the Ternary number resulting from applying the transformation to its digits.

§Examples
use balanced_ternary::{Digit, Pos, Neg, Zero, Ternary};

let ternary = Ternary::new(vec![Pos, Zero, Neg]);

let custom_repr = ternary.to_string_repr(Digit::to_char_t);
assert_eq!(custom_repr, "10T");
let custom_repr = ternary.to_string_repr(Digit::to_char_theta);
assert_eq!(custom_repr, "10Θ");
let custom_repr = ternary.to_string_repr(Digit::to_char);
assert_eq!(custom_repr, "+0-");
§Notes
  • The function provides flexibility to define custom string representations for the ternary number digits.
  • Call to Ternary::to_string() is equivalent to Ternary::to_string_repr(Digit::to_char).
Source

pub fn concat(&self, other: &Ternary) -> Ternary

Concatenates the current Ternary number with another Ternary number.

This function appends the digits of the provided Ternary object to the digits of the current Ternary object, creating a new Ternary number as the result.

§Arguments
  • other - A reference to the Ternary number to be concatenated to the current one.
§Returns
  • Ternary - A new Ternary object formed by concatenating the digits.
§Examples
use balanced_ternary::{Ternary, Pos, Zero, Neg};

let ternary1 = Ternary::new(vec![Pos, Zero]);
let ternary2 = Ternary::new(vec![Neg, Pos]);

let concatenated = ternary1.concat(&ternary2);
assert_eq!(concatenated.to_string(), "+0-+");

Trait Implementations§

Source§

impl Add<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Digit> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl BitAnd<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &Ternary) -> Self::Output

Performs the & operation. Read more
Source§

impl BitOr<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Ternary) -> Self::Output

Performs the | operation. Read more
Source§

impl BitXor<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &Ternary) -> Self::Output

Performs the ^ operation. Read more
Source§

impl Clone for Ternary

Source§

fn clone(&self) -> Ternary

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 Ternary

Source§

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

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

impl DigitOperate for Ternary

Source§

fn to_digits(&self) -> Vec<Digit>

Returns every individual Digit of this DigitOperate object.
Source§

fn digit(&self, index: usize) -> Option<Digit>

Returns one individual Digit ot this DigitOperate object if it exists.
Source§

fn each(&self, f: impl Fn(Digit) -> Digit) -> Self

Applies a transformation function to each digit of the balanced ternary number, returning a new DigitOperate object with the transformed digits. Read more
Source§

fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, other: Digit) -> Self

Applies a transformation function to each digit of the balanced ternary number, using an additional parameter for the transformation process, returning a new DigitOperate object with the transformed digits. Read more
Source§

fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self

Applies a transformation function to each digit of the balanced ternary number, along with a corresponding digit from another DigitOperate number. Read more
Source§

fn each_zip_carry( &self, f: impl Fn(Digit, Digit, Digit) -> (Digit, Digit), other: Self, ) -> Self

Applies a transformation function to each digit of the balanced ternary number, along with a corresponding digit from another DigitOperate number, and a carry digit. Read more
Source§

impl Display for Ternary

Source§

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

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

impl Div<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl From<&str> for Ternary

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<DataTernary> for Ternary

Source§

fn from(value: DataTernary) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Ternary

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<Ter40> for Ternary

Source§

fn from(value: Ter40) -> Self

Converts to this type from the input type.
Source§

impl From<Ternary> for DataTernary

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl From<Ternary> for String

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl From<Ternary> for Ter40

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Ternary> for Tryte<SIZE>

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl From<Ternary> for i64

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Tryte<SIZE>> for Ternary

Source§

fn from(value: Tryte<SIZE>) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Ternary

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Ternary

Source§

type Err = ParseTernaryError

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 Hash for Ternary

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoIterator for Ternary

Source§

type Item = Digit

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Digit>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Mul<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Neg for &Ternary

Source§

fn neg(self) -> Self::Output

Returns the negation of the current Ternary object.

Negates each digit in the number.

Source§

type Output = Ternary

The resulting type after applying the - operator.
Source§

impl Not for &Ternary

Source§

type Output = Ternary

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Ord for Ternary

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,

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

impl PartialEq for Ternary

Source§

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

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

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 Shl<usize> for &Ternary

Source§

type Output = Ternary

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

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

Performs the << operation. Read more
Source§

impl Shr<usize> for &Ternary

Source§

type Output = Ternary

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

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

Performs the >> operation. Read more
Source§

impl Sub<&Ternary> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Digit> for &Ternary

Source§

type Output = Ternary

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Eq for Ternary

Source§

impl StructuralPartialEq for Ternary

Auto Trait Implementations§

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.