runlength 0.1.0

Generic run-length encoded sequences with plan-based arithmetic.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use std::fmt;

use crate::batch::RleBatch;
use crate::error::{Error, Result};
use crate::identical::Identical;
use crate::merge::{self, MergePlan};
use crate::num::Num;
use crate::ops;
use crate::plan::{self, Plan, Segments};
use crate::reduce;
use crate::slice::{self, SliceView};
use crate::transform;

/// A run-length encoded sequence that owns its data.
///
/// Internally a sequence is stored as the cumulative *ends* of its runs plus
/// one value per run: runs `[3, 2]` with values `[a, b]` are stored as ends
/// `[3, 5]`. Run `i` covers positions `ends[i - 1]..ends[i]` (the first run
/// starts at 0). Every sequence starts at position 0.
///
/// Invariants: ends are strictly increasing and positive, so there are no
/// zero-length runs, and there is one value per run. Neighbouring runs are
/// *not* guaranteed to hold different values; see [`Rle::merge`].
#[derive(Clone)]
pub struct Rle<V> {
    pub(crate) ends: Vec<i64>,
    pub(crate) values: Vec<V>,
}

/// A borrowed run-length encoded sequence, for example one entry of an
/// [`RleBatch`]. It has the same invariants as [`Rle`].
pub struct RleRef<'a, V> {
    pub(crate) ends: &'a [i64],
    pub(crate) values: &'a [V],
}

impl<V> Clone for RleRef<'_, V> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<V> Copy for RleRef<'_, V> {}

pub(crate) fn validate_ends(ends: &[i64]) -> Result<()> {
    let mut prev = 0;
    for (index, &end) in ends.iter().enumerate() {
        if end <= prev {
            return Err(Error::InvalidEnds { index });
        }
        prev = end;
    }
    Ok(())
}

fn check_same_len(what: &'static str, left: usize, right: usize) -> Result<()> {
    if left != right {
        return Err(Error::LengthMismatch { what, left, right });
    }
    Ok(())
}

impl<V> Rle<V> {
    /// An empty sequence of length 0.
    pub fn new() -> Self {
        Rle {
            ends: Vec::new(),
            values: Vec::new(),
        }
    }

    /// Builds a sequence from run lengths and one value per run.
    ///
    /// Zero-length runs are dropped. Negative runs and mismatched lengths are
    /// errors. Neighbouring runs with identical values are kept as they are.
    pub fn from_runs(runs: &[i64], values: Vec<V>) -> Result<Self> {
        check_same_len("runs and values", runs.len(), values.len())?;
        let mut out = Rle {
            ends: Vec::with_capacity(runs.len()),
            values: Vec::with_capacity(values.len()),
        };
        let mut total: i64 = 0;
        for (index, (&run, value)) in runs.iter().zip(values).enumerate() {
            if run < 0 {
                return Err(Error::NegativeRun { index, run });
            }
            if run == 0 {
                continue;
            }
            total = total.checked_add(run).ok_or(Error::Overflow)?;
            out.ends.push(total);
            out.values.push(value);
        }
        Ok(out)
    }

    /// Builds a sequence from cumulative run ends and one value per run.
    pub fn from_ends(ends: Vec<i64>, values: Vec<V>) -> Result<Self> {
        check_same_len("ends and values", ends.len(), values.len())?;
        validate_ends(&ends)?;
        Ok(Rle { ends, values })
    }

    /// A sequence of `length` copies of `value`.
    pub fn repeat(value: V, length: i64) -> Result<Self> {
        Self::from_runs(&[length], vec![value])
    }

    /// Splits the sequence into its ends and values.
    pub fn into_parts(self) -> (Vec<i64>, Vec<V>) {
        (self.ends, self.values)
    }

    /// Mutable access to the values. Changing values cannot break the
    /// invariants, but may leave identical neighbours unmerged.
    pub fn values_mut(&mut self) -> &mut [V] {
        &mut self.values
    }

    /// Appends a run. Zero-length runs are ignored; neighbours are not merged.
    pub fn push_run(&mut self, run: i64, value: V) -> Result<()> {
        if run < 0 {
            return Err(Error::NegativeRun {
                index: self.ends.len(),
                run,
            });
        }
        if run > 0 {
            let end = self.len().checked_add(run).ok_or(Error::Overflow)?;
            self.ends.push(end);
            self.values.push(value);
        }
        Ok(())
    }

