pub struct Ternary { /* private fields */ }
Expand description
Represents a balanced ternary number using a sequence of Digit
s.
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 Digit
s.
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 iter(&self) -> Iter<'_, Digit>
pub fn iter(&self) -> Iter<'_, Digit>
Returns an iterator over the digits from most significant to least significant.
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 -
.
§Example
use balanced_ternary::Ternary;
let ternary = "+-0".parse::<Ternary>().unwrap();
assert_eq!(ternary.to_string(), "+-0");
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 Digit
s.
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);
Sourcepub fn trim(&self) -> Self
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 newTernary
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.
Sourcepub fn with_length(&self, length: usize) -> Self
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 theTernary
number.
§Returns
Self
- A newTernary
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(), "+");
Sourcepub fn to_string_repr<F: Fn(&Digit) -> char>(&self, transform: F) -> String
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 aDigit
and returns achar
, 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 toTernary::to_string_repr(Digit::to_char)
.
Sourcepub fn concat(&self, other: &Ternary) -> Ternary
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 theTernary
number to be concatenated to the current one.
§Returns
Ternary
- A newTernary
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 DigitOperate for Ternary
impl DigitOperate for Ternary
Source§fn digit(&self, index: usize) -> Option<Digit>
fn digit(&self, index: usize) -> Option<Digit>
Source§fn each(&self, f: impl Fn(Digit) -> Digit) -> Self
fn each(&self, f: impl Fn(Digit) -> Digit) -> Self
DigitOperate
object with the transformed digits. Read moreSource§fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, other: Digit) -> Self
fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, other: Digit) -> Self
DigitOperate
object with the transformed digits. Read more