pub struct PackedIntegers { /* private fields */ }Expand description
An array of integers packed together in blocks using a variable number of bits for each integer, along with an index to allow fast random access.
Implementations§
Source§impl PackedIntegers
impl PackedIntegers
Sourcepub fn from_vec(data: Vec<u64>) -> Self
pub fn from_vec(data: Vec<u64>) -> Self
Build a packed array of integers from a vector.
This re-uses the space the vector is allocated in while building the packed array, though it may involve a re-allocation of the space to reduce the size at the end (it should never increase this allocation, though the additional size of the separate index may mean more space is used overall in some cases).
use ::packed_integer_array::PackedIntegers;
let input_data: Vec<u64> = vec![5, 138, 10, 90, 242, 312, 541, 48];
let packed = PackedIntegers::from_vec(input_data.clone());
assert_eq!(input_data.len(), packed.len());
for i in 0..input_data.len() {
assert_eq!(Some(input_data[i]), packed.get(i));
}Sourcepub fn from_iter<I, T>(iter: I) -> Self
pub fn from_iter<I, T>(iter: I) -> Self
Build a packed array of integers from an iterator.
use ::packed_integer_array::PackedIntegers;
let input_data: Vec<u64> = vec![5, 138, 10, 90, 242, 312, 541, 48];
let packed = PackedIntegers::from_iter(input_data.iter().cloned());
assert_eq!(input_data.len(), packed.len());
for i in 0..input_data.len() {
assert_eq!(Some(input_data[i]), packed.get(i));
}Sourcepub fn get(&self, idx: usize) -> Option<u64>
pub fn get(&self, idx: usize) -> Option<u64>
Get the integer at a specific index.
This operation should be fast, and does not involve unpacking many others of the packed integers or using a lot of memory for the unpacking.
Returns None for out-of-bounds.
use rand::prelude::*;
use ::packed_integer_array::PackedIntegers;
let mut rng = rand::thread_rng();
let len = rng.gen_range(137, 549);
let unpacked: Vec<u64> = (0..len).map(|_| rng.gen()).collect();
let packed = PackedIntegers::from_iter(unpacked.iter().cloned());
for i in 0..len {
assert_eq!(Some(unpacked[i]), packed.get(i));
}
assert_eq!(None, packed.get(len));
assert_eq!(None, packed.get(len + 5));Source§impl PackedIntegers
impl PackedIntegers
Sourcepub fn iter<'a>(&'a self) -> Iter<'a> ⓘ
pub fn iter<'a>(&'a self) -> Iter<'a> ⓘ
Iterate through the packed integers without consuming self.
use rand::prelude::*;
use ::packed_integer_array::PackedIntegers;
let mut rng = rand::thread_rng();
let len = rng.gen_range(0, 10000);
let data: Vec<u64> = (0..len).map(|_| rng.gen()).collect();
let packed = PackedIntegers::from_iter(data.iter().cloned());
let unpacked: Vec<_> = packed.iter().collect();
assert_eq!(data, unpacked);Trait Implementations§
Source§impl Clone for PackedIntegers
impl Clone for PackedIntegers
Source§fn clone(&self) -> PackedIntegers
fn clone(&self) -> PackedIntegers
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more