    /// Pads the sequence with `value` up to `length`, merging with the last
    /// run if it holds an identical value.
    pub fn extend_to(&mut self, length: i64, value: V) -> Result<()>
    where
        V: Identical,
    {
        let current = self.len();
        if length < current {
            return Err(Error::LengthTooShort {
                requested: length,
                required: current,
            });
        }
        if length > current {
            self.push_merged(length, value);
        }
        Ok(())
    }

    /// Appends a run ending at `end`, extending the last run instead if its
    /// value is identical. `end` must be greater than the current length.
    #[inline]
    pub(crate) fn push_merged(&mut self, end: i64, value: V)
    where
        V: Identical,
    {
        debug_assert!(end > self.len());
        match self.values.last() {
            Some(last) if last.identical(&value) => {
                *self
                    .ends
                    .last_mut()
                    .expect("ends and values have equal length") = end;
            }
            _ => {
                self.ends.push(end);
                self.values.push(value);
            }
        }
    }
}

impl<V: Identical> Rle<V> {
    /// Encodes a dense sequence, merging identical neighbours.
    pub fn from_dense<I: IntoIterator<Item = V>>(values: I) -> Self {
        let mut out = Rle::new();
        for (i, value) in values.into_iter().enumerate() {
            out.push_merged(i as i64 + 1, value);
        }
        out
    }
}

impl<V> Default for Rle<V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, V> RleRef<'a, V> {
    /// Borrows cumulative ends and values as a sequence, validating them.
    pub fn from_ends(ends: &'a [i64], values: &'a [V]) -> Result<Self> {
        check_same_len("ends and values", ends.len(), values.len())?;
        validate_ends(ends)?;
        Ok(RleRef { ends, values })
    }

    /// An empty sequence.
    pub fn empty() -> Self {
        RleRef {
            ends: &[],
            values: &[],
        }
    }

    pub(crate) fn new_unchecked(ends: &'a [i64], values: &'a [V]) -> Self {
        debug_assert_eq!(ends.len(), values.len());
        debug_assert!(validate_ends(ends).is_ok());
        RleRef { ends, values }
    }
}

/// Read-only operations shared by [`Rle`] and [`RleRef`].
///
/// Import it (or [`prelude`](crate::prelude)) to call these methods.
pub trait RleLike<V> {
    /// The cumulative end of each run.
    fn ends(&self) -> &[i64];
    /// One value per run.
    fn values(&self) -> &[V];

