PrimeByte

Struct PrimeByte 

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

A “byte of primes”, a chunk of 8 bits corresponding to the 8 values in the (0..30) range that are not divisible by 2, 3, or 5. Those values are also called k-values.

To learn more, read the guide.

“k-values” are values k, such that (N % 30 = k) and N is coprime with 30. Those values are listed here.

Implementations§

Source§

impl PrimeByte

Source

pub fn new() -> Self

Creates a new byte, setting all k-values as prime

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::new();
assert_eq!(u8::from(byte), 0b11111111);
Source

pub fn set_nonprime(&mut self, k_value: u8) -> Result<bool, ()>

Sets one of the bits to non-prime/composite based on the k-value

If the bit was already set to non-prime, returns false. Otherwise, returns true.

Returns an OutOfBounds error if the given value is not a k-value, returns an error.

To understand what k-value maps to what bit, refer to the k-values

§Examples
use prime_data::PrimeByte;
 
let mut byte = PrimeByte::from(0b10110111);
// k-value 1 corresponds to the first bit
// returns Ok(true) because the first bit was indeed, a one
assert!(byte.set_nonprime(1).unwrap());
assert_eq!(byte.as_u8(), 0b00110111);
// now, if we try to set it to non-prime again, it'll return false,
// because it's already a zero
assert!(!byte.set_nonprime(1).unwrap());
// when given a non-k-value, returns an error
assert!(byte.set_nonprime(4).is_err());
Source

pub fn as_boolean_array(&self) -> [bool; 8]

Converts the bits into boolean entries

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
    byte.as_boolean_array(),
    [true, false, true, false, false, true, true, false]
);
Source

pub fn as_k_values(&self) -> Vec<u8>

Retrieves all bits set to one and converts them into their respective k-values

For more information, read the guide, or refer to the list of k-values.

If you wish to retrieve k-values within a specific range, see PrimeByte::as_k_values_in_range.

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
    byte.as_k_values(),
    vec![1, 11, 19, 23]
);
Source

pub fn as_k_values_in_range(&self, range: RangeInclusive<u8>) -> Vec<u8>

Retrieves all bits set to one and converts them into their respective k-values, as long as they fall inside the inclusive range

For more information, read the guide, or refer to the list of k-values.

If you wish to retrieve all k-values, see PrimeByte::as_k_values.

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
    byte.as_k_values_in_range(2..=23),
    vec![11, 19, 23]
);
Source

pub fn as_primes(&self, offset: u64) -> Vec<u64>

Retrieves the k-values and converts them to actual prime numbers.

For more information, read the guide.

Calling this function with offset = 1 yields the same results as PrimeByte::as_k_values, except that it returns a vector of u64 instead of u8.

If you wish to only retrieve primes within a range, see PrimeByte::as_primes_in_range

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
    byte.as_primes(21),
    vec![631, 641, 649, 653]
);
Source

pub fn as_primes_in_range( &self, offset: u64, range: RangeInclusive<u8>, ) -> Vec<u64>

Retrieves the k-values, as long as they fall inside the inclusive range, and converts them to actual prime numbers

Calling this function with offset = 1 yields the same results as PrimeByte::as_k_values_in_range, except that it returns a vector of u64 instead of u8.

If you wish to retrieve all primes, see PrimeByte::as_primes

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
    byte.as_primes_in_range(21, 2..=23),
    vec![641, 649, 653]
);
Source

pub fn overwrite_at(&mut self, overwriting: Self, position: u8)

Overwrites the bits in this byte with the bits in the other byte, from a given position

The position should be a bit index in the range (0..=7), 0 meaning it’ll overwrite the entire byte with the given one.

This function is useful for joining two data structures of Prime Bytes, as most of the time the former’s end and the latter’s start intersect in the middle of a prime byte. That way, we overwrite the byte at the intersection.

§Panics

Panics if position > 7

§Examples
use prime_data::PrimeByte;
let mut original = PrimeByte::from(0b00000000);
let     new_bits = PrimeByte::from(0b11111111);
original.overwrite_at(new_bits, 5);
assert_eq!(
    u8::from(original),
    0b00000111
);
Source

pub fn is_prime(&self, x: u8) -> bool

Vefifies if the given x is prime, based on the k-values

Warning: It will return false for 2, 3, and 5. So take care of those cases before you call this function.

Second Warning: Will return false for values above 29. So always make sure to apply modulo 30 when calling this function.

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b00101000);
assert!( byte.is_prime(11));
assert!(!byte.is_prime(13));
assert!( byte.is_prime(17));
Source

pub fn count_primes(&self) -> u64

Counts the number of primes (a.k.a the number of ones) it has.

If you wish to count primes in a specific range, see PrimeByte::count_primes_in_range

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b11010111);
assert_eq!(byte.count_primes(), 6);
Source

pub fn count_primes_in_range(&self, range: RangeInclusive<u8>) -> u64

Counts the number of primes it has as long as their k-values fall within the range

If you wish to count all primes, see PrimeByte::count_primes

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b11010111);
assert_eq!(byte.count_primes_in_range(0..=30), 6);
assert_eq!(byte.count_primes_in_range(2..=30), 5);
assert_eq!(byte.count_primes_in_range(8..=12), 0);
Source

pub fn as_u8(&self) -> u8

Converts byte into a u8

This has the same effect as calling u8::from(). It’s meant to be an alternative way of converting into a u8, having the byte be written on the left instead of right.

§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::new();
assert_eq!(byte.as_u8(), u8::from(byte))
Source

pub fn matches_in_range( &self, other: PrimeByte, range: RangeInclusive<u8>, ) -> bool

Compares two bytes to see if their bits match in the given k-value range

Range is expected to be within (0..=30), but it will not return an error or panic, if given anything above 30. If you pass it some range like (30..=199), since none of the bits fall in that range, none will be compared, and hence the function will trivially return true.

There is no Self::matches method (without a range restriction) because that’s the same as verifying if the two bytes are equal. PrimeByte implements Eq.

§Examples
use prime_data::PrimeByte;
let byte  = PrimeByte::from(0b10111010);
let other = PrimeByte::from(0b10110111);
assert!( byte.matches_in_range(other, 0..=14));
assert!( byte.matches_in_range(other, 23..=23));
assert!(!byte.matches_in_range(other, 16..=17));
assert!( byte.matches_in_range(other, 30..=255));

Trait Implementations§

Source§

impl Clone for PrimeByte

Source§

fn clone(&self) -> PrimeByte

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 PrimeByte

Source§

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

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

impl Display for PrimeByte

Source§

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

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

impl From<PrimeByte> for u8

Source§

fn from(byte: PrimeByte) -> u8

Converts to this type from the input type.
Source§

impl From<u8> for PrimeByte

Source§

fn from(byte: u8) -> PrimeByte

Converts to this type from the input type.
Source§

impl PartialEq for PrimeByte

Source§

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

Source§

impl Eq for PrimeByte

Source§

impl StructuralPartialEq for PrimeByte

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.