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
impl Ternary
Sourcepub fn new(digits: Vec<Digit>) -> Ternary
pub fn new(digits: Vec<Digit>) -> Ternary
Creates a new balanced ternary number from a vector of Digits.
Sourcepub fn log(&self) -> usize
pub fn log(&self) -> usize
Returns the number of digits (length) of the balanced ternary number.
Sourcepub fn to_digit_slice(&self) -> &[Digit]
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.
Sourcepub fn get_digit(&self, index: usize) -> Option<&Digit>
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(), '-')Sourcepub fn parse(str: &str) -> Self
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 -.
Sourcepub fn to_dec(&self) -> i64
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.
Sourcepub fn from_dec(dec: i64) -> Self
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.
Sourcepub fn to_unbalanced(&self) -> String
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 of0,1, or2.
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);Sourcepub fn from_unbalanced(unbalanced: &str) -> Self
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);