pub struct Tryte<const SIZE: usize = 6> { /* private fields */ }Expand description
The Tryte<S> struct represents a Copy type balanced ternary number with exactly S digits (6 by default).
Each digit in a balanced ternary system can have one of three values: -1, 0, or 1.
A Tryte<6> can holds value between -364 and +364.
The underlying representation of the number is an array of SIZE Digit values.
This struct provides conversion methods to and from other formats.
§Default SIZE
SIZE is 6 by default (the size of a tryte in a Setun computer).
6 trits ~= 9.505 bits
-364to364
§Warning
Because arithmetic operations are performed in with 64 bits integers, SIZE cannot be > 40.
40 trits ~= 63.398 bits
-6 078 832 729 528 464 400to6 078 832 729 528 464 400
Implementations§
Source§impl<const SIZE: usize> Tryte<SIZE>
impl<const SIZE: usize> Tryte<SIZE>
Sourcepub const fn new(digits: [Digit; SIZE]) -> Self
pub const fn new(digits: [Digit; SIZE]) -> Self
Creates a new Tryte instance from a given array of Digits.
§Arguments
raw- An array of exactly SIZEDigitvalues representing the balanced ternary digits.
§Returns
A new Tryte instance with the specified balanced ternary digits.
§Panics
Panic if SIZE > 40 as 41 trits would be too much information for 64 bits.
§Examples
use balanced_ternary::{Tryte, Digit::{Pos, Zero, Neg}};
let digits = [Pos, Zero, Neg, Zero, Pos, Neg];
let tryte = Tryte::new(digits);
assert_eq!(tryte.to_digit_slice(), &digits);Sourcepub fn to_ternary(&self) -> Ternary
pub fn to_ternary(&self) -> Ternary
Converts the Tryte into its Ternary representation.
§Returns
A Ternary object representing the same balanced ternary number.
Sourcepub const fn to_digit_slice(&self) -> &[Digit]
pub const fn to_digit_slice(&self) -> &[Digit]
Retrieves a slice containing the digits of the Tryte.
§Returns
A slice referencing the six-digit array of the Tryte.
This function allows access to the raw representation of the
balanced ternary number as a slice of Digit values.
Sourcepub fn from_ternary(v: &Ternary) -> Self
pub fn from_ternary(v: &Ternary) -> Self
Trait Implementations§
Source§impl<const SIZE: usize> DigitOperate for Tryte<SIZE>
impl<const SIZE: usize> DigitOperate for Tryte<SIZE>
Source§fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, with: Digit) -> Self
fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, with: Digit) -> Self
See Ternary::each_with.
Source§fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self
fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self
See Ternary::each_zip.