Struct packed_integers::PackedIntegers
source · 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>
impl<T: PackedInt> PackedIntegers<T>
sourcepub fn new() -> PackedIntegers<T>
pub fn new() -> PackedIntegers<T>
Constructs a new, empty PackedIntegers<T>
.
Example
use packed_integers::{PackedIntegers, U9};
let mut is = PackedIntegers::<U9>::new();
sourcepub fn with_capacity(capacity: usize) -> PackedIntegers<T>
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);
sourcepub fn append(&mut self, other: &mut Self)
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());
sourcepub fn capacity(&self) -> usize
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);
sourcepub fn clear(&mut self)
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());
sourcepub fn from_vec(buf: Vec<u32>, num_ints: usize) -> PackedIntegers<T>
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);
sourcepub fn get(&self, index: usize) -> Option<u32>
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);
sourcepub fn insert(&mut self, index: usize, value: u32)
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]);
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn iter(&self) -> PackedIntegersIterator<'_, T> ⓘ
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);
sourcepub fn len(&self) -> usize
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);
sourcepub fn pop(&mut self) -> Option<u32>
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]);
sourcepub fn push(&mut self, value: u32)
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]);
sourcepub fn remove(&mut self, index: usize) -> u32
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]);
sourcepub fn reserve(&mut self, additional: usize)
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);
sourcepub fn set(&mut self, index: usize, value: u32)
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]);
sourcepub fn to_vec(&self) -> Vec<u32>
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);
Trait Implementations§
source§impl<T: Clone + PackedInt> Clone for PackedIntegers<T>
impl<T: Clone + PackedInt> Clone for PackedIntegers<T>
source§fn clone(&self) -> PackedIntegers<T>
fn clone(&self) -> PackedIntegers<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: PackedInt> Debug for PackedIntegers<T>
impl<T: PackedInt> Debug for PackedIntegers<T>
source§impl<'a, T: PackedInt> IntoIterator for &'a PackedIntegers<T>
impl<'a, T: PackedInt> IntoIterator for &'a PackedIntegers<T>
source§impl<T: PackedInt> IntoIterator for PackedIntegers<T>
impl<T: PackedInt> IntoIterator for PackedIntegers<T>
source§impl<T: PackedInt> Ord for PackedIntegers<T>
impl<T: PackedInt> Ord for PackedIntegers<T>
source§impl<T: PackedInt> PartialEq<PackedIntegers<T>> for PackedIntegers<T>
impl<T: PackedInt> PartialEq<PackedIntegers<T>> for PackedIntegers<T>
source§impl<T: PackedInt> PartialOrd<PackedIntegers<T>> for PackedIntegers<T>
impl<T: PackedInt> PartialOrd<PackedIntegers<T>> for PackedIntegers<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more