Struct nucleic_acid::BitsVec [] [src]

pub struct BitsVec<T: ReprUsize> { /* fields omitted */ }

A vector to hold values that have a known bit range.

For example, DNA nucleotides don't need 8 bits to represent them. We know they only have four possible values, so 2 bits would be enough.

use nucleic_acid::{BitsVec, ReprUsize};
use std::mem;

#[derive(Clone, Copy, Debug)]
#[repr(usize)]
enum Nucleotide {
    Adenine,
    Thymine,
    Guanine,
    Cytosine,
}

impl ReprUsize for Nucleotide {
    fn from_usize(i: usize) -> Self {
        assert!(i <= 3, "expected vales in the range [0, 3]");
        unsafe { mem::transmute(i) }
    }

    fn into_usize(self) -> usize {
        self as usize
    }
}

fn main() {
    let vec = BitsVec::with_elements(2, 100, Nucleotide::Adenine);
    assert!(vec.len() == 100);
    // depends on the architecture (since BitsVec internally uses Vec<usize>)
    assert!(vec.inner_len() == 2 || vec.inner_len() == 4);
}

The human genome has ~3 billion bases (that's 3 GB). Using 8 bits for each of them would be a waste of space. This representation reduces the memory consumed by a factor of 6.

Methods

impl<T: ReprUsize> BitsVec<T>
[src]

Create a new vector that can hold values no larger than the specified bits

Creates a new vector that can hold the specified bits (atmost) and has capacity for "N" additional elements.

Push a value into the vector.

Get the value from an index in the vector. Note that this is similar to indexed getting, and so it panics when the index is out of bounds. For the non-panicking version, use checked_get

Returns Some(T) if the element exists at the given index or None if it doesn't.

Set a value at the given index. Note that this is similar to indexed setting, and so it panics when the index is out of bounds.

Creates a vector consuming an iterator of elements.

Returns the length of the vector. This only indicates the number of units it contains, and not the length of the inner vector.

Returns true if the vector contains no values (or false otherwise).

Reserve space for "N" additional elements.

Shrink the inner vector's capacity to fit to its length. It does nothing more than calling the same method in the inner vector.

Truncate the vector to the given length, removing the out-of-bound elements. Note that this method panics when the length is greater than current length.

Clears the inner vector. Note that this is similar to calling truncate with zero.

Returns the length of the inner vector. Useful for measuring the memory consumption of the elements.

Creates an iterator over the elements. Note that unlike other iterators, this gives the elements themselves, and not their references.

Creates an iterator consuming the vector.

impl<T: ReprUsize + Clone> BitsVec<T>
[src]

Creates a vector initialized with "N" copies of the given element.

Extends the vector to the specified length, filling additional values with the given element. Note that this method panics when the specified length is shorter than the initial length.

impl<T: ReprUsize + PartialEq> BitsVec<T>
[src]

Checks whether the vector contains the given element in O(n) time.

Trait Implementations

impl<T: Clone + ReprUsize> Clone for BitsVec<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Hash + ReprUsize> Hash for BitsVec<T>
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<T: ReprUsize + Debug> Debug for BitsVec<T>
[src]

Formats the value using the given formatter.

impl<T: ReprUsize> PartialEq for BitsVec<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a, T: ReprUsize> IntoIterator for &'a BitsVec<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<T: ReprUsize> IntoIterator for BitsVec<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more