DataTernary

Struct DataTernary 

Source
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

Source

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 - A Ternary value to be converted into a DataTernary.
§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);
Source

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);
Source

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
Source

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]);
Source

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 into DataTernary.
§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);
Source

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

Source§

fn clone(&self) -> DataTernary

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DataTernary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DataTernary

Source§

fn default() -> DataTernary

Returns the “default value” for a type. Read more
Source§

impl Display for DataTernary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<DataTernary> for Ternary

Source§

fn from(value: DataTernary) -> Self

Converts to this type from the input type.
Source§

impl From<Ternary> for DataTernary

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl Hash for DataTernary

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for DataTernary

Source§

fn cmp(&self, other: &DataTernary) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for DataTernary

Source§

fn eq(&self, other: &DataTernary) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for DataTernary

Source§

fn partial_cmp(&self, other: &DataTernary) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for DataTernary

Source§

impl StructuralPartialEq for DataTernary

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.