pub struct PackedIntegers<T: PackedInt> { /* private fields */ }
Expand description

A growable array of packed integers, backed by a Vec<u32> buffer.

Examples

use packed_integers::{PackedIntegers, U9};

let mut is = PackedIntegers::<U9>::new();
is.push(510);
is.push(511);

assert_eq!(is.len(), 2);
assert_eq!(is.get(0), Some(510));

assert_eq!(is.pop(), Some(511));
assert_eq!(is.len(), 1);

is.set(0, 509);
assert_eq!(is.get(0), Some(509));

// This will panic, as 512 > U9::MAX.
// is.push(512);

for i in &is {
    println!("{}", i);
}

The packed_ints! macro makes initialisation more convenient:

use packed_integers::{packed_ints, U7, U9};

let mut is_u7 = packed_ints![125, 126, 127; U7];

let mut is_u9 = packed_ints![509, 510, 511; U9];

Implementations§

source§

impl<T: PackedInt> PackedIntegers<T>

source

pub fn new() -> PackedIntegers<T>

Constructs a new, empty PackedIntegers<T>.

Example
use packed_integers::{PackedIntegers, U9};

let mut is = PackedIntegers::<U9>::new();
source

pub fn with_capacity(capacity: usize) -> PackedIntegers<T>

Constructs a new, empty PackedIntegers<T> with at least the specified capacity.

Example
use packed_integers::{PackedIntegers, U8};

let mut is = PackedIntegers::<U8>::with_capacity(1);

// The specified capacity is 1, but because `PackedIntegers` is backed by a `Vec<u32>`
// buffer, it will actually hold 4 `U8`s without reallocating.
assert_eq!(is.capacity(), 4);
source

pub fn append(&mut self, other: &mut Self)

Moves all integers of other into Self, leaving other empty.

Example
use packed_integers::{packed_ints, U8};

let mut is1 = packed_ints![10, 20, 30; U8];
let mut is2 = packed_ints![40, 50, 60; U8];
is1.append(&mut is2);

assert_eq!(is1, packed_ints![10, 20, 30, 40, 50, 60; U8]);
assert!(is2.is_empty());
source

pub fn capacity(&self) -> usize

Returns the number of integers the vector can hold without reallocating.

Example
use packed_integers::{PackedIntegers, U8};

let mut is = PackedIntegers::<U8>::with_capacity(1);

// The specified capacity is 1, but because `PackedIntegers` is backed by a `Vec<u32>`
// buffer, it will actually hold 4 `U8`s without reallocating.
assert_eq!(is.capacity(), 4);
source

pub fn clear(&mut self)

Clears the vector. This method does not affect the vector’s allocated capacity.

Example
use packed_integers::{packed_ints, U9};

let mut is = packed_ints![100, 200, 300; U9];

is.clear();

assert!(is.is_empty());
source

pub fn from_vec(buf: Vec<u32>, num_ints: usize) -> PackedIntegers<T>

Creates an array of packed integers from a supplied Vec<u32> buffer. Panics if num_ints * T::NUM_BITS is greater than the number of bits the buffer has.

Example
use packed_integers::{PackedIntegers, U8};

let buf = vec![0b00000001_00000010_00000100_00001000];
let is = PackedIntegers::<U8>::from_vec(buf, 4);

assert_eq!(is.get(0), Some(0b1000));
assert_eq!(is.get(1), Some(0b0100));
assert_eq!(is.get(2), Some(0b0010));
assert_eq!(is.get(3), Some(0b0001));
assert_eq!(is.get(4), None);
source

pub fn get(&self, index: usize) -> Option<u32>

Returns the value of the integer at position index, or None if out of bounds.

Example
use packed_integers::{packed_ints, U9};

let mut is = packed_ints![100, 200, 300; U9];

assert_eq!(is.get(1), Some(200));
assert_eq!(is.get(3), None);
source

pub fn insert(&mut self, index: usize, value: u32)

Inserts an integer at position index, shifting all integers after it to the right.

Example
use packed_integers::{packed_ints, U8};

let mut is = packed_ints![10, 20, 30; U8];

is.insert(1, 40);
assert_eq!(is, packed_ints![10, 40, 20, 30; U8]);

is.insert(4, 50);
assert_eq!(is, packed_ints![10, 40, 20, 30, 50; U8]);
source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no integers.

