https://github.com/softdevteam/packedvec
Store vectors of integers efficiently
https://github.com/softdevteam/packedvec
rust
Last synced: 10 months ago
JSON representation
Store vectors of integers efficiently
- Host: GitHub
- URL: https://github.com/softdevteam/packedvec
- Owner: softdevteam
- License: other
- Created: 2017-10-03T15:17:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-03-17T17:49:35.000Z (11 months ago)
- Last Synced: 2025-03-17T18:04:31.243Z (11 months ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 90.8 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
[](https://travis-ci.org/softdevteam/packedvec)
[](https://crates.io/crates/packedvec)
[](https://docs.rs/packedvec)
# PackedVec
A [`PackedVec`](https://docs.rs/packedvec/) stores vectors of integers
efficiently while providing an API similar to `Vec`. The basic idea is to store
each element using the minimum number of bits needed to represent every element
in the `Vec`. For example, if we have a `Vec` with elements [20, 30, 140],
every element wastes most of its 64 bits: 7 bits is sufficient to represent the
range of elements in the vector. Given this input vector, `PackedVec` stores
each elements using exactly 7 bits, saving substantial memory. For vectors which
often contain small ranges of numbers, and which are created rarely, but read
from frequently, this can be a significant memory and performance win.
# Examples
`PackedVec` has two main API differences from `Vec`: a `PackedVec` is created
from a `Vec`; and a `PackedVec` returns values rather than references. Both
points can be seen in this example:
```rust
use packedvec::PackedVec;
let v = vec![-1, 30, 120];
let pv = PackedVec::new(v.clone());
assert_eq!(pv.get(0), Some(-1));
assert_eq!(pv.get(2), Some(120));
assert_eq!(pv.get(3), None);
assert_eq!(v.iter().cloned().collect::>(), pv.iter().collect::>());
```