pub struct DataTernary { /* private fields */ }
Expand description
Offers a compact structure to store a ternary number.
- A Ternary is 1 byte long per Digit. An 8 (16, 32, 64) digits ternary number is 8 (16, 32, 64) bytes long.
- A DataTernary is stored into TritsChunk. An 8 (16, 32, 64) digits ternary number with this structure is 2 (4, 7, 13) bytes long (1 byte for 5 digits).
Use the Ternary type to execute operations on numbers and DataTernary to store numbers.
Implementations§
Source§impl DataTernary
impl DataTernary
Sourcepub fn from_ternary(ternary: Ternary) -> Self
pub fn from_ternary(ternary: Ternary) -> Self
Creates a new instance of DataTernary
from a given Ternary
value.
This method ensures that the total number of ternary digits is a multiple of 5
by padding as necessary. It then divides the ternary number into chunks of
5 digits each, which are stored in the DataTernary
structure.
§Arguments
ternary
- ATernary
value to be converted into aDataTernary
.
§Returns
A new DataTernary
instance containing the converted chunks.
§Example
use balanced_ternary::{DataTernary, Ternary};
let ternary = Ternary::from_dec(42);
let data_ternary = DataTernary::from_ternary(ternary);
assert_eq!(data_ternary.to_dec(), 42);
Sourcepub fn to_ternary(&self) -> Ternary
pub fn to_ternary(&self) -> Ternary
Converts a DataTernary
into its equivalent Ternary
representation.
This function iterates over all the TritsChunk
instances in the DataTernary
,
extracts their ternary representations, and reconstructs them into the full
Ternary
value. The resulting Ternary
value may be trimmed to remove
any leading zeroes in its ternary digit representation.
§Returns
A Ternary
value that represents the combined ternary digits of the
DataTernary
.
§Example
use balanced_ternary::{DataTernary, Ternary};
let ternary = Ternary::from_dec(42);
let data_ternary = DataTernary::from_ternary(ternary.clone());
assert_eq!(data_ternary.to_ternary(), ternary);
Sourcepub fn to_fixed_ternary(&self) -> Ternary
pub fn to_fixed_ternary(&self) -> Ternary
Converts the DataTernary
into its fixed-length Ternary
representation.
This method iterates over all the TritsChunk
instances in the DataTernary
and
extracts and combines their ternary digits into a single Ternary
value.
The resulting Ternary
value will contain a fixed number of digits without trimming
or removing leading zeroes.
§Returns
A Ternary
value representing the combined fixed-length ternary digits of the DataTernary
.
§Example
use balanced_ternary::{DataTernary, Ternary};
let ternary = Ternary::from_dec(42);
let data_ternary = DataTernary::from_ternary(ternary);
let fixed_ternary = data_ternary.to_fixed_ternary();
assert_eq!(fixed_ternary.to_dec(), 42); // When properly encoded
Sourcepub fn to_digits(&self) -> Vec<Digit>
pub fn to_digits(&self) -> Vec<Digit>
Converts the DataTernary
into a vector of ternary digits.
This method first converts the DataTernary
structure into its Ternary
representation,
trims any leading zeroes, and then returns the sequence of ternary digits as a Vec<Digit>
.
§Returns
A Vec<Digit>
containing the ternary digits that represent the DataTernary
value.
§Example
use balanced_ternary::{DataTernary, Digit, Ternary};
let ternary = Ternary::from_dec(42);
let data_ternary = DataTernary::from_ternary(ternary);
let digits = data_ternary.to_digits();
assert_eq!(digits, vec![Digit::Pos, Digit::Neg, Digit::Neg, Digit::Neg, Digit::Zero]);
Sourcepub fn from_dec(from: i64) -> Self
pub fn from_dec(from: i64) -> Self
Converts a decimal number into a DataTernary
structure.
This method takes a signed 64-bit integer as input and converts it into a
Ternary
representation, which is then stored in the compact DataTernary
structure. The conversion ensures that the ternary representation uses
fixed-length chunks for efficient storage.
§Arguments
from
- A signed 64-bit integer value to be converted intoDataTernary
.
§Returns
A DataTernary
instance that represents the given decimal number.
§Example
use balanced_ternary::{DataTernary};
let data_ternary = DataTernary::from_dec(42);
assert_eq!(data_ternary.to_dec(), 42);
Sourcepub fn to_dec(&self) -> i64
pub fn to_dec(&self) -> i64
Converts a DataTernary
into its decimal representation.
This method reconstructs the ternary value represented by the DataTernary
structure and converts it into the corresponding signed 64-bit decimal integer.
§Returns
A signed 64-bit integer (i64
) representing the decimal equivalent of the
DataTernary
structure.
§Example
use balanced_ternary::{DataTernary};
let data_ternary = DataTernary::from_dec(42);
let decimal = data_ternary.to_dec();
assert_eq!(decimal, 42);
Trait Implementations§
Source§impl Clone for DataTernary
impl Clone for DataTernary
Source§fn clone(&self) -> DataTernary
fn clone(&self) -> DataTernary
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more