formualizer-eval 0.10.0

High-performance Arrow-backed Excel formula engine with dependency graph and incremental recalculation
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
//! Cells a legacy graph gave a vertex without a formula (decision 27 removed
//! those vertices): referenced cells (placeholders), value cells and spill
//! children. The graph's used extent (`used_row_bounds_for_columns`,
//! `used_col_bounds_for_rows`) counted them, and the evaluation-compat extent
//! of an open range falls back to it when a column (or row) has no value and
//! no formula. They are kept here as column runs, so that extent, and every
//! result that depends on it, stays what the legacy graph gave.
//!
//! Mutation takes `&mut` (no locking); queries run under `&self` from
//! evaluation threads and fold the pending cells in under the mutex.

use crate::SheetId;
use crate::engine::graph::editor::reference_adjuster::ShiftOperation;
use std::sync::Mutex;

/// Rows `r0..=r1` of one column: `sc` is `sheet << 16 | col` (0-based).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ExtentRun {
    sc: u32,
    r0: u32,
    r1: u32,
}

impl ExtentRun {
    fn sheet(self) -> SheetId {
        (self.sc >> 16) as SheetId
    }
    fn col(self) -> u32 {
        self.sc & 0xFFFF
    }
}

fn sc(sheet: SheetId, col: u32) -> u32 {
    (u32::from(sheet) << 16) | col.min(0xFFFF)
}

#[derive(Debug, Default)]
struct Inner {
    /// Disjoint runs sorted by `(sc, r0)`.
    runs: Vec<ExtentRun>,
    /// Cells not yet folded into `runs`: `sc << 32 | row`.
    pending: Vec<u64>,
}

impl Inner {
    fn fold(&mut self) {
        if self.pending.is_empty() {
            return;
        }
        self.pending.sort_unstable();
        self.pending.dedup();
        let pending = std::mem::take(&mut self.pending);
        let old = std::mem::take(&mut self.runs);
        let mut merged = Vec::with_capacity(old.len() + 16);
        let mut a = old.into_iter().peekable();
        let mut b = pending
            .into_iter()
            .map(|k| ExtentRun {
                sc: (k >> 32) as u32,
                r0: k as u32,
                r1: k as u32,
            })
            .peekable();
        loop {
            let next = match (a.peek(), b.peek()) {
                (Some(x), Some(y)) => {
                    if x <= y {
                        a.next()
                    } else {
                        b.next()
                    }
                }
                (Some(_), None) => a.next(),
                (None, Some(_)) => b.next(),
                (None, None) => break,
            };
            push_coalesced(&mut merged, next.unwrap());
        }
        self.runs = merged;
    }

    fn normalize(&mut self) {
        self.runs.sort_unstable();
        let runs = std::mem::take(&mut self.runs);
        let mut out = Vec::with_capacity(runs.len());
        for r in runs {
            push_coalesced(&mut out, r);
        }
        self.runs = out;
    }

    fn sheet_span(&self, sheet: SheetId) -> std::ops::Range<usize> {
        let lo = sc(sheet, 0);
        let hi = sc(sheet, 0xFFFF);
        let a = self.runs.partition_point(|r| r.sc < lo);
        let b = self.runs.partition_point(|r| r.sc <= hi);
        a..b
    }
}

fn push_coalesced(out: &mut Vec<ExtentRun>, r: ExtentRun) {
    if let Some(last) = out.last_mut()
        && last.sc == r.sc
        && r.r0 <= last.r1.saturating_add(1)
    {
        last.r1 = last.r1.max(r.r1);
        return;
    }
    out.push(r);
}

#[derive(Debug, Default)]
pub(crate) struct ExtentRecord {
    inner: Mutex<Inner>,
}

impl ExtentRecord {
    fn inner(&mut self) -> &mut Inner {
        self.inner.get_mut().unwrap_or_else(|e| e.into_inner())
    }

