memory_range

Function flatten_ranges

Source
pub fn flatten_ranges(
    ranges: impl IntoIterator<Item = MemoryRange>,
) -> impl Iterator<Item = MemoryRange>
Expand description

Takes a sequence of memory ranges, sorted by their start address, and returns an iterator over the flattened ranges, where overlapping and adjacent ranges are merged and deduplicated.

Panics if the input ranges are not sorted by their start address.

ยงExample

let ranges = [
    MemoryRange::new(0x1000..0x2000),
    MemoryRange::new(0x2000..0x5000),
    MemoryRange::new(0x4000..0x6000),
    MemoryRange::new(0x5000..0x6000),
    MemoryRange::new(0x8000..0x9000),
];
let flattened = [
    MemoryRange::new(0x1000..0x6000),
    MemoryRange::new(0x8000..0x9000),
];
assert!(flatten_ranges(ranges).eq(flattened));