rle_from_dense

Function rle_from_dense 

Source
pub fn rle_from_dense<T, F>(
    values: Vec<T>,
    same: F,
) -> (Vec<T>, Vec<(Range<usize>, u32)>)
where F: FnMut(&T, &T) -> bool,
Expand description

Run-length encodes a dense sequence into a table of unique values and a run list, using a caller-provided equivalence predicate.

Consumes the dense values vector and produces:

  • table: the deduplicated values in first-occurrence order
  • runs: a list of (start..end, id) where id indexes into table

Two adjacent elements a, b belong to the same run iff same(a, b) returns true. In practice same should behave like an equivalence relation over adjacent elements (reflexive and symmetric; transitive is not required globally because only adjacency is consulted).

§Parameters

  • values: dense sequence; exactly one value per rank (consumed)
  • same: predicate deciding adjacency-equality (same(&a, &b) => merge)

§Returns

(table, runs) where:

  • table.len() >= 0
  • runs is empty iff values is empty

§Invariants on the output

  • runs is sorted by start and non-overlapping
  • runs exactly covers 0..values.len() with contiguous, non-empty half-open intervals
  • Adjacent runs always refer to different ids under same
  • For every (r, id) in runs, id < table.len()
  • For every index i in r, values[i] is same-equal to table[id]
  • table preserves first-appearance order of distinct (under same) values encountered while scanning left-to-right

§Examples

// values: [A, A, B, B, B, A]
// table:  [A, B, A]
// runs:   [(0..2, 0), (2..5, 1), (5..6, 2)]

Note: table preserves first-appearance order of each distinct contiguous run, not global uniqueness - the same logical value may appear multiple times in table if it occurs in disjoint runs.