rle_vec
Usage
Put this in your Cargo.toml:
[]
= "0.1"
Then put this in your crate root:
extern crate rle_vec;
RleVec
This crate provides RleVec, a vector like structure that stores runs of identical values coded
by the value and the number of repeats.
If your data consists of long stretches of identical values is can be beneficial to only store the number of times each value occurs. This can result in significant space savings, but there is a cost. Accessing an arbitrary index requires a binary search over the stored runs resulting in a O(log n) complexity versus O(1) for a normal vector. Other complexities are in the table where n is equal to the number of runs, not the length of a comparable Vec.
|push|index |set with breaking a run|set without breaking a run|insert with breaking a run|insert without breaking a run|
--------|----|--------|-----------------------|--------------------------|--------------------------|-----------------------------|
RleVec|O(1)|O(log n)|O((log n) + 2n) |O(log n) |O((log n) + 2n) |O((log n) + n) |
Vec |O(1)|O(1) |O(1)* | |O(n) | |
*Benchmarks show that setting vec[idx] = value is a lot slower than getting vec[idx]
The RleVec struct handles like a normal vector and supports a subset from the Vec methods.
Examples:
use RleVec;
let mut rle = new;
rle.push; rle.push; rle.push;
assert_eq!;
assert_eq!;
rle.insert;
assert_eq!;
rle.set;
assert_eq!;
RleVec can be constructed from Iterators and be iterated over just like a Vec.
use RleVec;
let v = vec!;
let mut rle: = v.into_iter.collect;
assert_eq!;
assert_eq!;
assert_eq!;
An RleVec can be indexed like a regular vector, but not mutated. Use RleVec::set to change the
value at an index.
use RleVec;
let v = vec!;
let mut rle: = v.into_iter.collect;
rle.set;
rle.insert;
assert_eq!;
RleVec::set and RleVec::insert require T: Clone.
Not all methods implemented on Vec are implemented for RleVec. All methods returning a slice
cannot work for RleVec.