Skip to main content

BitEnc

Struct BitEnc 

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

A sequence of bitencoded values.

Space complexity: O(⌈(n * width) / k⌉) * 32 bit, where n is the length of the input sequence and k = 32 - (32 % width) is the number of bits in each 32-bit block that can be used to store values. For values that are not a divider of 32, some bits will remain unused. For example for width = 7 only 4 * 7 = 28 bits are used. Five 7-bit values are stored in 2 blocks.

Implementations§

Source§

impl BitEnc

Source

pub fn new(width: usize) -> Self

Create a new instance with a given encoding width (e.g. width=2 for using two bits per value). Supports widths up to 8 bits per character, i.e. 1 <= width <= 8.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;
let bitenc = BitEnc::new(3);
Source

pub fn with_capacity(width: usize, n: usize) -> Self

Create a new instance with a given capacity and encoding width (e.g. width=2 for using two bits per value). Supports widths up to 8 bits per character, i.e. 1 <= width <= 8.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let bitenc = BitEnc::with_capacity(3, 42);
Source

pub fn push(&mut self, value: u8)

Append a character to the current bit-encoding.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(4);
bitenc.push(0b0000);
bitenc.push(0b1000);
bitenc.push(0b1010);
// The three characters added above are encoded into one u32 entry.
let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [0b0000, 0b1000, 0b1010]);
Source

pub fn push_values(&mut self, n: usize, value: u8)

Append the given value to the encoding n times.

The added values comprise 0 to 1 blocks that need to be filled up from previous steps, 0 to m blocks that are completely filled with the value and 0 to 1 blocks that are only partially filled.

Complexity: O(n)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(8);
// Width: 8 → 4 values per block
// | __ __ __ __ | Denotes one block with 4 empty slots

bitenc.push_values(5, 0b101010);
// This adds one full and one partial block.
// | 42 42 42 42 | __ __ __ 42 |

let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [42, 42, 42, 42, 42]);

bitenc.push_values(1, 23);
// This only fills up an existing block;
// | 42 42 42 42 | __ __ 23 42 |

let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [42, 42, 42, 42, 42, 23]);

bitenc.push_values(6, 17);
// Fills up the current block, adds a whole new one but does not create a partial block.
// | 42 42 42 42 | 17 17 23 42 | 17 17 17 17 |

let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [42, 42, 42, 42, 42, 23, 17, 17, 17, 17, 17, 17]);
Source

pub fn set(&mut self, i: usize, value: u8)

Replace the current value as position i with the given value.

Complexity: O(1)

use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(4);
bitenc.push_values(4, 0b1111);
bitenc.set(2, 0b0000);

let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [0b1111, 0b1111, 0b0000, 0b1111]);
Source

pub fn get(&self, i: usize) -> Option<u8>

Get the value at position i.

Complexity: O(1)

use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(4);
for value in 1..=4 {
    bitenc.push(value);
}

let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [0b0001, 0b0010, 0b0011, 0b0100]);
Source

pub fn iter(&self) -> BitEncIter<'_>

Iterate over stored values (values will be unpacked into bytes).

Complexity: O(n), where n is the number of encoded values

§Example
use fqtk_lib::bitenc::BitEnc;

// Fill bitenc with 1, 2, 3, and 4.
let mut bitenc = BitEnc::new(4);
for value in 1..=4 {
    bitenc.push(value);
}

// Collect iterator for comparison
let values: Vec<u8> = bitenc.iter().collect();
assert_eq!(values, [0b0001, 0b0010, 0b0011, 0b0100]);
Source

pub fn clear(&mut self)

Clear the sequence.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(2);
bitenc.push(2);
assert_eq!(bitenc.len(), 1);
bitenc.clear();
assert_eq!(bitenc.len(), 0);
Source

pub fn len(&self) -> usize

👎Deprecated since 0.33.0:

Please use the more specific nr_blocks and nr_symbols functions instead.

Get the number of symbols encoded.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(8);
bitenc.push(2);
assert_eq!(bitenc.len(), 1);
bitenc.push(2);
bitenc.push(2);
bitenc.push(2);
assert_eq!(bitenc.len(), 4);
// Add another 2 to create a second block
bitenc.push(2);
assert_eq!(bitenc.len(), 5);
Source

pub fn nr_blocks(&self) -> usize

Get the number of blocks used by the encoding.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(8);
bitenc.push(2);
assert_eq!(bitenc.nr_blocks(), 1);
// Add enough 2s to completely fill the first block
bitenc.push(2);
bitenc.push(2);
bitenc.push(2);
assert_eq!(bitenc.nr_blocks(), 1);
// Add another 2 to create a second block
bitenc.push(2);
assert_eq!(bitenc.nr_blocks(), 2);
Source

pub fn nr_symbols(&self) -> usize

Get the number of symbols encoded.

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(8);
bitenc.push(2);
assert_eq!(bitenc.nr_symbols(), 1);
bitenc.push(2);
bitenc.push(2);
bitenc.push(2);
assert_eq!(bitenc.nr_symbols(), 4);
bitenc.push(2);
assert_eq!(bitenc.nr_symbols(), 5);
Source

pub fn is_empty(&self) -> bool

Is the encoded sequence empty?

Complexity: O(1)

§Example
use fqtk_lib::bitenc::BitEnc;

let mut bitenc = BitEnc::new(2);
assert!(bitenc.is_empty());
bitenc.push(2);
assert!(!bitenc.is_empty());
bitenc.clear();
assert!(bitenc.is_empty());
Source

pub fn hamming(&self, other: &BitEnc, max_mismatches: u32) -> u32

Calculate the Hamming distance between this and another bitencoded sequence.

Note: this allows IUPAC fuzzy matching with IUPAC bases in this sequence. An IUPAC base in the other sequence matches if it is at least as specific as the corresponding (IUPAC) base in this sequence. E.g. If the other sequence is an N, it will not match anything but an N, and if the other base is an R, it will match R, V, D, and N, since the latter IUPAC codes allow both A and G.

§Panics

Panics if the length and widths of the two sequences are not the same.

Trait Implementations§

Source§

impl Clone for BitEnc

Source§

fn clone(&self) -> BitEnc

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BitEnc

Source§

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

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

impl Default for BitEnc

Source§

fn default() -> BitEnc

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

impl<'de> Deserialize<'de> for BitEnc

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for BitEnc

Source§

impl Hash for BitEnc

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 BitEnc

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · 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 BitEnc

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 BitEnc

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Serialize for BitEnc

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for BitEnc

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.