Example
use packed_integers::{PackedIntegers, U8};

let mut is = PackedIntegers::<U8>::new();
assert!(is.is_empty());

is.push(255);
assert!(!is.is_empty());
source

pub fn iter(&self) -> PackedIntegersIterator<'_, T>

Returns an iterator over the vector.

Example
use packed_integers::{packed_ints, U9};

let is = packed_ints![509, 510, 511; U9];
let mut iter = is.iter();

assert_eq!(iter.next(), Some(509));
assert_eq!(iter.next(), Some(510));
assert_eq!(iter.next(), Some(511));
assert_eq!(iter.next(), None);
source

pub fn len(&self) -> usize

Returns the number of integers in the vector.

Example
use packed_integers::{packed_ints, U9};

let is = packed_ints![507, 508, 509, 510, 511; U9];

assert_eq!(is.len(), 5);
source

pub fn pop(&mut self) -> Option<u32>

Removes the last integer from the vector and returns it, or None if empty.

Example
use packed_integers::{packed_ints, U10};

let mut is = packed_ints![100, 200, 300; U10];

assert_eq!(is.pop(), Some(300));
assert_eq!(is, packed_ints![100, 200; U10]);
source

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

Appends an integer to the back of the vector.

Example
use packed_integers::{packed_ints, U10};

let mut is = packed_ints![100, 200; U10];
is.push(300);

assert_eq!(is, packed_ints![100, 200, 300; U10]);
source

pub fn remove(&mut self, index: usize) -> u32

Removes and returns the integer at position index, shifting all integers after it to the left.

Example
use packed_integers::{packed_ints, U8};

let mut is = packed_ints![10, 20, 30; U8];

assert_eq!(is.remove(1), 20);
assert_eq!(is, packed_ints![10, 30; U8]);
source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more integers to be inserted.

Example
use packed_integers::{packed_ints, U8};

let mut is = packed_ints![100; U8];
is.reserve(4);

assert!(is.capacity() >= 5);
source

pub fn set(&mut self, index: usize, value: u32)

Sets the integer value at index to value.

Example
use packed_integers::{packed_ints, U9};

let mut is = packed_ints![100, 200, 300; U9];
is.set(1, 400);

assert_eq!(is, packed_ints![100, 400, 300; U9]);
source

pub fn to_vec(&self) -> Vec<u32>

Returns a copy of the backing Vec<u32> buffer.

Example
use packed_integers::{packed_ints, U8};

let is = packed_ints![
    0b0001,
    0b0010,
    0b0100,
    0b1000;
U8];
let vec = is.to_vec();

assert_eq!(vec.len(), 1);
assert_eq!(vec[0], 0b00001000_00000100_00000010_00000001);
source

pub fn truncate(&mut self, len: usize)

Keeps the first len integers, and drops the rest.

Example
use packed_integers::{packed_ints, U9};

let mut is = packed_ints![100, 200, 300, 400, 500; U9];
is.truncate(2);

assert_eq!(is, packed_ints![100, 200; U9]);

Trait Implementations§

source§

impl<T: Clone + PackedInt> Clone for PackedIntegers<T>

source§

fn clone(&self) -> PackedIntegers<T>

Returns a copy 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<T: PackedInt> Debug for PackedIntegers<T>

source§

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

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

impl<'a, T: PackedInt> IntoIterator for &'a PackedIntegers<T>

§

type Item = u32

The type of the elements being iterated over.
§

type IntoIter = PackedIntegersIterator<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PackedInt> IntoIterator for PackedIntegers<T>

§

type Item = u32

The type of the elements being iterated over.
§

type IntoIter = PackedIntegersIntoIterator<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PackedInt> Ord for PackedIntegers<T>

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PackedInt> PartialEq<PackedIntegers<T>> for PackedIntegers<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PackedInt> PartialOrd<PackedIntegers<T>> for PackedIntegers<T>

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: PackedInt> Eq for PackedIntegers<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for PackedIntegers<T>where T: RefUnwindSafe,

§

impl<T> Send for PackedIntegers<T>where T: Send,

§

impl<T> Sync for PackedIntegers<T>where T: Sync,

§

impl<T> Unpin for PackedIntegers<T>where T: Unpin,

§

impl<T> UnwindSafe for PackedIntegers<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.