pub struct TritsChunk(/* private fields */);Expand description
A struct to store 5 ternary digits (~7.8 bits) value into one byte.
TritsChunks helps store ternary numbers into a compact memory structure.
From 0 to ± 121.
Implementations§
Source§impl TritsChunk
impl TritsChunk
Sourcepub fn from_dec(from: i8) -> Self
pub fn from_dec(from: i8) -> Self
Creates a TritsChunk from a given decimal value.
§Arguments
from- Ani8value representing the decimal value to be converted into aTritsChunk.
§Panics
This function panics if the input value is out of the valid range -121..=121.
§Example
use balanced_ternary::TritsChunk;
let chunk = TritsChunk::from_dec(42);
assert_eq!(chunk.to_dec(), 42);Sourcepub fn to_ternary(&self) -> Ternary
pub fn to_ternary(&self) -> Ternary
Sourcepub fn to_fixed_ternary(&self) -> Ternary
pub fn to_fixed_ternary(&self) -> Ternary
Converts the TritsChunk into its fixed-length ternary representation.
§Returns
A Ternary type representing the 5-digit fixed-length ternary form of the TritsChunk.
§Example
use balanced_ternary::{TritsChunk, Ternary};
let chunk = TritsChunk::from_dec(42);
let fixed_ternary = chunk.to_fixed_ternary();
assert_eq!(fixed_ternary.to_dec(), 42);
assert_eq!(fixed_ternary.to_digit_slice().len(), 5);Sourcepub fn to_digits(&self) -> Vec<Digit>
pub fn to_digits(&self) -> Vec<Digit>
Converts the TritsChunk into a vector of its individual ternary digits.
§Returns
A Vec<Digit> representing the individual ternary digits of the TritsChunk.
The resulting vector will always contain 5 digits since the TritsChunk is
represented in a fixed-length ternary form.
§Example
use balanced_ternary::{TritsChunk, Digit};
let chunk = TritsChunk::from_dec(42);
let digits: Vec<Digit> = chunk.to_digits();
assert_eq!(digits.len(), 5);Sourcepub fn from_ternary(ternary: Ternary) -> Self
pub fn from_ternary(ternary: Ternary) -> Self
Creates a TritsChunk from a given Ternary value.
§Arguments
ternary- ATernaryvalue to be converted into aTritsChunk.
§Panics
This function panics if the provided ternary value has a logarithmic length greater than 5,
indicating that it cannot be represented by a single TritsChunk.
§Example
use balanced_ternary::{TritsChunk, Ternary};
let ternary = Ternary::from_dec(42);
let chunk = TritsChunk::from_ternary(ternary);
assert_eq!(chunk.to_dec(), 42);Trait Implementations§
Source§impl Clone for TritsChunk
impl Clone for TritsChunk
Source§fn clone(&self) -> TritsChunk
fn clone(&self) -> TritsChunk
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more