mirror of
https://github.com/storytold/gridcraft.git
synced 2026-10-24 02:36:48 -06:00
Clean-room spreadsheet engine: addresses/values/dates, Excel number formats, formula parser/printer/reference adjustment, worksheet function library, copy-on-write workbook model, dependency-driven recalculation with dynamic arrays, XLSX/CSV I/O, and a command engine (cell entry, clipboard, fill, formatting, sheets, sort/filter, tables, charts, CF, names, view, file). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
232 lines
8.9 KiB
Rust
232 lines
8.9 KiB
Rust
//! Copy-on-write sparse cell storage.
|
|
//!
|
|
//! Cells live in bands of 64 rows, each band behind an `Arc`. Cloning a store (an undo snapshot)
|
|
//! copies only the band pointers; editing a cell copies just its band.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::sync::Arc;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use sheetcraft_core::{CellRef, RangeRef};
|
|
|
|
use crate::cell::Cell;
|
|
|
|
const BAND: u32 = 64;
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
struct Band {
|
|
/// row → (col → cell)
|
|
rows: BTreeMap<u32, BTreeMap<u32, Cell>>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
|
pub struct CellStore {
|
|
bands: BTreeMap<u32, Arc<Band>>,
|
|
len: usize,
|
|
}
|
|
|
|
impl CellStore {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
pub fn len(&self) -> usize {
|
|
self.len
|
|
}
|
|
pub fn is_empty(&self) -> bool {
|
|
self.len == 0
|
|
}
|
|
pub fn get(&self, c: CellRef) -> Option<&Cell> {
|
|
self.bands.get(&(c.row / BAND))?.rows.get(&c.row)?.get(&c.col)
|
|
}
|
|
/// Mutable access (copies the band if shared).
|
|
pub fn get_mut(&mut self, c: CellRef) -> Option<&mut Cell> {
|
|
let band = self.bands.get_mut(&(c.row / BAND))?;
|
|
if !band.rows.get(&c.row).is_some_and(|r| r.contains_key(&c.col)) {
|
|
return None;
|
|
}
|
|
Arc::make_mut(band).rows.get_mut(&c.row)?.get_mut(&c.col)
|
|
}
|
|
/// Stores a cell; a default (empty, unstyled) cell is removed instead.
|
|
pub fn set(&mut self, c: CellRef, cell: Cell) {
|
|
if cell.is_blank() {
|
|
self.remove(c);
|
|
return;
|
|
}
|
|
let band = Arc::make_mut(self.bands.entry(c.row / BAND).or_default());
|
|
if band.rows.entry(c.row).or_default().insert(c.col, cell).is_none() {
|
|
self.len += 1;
|
|
}
|
|
}
|
|
pub fn remove(&mut self, c: CellRef) -> Option<Cell> {
|
|
let key = c.row / BAND;
|
|
let band = self.bands.get_mut(&key)?;
|
|
if !band.rows.get(&c.row).is_some_and(|r| r.contains_key(&c.col)) {
|
|
return None;
|
|
}
|
|
let b = Arc::make_mut(band);
|
|
let row = b.rows.get_mut(&c.row)?;
|
|
let old = row.remove(&c.col);
|
|
if row.is_empty() {
|
|
b.rows.remove(&c.row);
|
|
}
|
|
if b.rows.is_empty() {
|
|
self.bands.remove(&key);
|
|
}
|
|
if old.is_some() {
|
|
self.len -= 1;
|
|
}
|
|
old
|
|
}
|
|
/// Cells in `r`, row-major. Efficient for sparse data: skips empty bands and rows.
|
|
pub fn iter_range(&self, r: RangeRef) -> impl Iterator<Item = (CellRef, &Cell)> + '_ {
|
|
let (b0, b1) = (r.start.row / BAND, r.end.row / BAND);
|
|
self.bands.range(b0..=b1).flat_map(move |(_, band)| {
|
|
band.rows.range(r.start.row..=r.end.row).flat_map(move |(row, cols)| cols.range(r.start.col..=r.end.col).map(move |(col, cell)| (CellRef::new(*row, *col), cell)))
|
|
})
|
|
}
|
|
pub fn iter(&self) -> impl Iterator<Item = (CellRef, &Cell)> + '_ {
|
|
self.bands.values().flat_map(|band| band.rows.iter().flat_map(|(row, cols)| cols.iter().map(move |(col, cell)| (CellRef::new(*row, *col), cell))))
|
|
}
|
|
/// Cells of one row in `c0..=c1`.
|
|
pub fn row(&self, row: u32, c0: u32, c1: u32) -> impl Iterator<Item = (u32, &Cell)> + '_ {
|
|
self.bands.get(&(row / BAND)).and_then(|b| b.rows.get(&row)).into_iter().flat_map(move |cols| cols.range(c0..=c1).map(|(c, cell)| (*c, cell)))
|
|
}
|
|
/// Smallest range containing every stored cell, `None` when empty.
|
|
pub fn used_range(&self) -> Option<RangeRef> {
|
|
let first_band = self.bands.values().next()?;
|
|
let last_band = self.bands.values().next_back()?;
|
|
let r0 = *first_band.rows.keys().next()?;
|
|
let r1 = *last_band.rows.keys().next_back()?;
|
|
let mut c0 = u32::MAX;
|
|
let mut c1 = 0;
|
|
for band in self.bands.values() {
|
|
for cols in band.rows.values() {
|
|
if let (Some(a), Some(b)) = (cols.keys().next(), cols.keys().next_back()) {
|
|
c0 = c0.min(*a);
|
|
c1 = c1.max(*b);
|
|
}
|
|
}
|
|
}
|
|
if c0 == u32::MAX {
|
|
return None;
|
|
}
|
|
Some(RangeRef::new(CellRef::new(r0, c0), CellRef::new(r1, c1)))
|
|
}
|
|
/// Last stored row in column `col` at or above `below_or_at` (for Ctrl+arrow navigation).
|
|
pub fn has(&self, c: CellRef) -> bool {
|
|
self.get(c).is_some_and(|cell| !cell.value.is_empty() || cell.formula.is_some())
|
|
}
|
|
/// Removes every cell in `r` and returns them.
|
|
pub fn take_range(&mut self, r: RangeRef) -> Vec<(CellRef, Cell)> {
|
|
let keys: Vec<CellRef> = self.iter_range(r).map(|(c, _)| c).collect();
|
|
keys.into_iter().filter_map(|c| self.remove(c).map(|cell| (c, cell))).collect()
|
|
}
|
|
/// Rows strictly greater than or equal to `from` moved by `delta` (positive = down). Rows that
|
|
/// would land before `from`... callers delete the vacated block first.
|
|
pub fn shift_rows(&mut self, from: u32, delta: i64) {
|
|
let moved: Vec<(CellRef, Cell)> = {
|
|
let keys: Vec<CellRef> = self.iter().filter(|(c, _)| c.row >= from).map(|(c, _)| c).collect();
|
|
keys.into_iter().filter_map(|c| self.remove(c).map(|cell| (c, cell))).collect()
|
|
};
|
|
for (c, cell) in moved {
|
|
let nr = c.row as i64 + delta;
|
|
if (0..sheetcraft_core::MAX_ROWS as i64).contains(&nr) {
|
|
self.set(CellRef::new(nr as u32, c.col), cell);
|
|
}
|
|
}
|
|
}
|
|
pub fn shift_cols(&mut self, from: u32, delta: i64) {
|
|
let keys: Vec<CellRef> = self.iter().filter(|(c, _)| c.col >= from).map(|(c, _)| c).collect();
|
|
let moved: Vec<(CellRef, Cell)> = keys.into_iter().filter_map(|c| self.remove(c).map(|cell| (c, cell))).collect();
|
|
for (c, cell) in moved {
|
|
let nc = c.col as i64 + delta;
|
|
if (0..sheetcraft_core::MAX_COLS as i64).contains(&nc) {
|
|
self.set(CellRef::new(c.row, nc as u32), cell);
|
|
}
|
|
}
|
|
}
|
|
/// Shifts cells within rows `r0..=r1` and columns `>= from` horizontally (Insert/Delete Cells
|
|
/// with shift right/left).
|
|
pub fn shift_cols_in_rows(&mut self, r0: u32, r1: u32, from: u32, delta: i64) {
|
|
let range = RangeRef::new(CellRef::new(r0, from), CellRef::new(r1, sheetcraft_core::MAX_COLS - 1));
|
|
let moved = self.take_range(range);
|
|
for (c, cell) in moved {
|
|
let nc = c.col as i64 + delta;
|
|
if (0..sheetcraft_core::MAX_COLS as i64).contains(&nc) {
|
|
self.set(CellRef::new(c.row, nc as u32), cell);
|
|
}
|
|
}
|
|
}
|
|
/// Shifts cells within columns `c0..=c1` and rows `>= from` vertically.
|
|
pub fn shift_rows_in_cols(&mut self, c0: u32, c1: u32, from: u32, delta: i64) {
|
|
let range = RangeRef::new(CellRef::new(from, c0), CellRef::new(sheetcraft_core::MAX_ROWS - 1, c1));
|
|
let moved = self.take_range(range);
|
|
for (c, cell) in moved {
|
|
let nr = c.row as i64 + delta;
|
|
if (0..sheetcraft_core::MAX_ROWS as i64).contains(&nr) {
|
|
self.set(CellRef::new(nr as u32, c.col), cell);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use sheetcraft_core::Value;
|
|
|
|
fn c(r: u32, col: u32) -> CellRef {
|
|
CellRef::new(r, col)
|
|
}
|
|
|
|
#[test]
|
|
fn set_get_remove() {
|
|
let mut s = CellStore::new();
|
|
s.set(c(0, 0), Cell::value(1.0.into()));
|
|
s.set(c(100, 5), Cell::value("x".into()));
|
|
assert_eq!(s.len(), 2);
|
|
assert_eq!(s.get(c(100, 5)).map(|x| x.value.clone()), Some(Value::from("x")));
|
|
assert_eq!(s.used_range().unwrap().a1(), "A1:F101");
|
|
s.set(c(0, 0), Cell::default());
|
|
assert_eq!(s.len(), 1);
|
|
assert!(s.remove(c(100, 5)).is_some());
|
|
assert!(s.is_empty());
|
|
assert!(s.used_range().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn cow_snapshots() {
|
|
let mut s = CellStore::new();
|
|
for r in 0..1000 {
|
|
s.set(c(r, 0), Cell::value((r as f64).into()));
|
|
}
|
|
let snap = s.clone();
|
|
s.set(c(5, 0), Cell::value(99.0.into()));
|
|
assert_eq!(snap.get(c(5, 0)).unwrap().value, Value::Number(5.0));
|
|
assert_eq!(s.get(c(5, 0)).unwrap().value, Value::Number(99.0));
|
|
}
|
|
|
|
#[test]
|
|
fn range_iteration_order() {
|
|
let mut s = CellStore::new();
|
|
s.set(c(70, 2), Cell::value(3.0.into()));
|
|
s.set(c(1, 1), Cell::value(1.0.into()));
|
|
s.set(c(1, 3), Cell::value(2.0.into()));
|
|
s.set(c(500, 0), Cell::value(4.0.into()));
|
|
let v: Vec<f64> = s.iter_range(RangeRef::parse("A1:D100").unwrap()).map(|(_, x)| x.value.as_f64().unwrap()).collect();
|
|
assert_eq!(v, [1.0, 2.0, 3.0]);
|
|
}
|
|
|
|
#[test]
|
|
fn shifting() {
|
|
let mut s = CellStore::new();
|
|
s.set(c(2, 0), Cell::value(1.0.into()));
|
|
s.set(c(5, 0), Cell::value(2.0.into()));
|
|
s.shift_rows(3, 2);
|
|
assert!(s.get(c(7, 0)).is_some());
|
|
assert!(s.get(c(2, 0)).is_some());
|
|
s.shift_cols(0, 1);
|
|
assert!(s.get(c(7, 1)).is_some());
|
|
}
|
|
}
|