Skip to main content

Vector

Trait Vector 

Source
pub trait Vector {
    type Item: Eq + Copy;

    // Required methods
    fn len(&self) -> usize;
    fn width(&self) -> usize;
    fn max_len(&self) -> usize;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

A vector that contains items of a fixed type.

The type of the item must implement Eq and Copy.

§Examples

use simple_sds_sbwt::ops::{Vector, Resize, Pack, Access, AccessIter};
use simple_sds_sbwt::bits;

#[derive(Clone, Debug, PartialEq, Eq)]
struct Example(Vec<u8>);

impl Example {
    fn new() -> Example {
        Example(Vec::new())
    }
}

impl Vector for Example {
    type Item = u8;

    fn len(&self) -> usize {
        self.0.len()
    }

    fn width(&self) -> usize {
        8
    }

    fn max_len(&self) -> usize {
        usize::MAX
    }
} 

impl Resize for Example {
    fn resize(&mut self, new_len: usize, value: Self::Item) {
        self.0.resize(new_len, value);
    }

    fn clear(&mut self) {
        self.0.clear();
    }

    fn capacity(&self) -> usize {
        self.0.capacity()
    }

    fn reserve(&mut self, additional: usize) {
        self.0.reserve(additional);
    }
}

// Packing does not make much sense with the running example.
impl Pack for Example {
    fn pack(&mut self) {}
}

impl<'a> Access<'a> for Example {
    type Iter = AccessIter<'a, Self>;

    fn get(&self, index: usize) -> Self::Item {
        self.0[index]
    }

    fn iter(&'a self) -> Self::Iter {
        Self::Iter::new(self)
    }

    fn is_mutable(&self) -> bool {
        true
    }

    fn set(&mut self, index: usize, value: Self::Item) {
        self.0[index] = value
    }
}

// Vector
let mut v = Example::new();
assert!(v.is_empty());
assert_eq!(v.len(), 0);
assert_eq!(v.width(), bits::bit_len(u8::MAX as u64));

// Resize
v.reserve(4);
assert!(v.capacity() >= 4);
v.resize(4, 0);
assert_eq!(v.len(), 4);
v.clear();
assert!(v.is_empty());

// Pack
let mut v = Example(vec![1, 2, 3]);
v.pack();
assert_eq!(v.len(), 3);

// Access
assert!(v.is_mutable());
for i in 0..v.len() {
    assert_eq!(v.get(i), (i + 1) as u8);
    v.set(i, i as u8);
    assert_eq!(v.get(i), i as u8);
}
let extracted: Vec<u8> = v.iter().collect();
assert_eq!(extracted, vec![0, 1, 2]);

Required Associated Types§

Source

type Item: Eq + Copy

The type of the items in the vector.

Required Methods§

Source

fn len(&self) -> usize

Returns the number of items in the vector.

Source

fn width(&self) -> usize

Returns the width of of an item in bits.

Source

fn max_len(&self) -> usize

Returns the maximum length of the vector.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the vector is empty.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Vector for IntVector

Source§

impl Vector for IntVectorWriter

Source§

impl Vector for WaveletMatrix

Source§

impl<'a> Vector for IntVectorMapper<'a>

Available on non-target_family=wasm only.