rle_from_value_runs

Function rle_from_value_runs 

Source
pub fn rle_from_value_runs<T: Clone + PartialEq>(
    runs: Vec<(Range<usize>, T)>,
) -> (Vec<T>, Vec<(Range<usize>, u32)>)
Expand description

Converts normalized value-bearing runs into a deduplicated table and a run list, coalescing adjacent runs that carry the same value.

§Input

  • runs: a vector of (start..end, value) pairs that is already normalized:
    • sorted by (start, end) in ascending order,
    • non-empty (start < end),
    • non-overlapping (touching is allowed).

§Behavior

  • Builds table by global deduplication of values in first-appearance order. The same logical value appearing in disjoint places will share a single entry in table.
  • Produces out as a list of (start..end, id), where id indexes into table.
  • If two consecutive input runs are touching (prev.end == next.start) and their values are ==, they are coalesced into a single output run referencing the same id.

§Output invariants

  • out is sorted by start and non-overlapping; touching runs have different ids.
  • Each (r, id) in out satisfies id < table.len().
  • table preserves first-appearance order of distinct values across the entire input.
  • The union of ranges in out equals the union of ranges in the input runs.

§Preconditions

This function assumes the input runs is normalized as described above; it does not revalidate or resort the input. Supplying unsorted or overlapping ranges results in unspecified behavior.

§Example

// Input runs (already sorted & non-overlapping):
// [(0..2, A), (2..5, A), (5..6, B), (8..10, A)]
// After coalescing equal-adjacent:
// [(0..5, A), (5..6, B), (8..10, A)]
// table  = [A, B]
// out    = [(0..5, 0), (5..6, 1), (8..10, 0)]