    /// A borrowed view of this sequence.
    fn view(&self) -> RleRef<'_, V> {
        RleRef::new_unchecked(self.ends(), self.values())
    }

    /// The number of positions the sequence covers.
    fn len(&self) -> i64 {
        self.ends().last().copied().unwrap_or(0)
    }

    fn is_empty(&self) -> bool {
        self.ends().is_empty()
    }

    fn num_runs(&self) -> usize {
        self.ends().len()
    }

    /// The length of each run.
    fn runs(&self) -> Vec<i64> {
        let mut prev = 0;
        self.ends()
            .iter()
            .map(|&end| {
                let run = end - prev;
                prev = end;
                run
            })
            .collect()
    }

    /// Iterates over `(start, end, value)` for each run.
    fn iter_runs<'a>(&'a self) -> impl Iterator<Item = (i64, i64, &'a V)>
    where
        V: 'a,
    {
        let ends = self.ends();
        ends.iter()
            .zip(self.values())
            .enumerate()
            .map(move |(i, (&end, value))| (if i == 0 { 0 } else { ends[i - 1] }, end, value))
    }

    /// Expands the sequence into one value per position.
    fn to_dense(&self) -> Vec<V>
    where
        V: Clone,
    {
        let mut out = Vec::with_capacity(self.len() as usize);
        for (start, end, value) in self.iter_runs() {
            out.extend(std::iter::repeat_n(value.clone(), (end - start) as usize));
        }
        out
    }

    /// Copies the sequence into an owned [`Rle`].
    fn to_rle(&self) -> Rle<V>
    where
        V: Clone,
    {
        Rle {
            ends: self.ends().to_vec(),
            values: self.values().to_vec(),
        }
    }

    /// The index of the run containing `pos`, if `pos` is in range.
    fn run_index(&self, pos: i64) -> Option<usize> {
        slice::run_index(self.ends(), pos)
    }

    /// The value at `pos`, if `pos` is in range.
    fn get(&self, pos: i64) -> Option<&V> {
        self.run_index(pos).map(|i| &self.values()[i])
    }

    /// The positions `start..end` as a new sequence starting at 0.
    ///
    /// The range is clamped to the sequence, like slicing in Python, so an
    /// out-of-range slice is shorter or empty rather than an error.
    fn slice(&self, start: i64, end: i64) -> Rle<V>
    where
        V: Clone,
    {
        slice::slice(self.view(), start, end)
    }

    /// Many slices at once, returned as a batch keyed by slice number.
    ///
    /// Each interval must have `0 <= start <= end`; ends past the sequence are
    /// clamped.
    fn getitems(&self, starts: &[i64], ends: &[i64]) -> Result<RleBatch<usize, V>>
    where
        V: Clone,
    {
        slice::getitems(self.view(), starts, ends)
    }

    /// The positions `start..end` without copying them, clamped like
    /// [`slice`](RleLike::slice). See [`SliceView`].
    fn slice_view(&self, start: i64, end: i64) -> SliceView<'_, V> {
        SliceView::new(self.view(), start, end)
    }

    /// Reduces many slices with `f` without copying them, in parallel, for
    /// example `rle.slice_reduce(&starts, &ends, |s| s.mean())`.
    ///
    /// Each interval must have `0 <= start <= end`; ends past the sequence are
    /// clamped.
    fn slice_reduce<'a, T, F>(&'a self, starts: &[i64], ends: &[i64], f: F) -> Result<Vec<T>>
    where
        V: Sync + 'a,
        T: Send,
        F: Fn(SliceView<'a, V>) -> T + Sync,
    {
        let rle = self.view();
        slice::slice_reduce(starts, ends, |_| rle, f)
    }

    /// Whether no two neighbouring runs hold identical values.
    fn is_merged(&self) -> bool
    where
        V: Identical,
    {
        merge::is_merged(self.values())
    }

    /// A copy with identical neighbours merged.
    fn merged(&self) -> Rle<V>
    where
        V: Clone + Identical,
    {
        merge::merged(self.view())
    }

    /// Groups runs by a mask over neighbouring pairs without touching values.
    /// See [`MergePlan`].
    fn merge_plan(&self, mask: &[bool]) -> Result<MergePlan> {
        merge::merge_plan(self.ends(), mask)
    }

    /// Whether both sequences represent the same values at every position,
    /// regardless of how the runs are split.
    fn equals<W: RleLike<V> + ?Sized>(&self, other: &W) -> bool
    where
        V: Identical,
    {
        if self.len() != other.len() {
            return false;
        }
        let (a, b) = (self.values(), other.values());
        Segments::new(self.ends(), other.ends()).all(|(_, i, j)| a[i].identical(&b[j]))
    }

    /// The plan for combining this sequence with `other`. See [`Plan`].
    fn plan_with<B, W: RleLike<B> + ?Sized>(&self, other: &W) -> Plan {
        plan::plan(self.ends(), other.ends())
    }

    /// Combines two sequences position by position and merges the result.
    /// See [`zip_with`](crate::zip_with).
    fn zip_with<B, C, W, F>(
        &self,
        other: &W,
        fill_a: Option<&V>,
        fill_b: Option<&B>,
        f: F,
    ) -> Result<Rle<C>>
    where
        W: RleLike<B> + ?Sized,
        C: Identical,
        F: FnMut(&V, &B) -> C,
    {
        ops::zip_with(self.view(), other.view(), fill_a, fill_b, f)
    }

    /// Like [`zip_with`](RleLike::zip_with) but without merging the result.
    fn zip_with_raw<B, C, W, F>(
        &self,
        other: &W,
        fill_a: Option<&V>,
        fill_b: Option<&B>,
        f: F,
    ) -> Result<Rle<C>>
    where
        W: RleLike<B> + ?Sized,
        F: FnMut(&V, &B) -> C,
    {
        ops::zip_with_raw(self.view(), other.view(), fill_a, fill_b, f)
    }

    /// Applies `f` to every run value and merges the result.
    fn map<W: Identical, F: FnMut(&V) -> W>(&self, mut f: F) -> Rle<W> {
        let mut out = Rle::new();
        for (&end, value) in self.ends().iter().zip(self.values()) {
            out.push_merged(end, f(value));
        }
        out
    }

    /// Applies `f` to every run value without merging the result.
    fn map_raw<W, F: FnMut(&V) -> W>(&self, f: F) -> Rle<W> {
        Rle {
            ends: self.ends().to_vec(),
            values: self.values().iter().map(f).collect(),
        }
    }

    /// Element-wise `==`, padding the shorter side with `V::default()`.
    fn elem_eq<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialEq + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a == b)
    }

    /// Element-wise `!=`, padding the shorter side with `V::default()`.
    fn elem_ne<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialEq + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a != b)
    }

    /// Element-wise `<`, padding the shorter side with `V::default()`.
    fn elem_lt<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialOrd + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a < b)
    }

    /// Element-wise `<=`, padding the shorter side with `V::default()`.
    fn elem_le<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialOrd + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a <= b)
    }

    /// Element-wise `>`, padding the shorter side with `V::default()`.
    fn elem_gt<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialOrd + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a > b)
    }

    /// Element-wise `>=`, padding the shorter side with `V::default()`.
    fn elem_ge<W: RleLike<V> + ?Sized>(&self, other: &W) -> Rle<bool>
    where
        V: PartialOrd + Default,
    {
        ops::compare(self.view(), other.view(), |a, b| a >= b)
    }

    /// Shifts values `distance` positions to the right (left if negative),
    /// keeping the length: positions shifted in hold `fill`, values shifted
    /// past either end are dropped. The result is merged.
    fn shift(&self, distance: i64, fill: V) -> Rle<V>
    where
        V: Clone + Identical,
    {
        transform::shift(self.view(), distance, fill)
    }

    /// The sequence back to front.
    fn reversed(&self) -> Rle<V>
    where
        V: Clone,
    {
        transform::reversed(self.view())
    }

    /// The half-open ranges `(starts, ends)` where `pred` holds, with
    /// touching ranges joined. For example, where coverage is positive.
    fn ranges_where<F: FnMut(&V) -> bool>(&self, pred: F) -> (Vec<i64>, Vec<i64>) {
        transform::ranges_where(self.view(), pred)
    }

    /// The sum over all positions (each value times its run length).
    fn sum(&self) -> V
    where
        V: Num,
    {
        reduce::sum(reduce::weighted(self.view()))
    }

    /// The mean over all positions, or `None` if the sequence is empty.
    fn mean(&self) -> Option<f64>
    where
        V: Num,
    {
        reduce::mean(reduce::weighted(self.view()), self.len())
    }

    /// The variance over all positions with `ddof` delta degrees of freedom
    /// (0 for population, 1 for sample). `None` if `len() <= ddof`.
    fn variance(&self, ddof: i64) -> Option<f64>
    where
        V: Num,
    {
        reduce::variance(reduce::weighted(self.view()), self.len(), ddof)
    }

    /// The square root of [`variance`](RleLike::variance).
    fn std(&self, ddof: i64) -> Option<f64>
    where
        V: Num,
    {
        self.variance(ddof).map(f64::sqrt)
    }

    /// The smallest value, ignoring values that are not comparable with
    /// themselves (such as `NaN`).
    fn min(&self) -> Option<&V>
    where
        V: PartialOrd,
    {
        reduce::extreme(self.values(), |a, b| a < b)
    }

    /// The largest value, ignoring values that are not comparable with
    /// themselves (such as `NaN`).
    fn max(&self) -> Option<&V>
    where
        V: PartialOrd,
    {
        reduce::extreme(self.values(), |a, b| a > b)
    }
}

impl<V> RleLike<V> for Rle<V> {
    fn ends(&self) -> &[i64] {
        &self.ends
    }
    fn values(&self) -> &[V] {
        &self.values
    }
}

impl<V> RleLike<V> for RleRef<'_, V> {
    fn ends(&self) -> &[i64] {
        self.ends
    }
    fn values(&self) -> &[V] {
        self.values
    }
}

/// Equality of the represented sequences; see [`RleLike::equals`].
impl<V: Identical> PartialEq for Rle<V> {
    fn eq(&self, other: &Self) -> bool {
        self.equals(other)
    }
}

impl<V: Identical> PartialEq for RleRef<'_, V> {
    fn eq(&self, other: &Self) -> bool {
        self.equals(other)
    }
}

fn debug_rle<V: fmt::Debug>(
    name: &str,
    rle: RleRef<'_, V>,
    f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
    f.debug_struct(name)
        .field("runs", &rle.runs())
        .field("values", &rle.values)
        .finish()
}

impl<V: fmt::Debug> fmt::Debug for Rle<V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        debug_rle("Rle", self.view(), f)
    }
}

impl<V: fmt::Debug> fmt::Debug for RleRef<'_, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        debug_rle("RleRef", *self, f)
    }
}