pub fn rle_from_dense<T, F>(
values: Vec<T>,
same: F,
) -> (Vec<T>, Vec<(Range<usize>, u32)>)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 orderruns: a list of(start..end, id)whereidindexes intotable
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() >= 0runsis empty iffvaluesis empty
§Invariants on the output
runsis sorted bystartand non-overlappingrunsexactly covers0..values.len()with contiguous, non-empty half-open intervals- Adjacent runs always refer to different
ids undersame - For every
(r, id)inruns,id < table.len() - For every index
iinr,values[i]issame-equal totable[id] tablepreserves first-appearance order of distinct (undersame) 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.