Expand description
Generic run-length encoded sequences.
A run-length encoded sequence stores runs of repeated values once:
a a a b b becomes runs [3, 2] with values [a, b]. This crate works
with any value type, and is built around one idea: combining sequences is
split into a plan, computed from run boundaries alone, and a separate step
that computes values.
use runlength::prelude::*;
let a = Rle::from_runs(&[3, 2], vec![1, 2]).unwrap();
let b = Rle::from_runs(&[1, 4], vec![10, 20]).unwrap();
// The plan says which run of each input covers each output segment.
let p = a.plan_with(&b);
assert_eq!(p.ends, vec![1, 3, 5]);
assert_eq!(p.idx_a, vec![0, 0, 1]);
assert_eq!(p.idx_b, vec![0, 1, 1]);
// Any operation can be applied to any value types...
let labels = a.zip_with(&b, None, None, |x, y| format!("{x}/{y}")).unwrap();
assert_eq!(labels.get(0), Some(&"1/10".to_string()));
// ...and the usual operators pad the shorter side with zero.
let sum = &a + &b;
assert_eq!(sum.to_dense(), vec![11, 21, 21, 22, 22]);
assert_eq!(sum.runs(), vec![1, 2, 2]);§Main types
Rleowns a sequence;RleRefborrows one. Their shared operations are on theRleLiketrait.RleBatchstores many sequences under arbitrary keys in shared arrays, and combines batches key by key in parallel.PlanandMergePlandescribe how runs combine without touching values, so callers can compute values any way they like.coverage_sum,coverage_countand their_by_keyvariants build sequences from intervals.
§Conventions
- Positions and lengths are
i64, and every sequence starts at 0. - Intervals are half-open,
[start, end). - Neighbouring runs merge when their values are
Identical:NaNmerges withNaN,-0.0does not merge with0.0. - Operations merge their result by default;
_rawvariants do not.
Modules§
- prelude
- The types and traits needed for everyday use.
Structs§
- Merge
Plan - How runs are grouped by a merge mask, without any values involved.
- Overlap
Plan - Which intervals cover each segment, for aggregating interval values in any way.
- Plan
- How to combine two sequences, computed from their run ends alone.
- Plan
Many - How to combine
ksequences, computed from their run ends alone: thek-input version ofPlan. - Rle
- A run-length encoded sequence that owns its data.
- RleBatch
- Many run-length encoded sequences, each identified by a key.
- RleRef
- A borrowed run-length encoded sequence, for example one entry of an
RleBatch. It has the same invariants asRle. - Slice
View - A borrowed slice
start..endof a sequence that is not copied, for reducing many slices cheaply. Positions are relative tostart.
Enums§
- Error
- Errors raised when input is invalid or an operation cannot be carried out.
- How
- Which keys a combination of two batches keeps, as in a DataFrame join.
- Keep
- Which value a merged group keeps when its runs hold different values.
Traits§
- Identical
- The equality used to decide whether two neighbouring runs merge.
- Num
- Numeric value types that support length-weighted reductions and coverage.
- RleLike
- Read-only operations shared by
RleandRleRef.
Functions§
- concat
- Joins sequences end to end. Identical neighbours are merged, including at the joins.
- coverage_
count - The number of intervals covering each position.
- coverage_
count_ by_ key coverage_countper key, computed in parallel. Keys appear in order of first appearance; each sequence covers0..max(end)of its intervals.- coverage_
max - The largest of
valuesover the intervals covering each position, orNonewhere no interval covers it. Works for any ordered value type;NaNonly wins where nothing else covers the position. - coverage_
max_ by_ key coverage_maxper key, computed in parallel.- coverage_
min - The smallest of
valuesover the intervals covering each position; seecoverage_max. - coverage_
min_ by_ key coverage_minper key, computed in parallel.- coverage_
sum - The sum of
valuesover the intervals covering each position. - coverage_
sum_ by_ key coverage_sumper key, computed in parallel. Keys appear in order of first appearance; each sequence covers0..max(end)of its intervals.- coverage_
sum_ opt - Like
coverage_sum, but uncovered positions areNoneinstead of 0. - merge_
plan - Groups runs with cumulative
endsby a mask over neighbouring pairs. SeeMergePlan. - overlap_
plan - Computes the
OverlapPlanfor intervals[start, end). - overlap_
plan_ by_ key overlap_planper key, computed in parallel. Returns the keys in order of first appearance and one plan per key, covering0..max(end)of that key’s intervals. Interval ids are positions in the input, not per key.- plan
- Computes the
Planfor combining sequences with run endsaandb. - plan_
many - Computes the
PlanManyfor sequences with the given run ends. - zip_
many - Combines several sequences position by position with
f, which receives one value per input, and merges the result. - zip_
with - Combines two sequences position by position with
f, merging identical neighbours in the result. - zip_
with_ raw - Like
zip_withbut keeps one run per plan segment, without merging.