    fn locked(&self) -> std::sync::MutexGuard<'_, Inner> {
        let mut g = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        g.fold();
        g
    }

    /// Note the cell at 0-based `(row, col)`.
    pub(crate) fn note(&mut self, sheet: SheetId, row: u32, col: u32) {
        let inner = self.inner();
        inner
            .pending
            .push((u64::from(sc(sheet, col)) << 32) | u64::from(row));
        if inner.pending.len() >= 4096 && inner.pending.len() >= inner.runs.len() {
            inner.fold();
        }
    }

    /// Fold the pending cells into the runs now (end of a load: the notes a
    /// load leaves pending would otherwise be sorted in by a later edit).
    pub(crate) fn fold_pending(&mut self) {
        self.inner().fold();
    }

    /// Forget the cells of `rows x cols` (0-based, inclusive). Returns the
    /// forgotten cells as `(col, r0, r1)` runs.
    pub(crate) fn forget_rect(
        &mut self,
        sheet: SheetId,
        rows: (u32, u32),
        cols: (u32, u32),
    ) -> Vec<(u32, u32, u32)> {
        let inner = self.inner();
        inner.fold();
        let span = inner.sheet_span(sheet);
        let mut forgotten = Vec::new();
        if span.is_empty() {
            return forgotten;
        }
        let mut changed = false;
        let mut out = Vec::with_capacity(inner.runs.len() + 1);
        out.extend_from_slice(&inner.runs[..span.start]);
        for &r in &inner.runs[span.clone()] {
            let col = r.col();
            if col < cols.0 || col > cols.1 || r.r1 < rows.0 || r.r0 > rows.1 {
                out.push(r);
                continue;
            }
            changed = true;
            forgotten.push((col, r.r0.max(rows.0), r.r1.min(rows.1)));
            if r.r0 < rows.0 {
                out.push(ExtentRun {
                    r1: rows.0 - 1,
                    ..r
                });
            }
            if r.r1 > rows.1 {
                out.push(ExtentRun {
                    r0: rows.1 + 1,
                    ..r
                });
            }
        }
        if changed {
            out.extend_from_slice(&inner.runs[span.end..]);
            inner.runs = out;
        }
        forgotten
    }

    /// Shift for a structural edit (pre-edit frame). Returns the runs a
    /// delete dropped (undo brings them back with [`Self::restore`]).
    pub(crate) fn shift(&mut self, op: &ShiftOperation) -> Vec<ExtentRun> {
        let (sheet, rows, start, count, insert) = match *op {
            ShiftOperation::InsertRows {
                sheet_id,
                before,
                count,
            } => (sheet_id, true, before, count, true),
            ShiftOperation::DeleteRows {
                sheet_id,
                start,
                count,
            } => (sheet_id, true, start, count, false),
            ShiftOperation::InsertColumns {
                sheet_id,
                before,
                count,
            } => (sheet_id, false, before, count, true),
            ShiftOperation::DeleteColumns {
                sheet_id,
                start,
                count,
            } => (sheet_id, false, start, count, false),
        };
        let inner = self.inner();
        inner.fold();
        let span = inner.sheet_span(sheet);
        let mut dropped = Vec::new();
        if span.is_empty() || count == 0 {
            return dropped;
        }
        let end = start.saturating_add(count);
        let mut moved = Vec::with_capacity(span.len() + 1);
        for &r in &inner.runs[span.clone()] {
            if rows {
                if r.r1 < start {
                    moved.push(r);
                } else if insert {
                    if r.r0 >= start {
                        moved.push(ExtentRun {
                            r0: r.r0.saturating_add(count),
                            r1: r.r1.saturating_add(count),
                            ..r
                        });
                    } else {
                        moved.push(ExtentRun { r1: start - 1, ..r });
                        moved.push(ExtentRun {
                            r0: end,
                            r1: r.r1.saturating_add(count),
                            ..r
                        });
                    }
                } else {
                    if r.r0 < start {
                        moved.push(ExtentRun { r1: start - 1, ..r });
                    }
                    let (d0, d1) = (r.r0.max(start), r.r1.min(end - 1));
                    if d0 <= d1 {
                        dropped.push(ExtentRun {
                            r0: d0,
                            r1: d1,
                            ..r
                        });
                    }
                    if r.r1 >= end {
                        moved.push(ExtentRun {
                            r0: r.r0.max(end) - count,
                            r1: r.r1 - count,
                            ..r
                        });
                    }
                }
            } else {
                let col = r.col();
                if col < start {
                    moved.push(r);
                } else if insert {
                    moved.push(ExtentRun {
                        sc: sc(r.sheet(), col.saturating_add(count)),
                        ..r
                    });
                } else if col < end {
                    dropped.push(r);
                } else {
                    moved.push(ExtentRun {
                        sc: sc(r.sheet(), col - count),
                        ..r
                    });
                }
            }
        }
        inner.runs.splice(span, moved);
        inner.normalize();
        dropped
    }

    /// Runs a delete dropped come back (after the band is inserted again).
    pub(crate) fn restore(&mut self, runs: Vec<ExtentRun>) {
        if runs.is_empty() {
            return;
        }
        let inner = self.inner();
        inner.fold();
        inner.runs.extend(runs);
        inner.normalize();
    }

    /// A sheet is removed.
    pub(crate) fn drop_sheet(&mut self, sheet: SheetId) {
        let inner = self.inner();
        inner.fold();
        let span = inner.sheet_span(sheet);
        inner.runs.drain(span);
    }

    /// Min/max 0-based row noted in columns `c0..=c1`.
    pub(crate) fn row_bounds_for_cols(
        &self,
        sheet: SheetId,
        c0: u32,
        c1: u32,
    ) -> Option<(u32, u32)> {
        let g = self.locked();
        if g.runs.is_empty() || c0 > c1 {
            return None;
        }
        let lo = sc(sheet, c0);
        let hi = sc(sheet, c1);
        let a = g.runs.partition_point(|r| r.sc < lo);
        let mut out: Option<(u32, u32)> = None;
        for r in g.runs[a..].iter().take_while(|r| r.sc <= hi) {
            out = Some(out.map_or((r.r0, r.r1), |(m, x)| (m.min(r.r0), x.max(r.r1))));
        }
        out
    }

    /// Min/max 0-based column noted in rows `r0..=r1`.
    pub(crate) fn col_bounds_for_rows(
        &self,
        sheet: SheetId,
        r0: u32,
        r1: u32,
    ) -> Option<(u32, u32)> {
        let g = self.locked();
        if g.runs.is_empty() {
            return None;
        }
        let span = g.sheet_span(sheet);
        let mut out: Option<(u32, u32)> = None;
        for r in &g.runs[span] {
            if r.r1 >= r0 && r.r0 <= r1 {
                let c = r.col();
                out = Some(out.map_or((c, c), |(m, x)| (m.min(c), x.max(c))));
            }
        }
        out
    }

    /// Whether the cell at 0-based `(row, col)` is noted.
    pub(crate) fn contains(&self, sheet: SheetId, row: u32, col: u32) -> bool {
        let g = self.locked();
        let key = sc(sheet, col);
        let i = g.runs.partition_point(|r| (r.sc, r.r0) <= (key, row));
        i > 0 && {
            let r = g.runs[i - 1];
            r.sc == key && r.r0 <= row && row <= r.r1
        }
    }

    /// Columns of `sheet` with a noted cell, ascending.
    pub(crate) fn columns(&self, sheet: SheetId) -> Vec<u32> {
        let g = self.locked();
        let mut out: Vec<u32> = g.runs[g.sheet_span(sheet)]
            .iter()
            .map(|r| r.col())
            .collect();
        out.dedup();
        out
    }

    /// Cells noted but not folded yet (tests).
    #[cfg(test)]
    pub(crate) fn pending_len(&self) -> usize {
        self.inner
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .pending
            .len()
    }

    /// Runs held (after folding pending cells): tests, accounting.
    pub(crate) fn run_count(&self) -> usize {
        self.locked().runs.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cells(rec: &ExtentRecord, sheet: SheetId) -> Vec<(u32, u32)> {
        let g = rec.locked();
        let mut out = Vec::new();
        for r in &g.runs[g.sheet_span(sheet)] {
            for row in r.r0..=r.r1 {
                out.push((row, r.col()));
            }
        }
        out.sort_unstable();
        out
    }

    #[test]
    fn notes_coalesce_into_runs_and_answer_bounds() {
        let mut rec = ExtentRecord::default();
        for row in (10..20).rev() {
            rec.note(1, row, 3);
        }
        rec.note(1, 15, 3);
        rec.note(1, 40, 3);
        rec.note(1, 5, 7);
        rec.note(2, 99, 3);
        assert_eq!(rec.run_count(), 4);
        assert_eq!(rec.row_bounds_for_cols(1, 3, 3), Some((10, 40)));
        assert_eq!(rec.row_bounds_for_cols(1, 0, 10), Some((5, 40)));
        assert_eq!(rec.row_bounds_for_cols(1, 4, 6), None);
        assert_eq!(rec.col_bounds_for_rows(1, 0, 9), Some((7, 7)));
        assert_eq!(rec.col_bounds_for_rows(1, 12, 12), Some((3, 3)));
        assert_eq!(rec.col_bounds_for_rows(1, 21, 39), None);
        assert_eq!(rec.col_bounds_for_rows(2, 0, 1000), Some((3, 3)));
        assert!(rec.contains(1, 10, 3) && rec.contains(1, 19, 3) && rec.contains(1, 40, 3));
        assert!(!rec.contains(1, 9, 3) && !rec.contains(1, 20, 3) && !rec.contains(1, 10, 4));
        assert!(!rec.contains(3, 10, 3));
    }

    #[test]
    fn row_delete_drops_the_band_and_undo_restores_it() {
        let mut rec = ExtentRecord::default();
        for row in 0..10 {
            rec.note(1, row, 2);
        }
        rec.note(1, 4, 5);
        let before = cells(&rec, 1);
        let op = ShiftOperation::DeleteRows {
            sheet_id: 1,
            start: 3,
            count: 4,
        };
        let dropped = rec.shift(&op);
        assert_eq!(
            cells(&rec, 1),
            vec![(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2)]
        );
        rec.shift(&ShiftOperation::InsertRows {
            sheet_id: 1,
            before: 3,
            count: 4,
        });
        rec.restore(dropped);
        assert_eq!(cells(&rec, 1), before);
    }

    #[test]
    fn row_insert_splits_a_run_and_column_edits_move_whole_columns() {
        let mut rec = ExtentRecord::default();
        for row in 0..6 {
            rec.note(3, row, 1);
        }
        rec.note(3, 2, 4);
        rec.shift(&ShiftOperation::InsertRows {
            sheet_id: 3,
            before: 3,
            count: 2,
        });
        assert_eq!(
            cells(&rec, 3),
            vec![(0, 1), (1, 1), (2, 1), (2, 4), (5, 1), (6, 1), (7, 1)]
        );
        let dropped = rec.shift(&ShiftOperation::DeleteColumns {
            sheet_id: 3,
            start: 1,
            count: 2,
        });
        assert_eq!(dropped.len(), 2);
        assert_eq!(cells(&rec, 3), vec![(2, 2)]);
        rec.shift(&ShiftOperation::InsertColumns {
            sheet_id: 3,
            before: 0,
            count: 1,
        });
        assert_eq!(cells(&rec, 3), vec![(2, 3)]);
    }

    #[test]
    fn forget_rect_splits_runs_and_drop_sheet_clears_one_sheet() {
        let mut rec = ExtentRecord::default();
        for row in 0..10 {
            rec.note(1, row, 1);
            rec.note(2, row, 1);
        }
        assert_eq!(rec.forget_rect(1, (3, 4), (0, 5)), vec![(1, 3, 4)]);
        assert_eq!(rec.row_bounds_for_cols(1, 1, 1), Some((0, 9)));
        assert_eq!(rec.col_bounds_for_rows(1, 3, 4), None);
        rec.forget_rect(1, (0, 9), (1, 1));
        assert_eq!(rec.row_bounds_for_cols(1, 1, 1), None);
        rec.drop_sheet(2);
        assert_eq!(rec.run_count(), 0);
    }
}