Struct Code

Source
pub struct Code { /* private fields */ }
Expand description

A Turing Machine code, represented by a flipped bit in a u128. This is the most efficient format for use with Set since it allows for fast set inclusion checks.

Implementations§

Source§

impl Code

Source

pub fn from_digits(triangle: u8, square: u8, circle: u8) -> Result<Self, Error>

Get the code with the given digits.

§Errors

If the provided digits do not lie in the range 1..=5, the code is invalid and the error [code::Error::InvalidDigits] will be returned.

§Examples
use turing_machine_ai::code;
assert!(code::Code::from_digits(1, 2, 3).is_ok());
assert_eq!(code::Code::from_digits(3, 4, 9), Err(code::Error::InvalidDigits));
Source

pub fn digits(self) -> (u8, u8, u8)

Get the digits of this code.

use turing_machine_ai::code::Code;

let code = Code::from_digits(5, 4, 3)?;
assert_eq!(code.digits(), (5, 4, 3));

let code_2 = Code::from_digits(1, 3, 4)?;
assert_eq!(code_2.digits(), (1, 3, 4));
Source

pub fn triangle(self) -> u8

Returns the digit for the triangle symbol in this code.

Source

pub fn square(self) -> u8

Returns the digit for the square symbol in this code.

Source

pub fn circle(self) -> u8

Returns the digit for the circle symbol in this code.

Source

pub fn digit_sum(self) -> u8

Get the sum of the digits.

use turing_machine_ai::code::Code;
let code = Code::from_digits(5, 2, 4)?;
assert_eq!(code.digit_sum(), 11);
Source

pub fn count_digit(&self, digit: u8) -> usize

Count the appearances of a particular digit.

§Example

use turing_machine_ai::code::Code;

assert_eq!(Code::from_digits(2, 3, 4)?.count_digit(2), 1);
assert_eq!(Code::from_digits(2, 3, 2)?.count_digit(2), 2);
Source

pub fn count_even(&self) -> usize

Count the even digits.

§Example
use turing_machine_ai::code::Code;

assert_eq!(Code::from_digits(2, 3, 4)?.count_even(), 2);
Source

pub fn sequence_ascending_or_descending(&self) -> usize

Number of digits in ascending or descending order as specified by verifier 25.

Source

pub fn sequence_ascending(self) -> usize

Number of digits in ascending order.

use turing_machine_ai::code::Code;
assert_eq!(Code::from_digits(2, 3, 4)?.sequence_ascending(), 3);
assert_eq!(Code::from_digits(2, 3, 3)?.sequence_ascending(), 2);
assert_eq!(Code::from_digits(1, 3, 5)?.sequence_ascending(), 0);
Source

pub fn repeating_numbers(self) -> usize

Counts the repetitions of the most frequent number, à la verifier card 20.

use turing_machine_ai::code::Code;
assert_eq!(Code::from_digits(2, 2, 2)?.repeating_numbers(), 2);
assert_eq!(Code::from_digits(1, 1, 2)?.repeating_numbers(), 1);
assert_eq!(Code::from_digits(1, 2, 1)?.repeating_numbers(), 1);
assert_eq!(Code::from_digits(1, 2, 5)?.repeating_numbers(), 0);
Source

pub fn is_ascending_or_descending(self) -> Order

Provides the order of the digits as in verifier 22.

use turing_machine_ai::code::{Code, Order};
assert_eq!(Code::from_digits(1, 3, 5)?.is_ascending_or_descending(), Order::Ascending);
assert_eq!(Code::from_digits(4, 2, 1)?.is_ascending_or_descending(), Order::Descending);
assert_eq!(Code::from_digits(2, 3, 1)?.is_ascending_or_descending(), Order::NoOrder);

Trait Implementations§

Source§

impl Clone for Code

Source§

fn clone(&self) -> Code

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 Code

Source§

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

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

impl FromIterator<Code> for Set

Source§

fn from_iter<T: IntoIterator<Item = Code>>(iter: T) -> Self

Create a new code set containing all codes in the iterator.

Source§

impl Hash for Code

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 PartialEq for Code

Source§

fn eq(&self, other: &Code) -> 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 Copy for Code

Source§

impl Eq for Code

Source§

impl StructuralPartialEq for Code

Auto Trait Implementations§

§

impl Freeze for Code

§

impl RefUnwindSafe for Code

§

impl Send for Code

§

impl Sync for Code

§

impl Unpin for Code

§

impl UnwindSafe for Code

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, 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.