8 releases
0.3.3 | Oct 13, 2023 |
---|---|
0.3.2 | Jul 23, 2022 |
0.2.1 | Apr 21, 2021 |
0.1.1 | Jan 15, 2021 |
0.1.0 | Oct 27, 2020 |
#800 in Data structures
16KB
335 lines
Subranges
Interval struct and collection for free intervals.
Example
use subranges::FreeIntervals;
fn main() {
// new collection with free interval
let free = (0..100).into();
let mut collection = FreeIntervals::new(free);
// fill subrange of free interval
let first = collection.take_exact(32).unwrap();
println!("first: {first}"); // output: first: [0, 32)
// fill other subrange of free interval with alignment
let aligned = collection.take_exact_aligned(32, 10).unwrap();
println!("aligned: {aligned}"); // output: aligned: [40, 72)
// no free subrange with `length == 40`.
let none = collection.take_exact(40);
assert!(none.is_none());
// free `first` intervals with `length == 32`,
// it connects with padding with `length == 8` from aligned interval.
collection.insert(first);
// now we have subrange with `length == 40`.
let some = collection.take_exact(40).unwrap();
println!("some: {some}"); // output: some: [0, 40)
}