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
//! `Sheet`: the sparse-matrix data model for a single worksheet, including
//! transparent merged-cell access.
use crate::model::cell::{Cell, CellRef};
use std::collections::HashMap;
/// A merged range. Holds the top-left (origin cell) and bottom-right
/// coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MergedRegion {
/// Origin cell (holds the actual data).
pub start: CellRef,
pub end: CellRef,
}
impl MergedRegion {
/// Callers (`resolve/merge.rs`) are expected to validate that `start <=
/// end` before constructing a region; this is asserted (debug-only) so
/// a violation is caught during testing rather than silently
/// underflowing `u32` in release builds.
pub fn row_span(&self) -> u32 {
debug_assert!(self.start.row <= self.end.row);
self.end.row - self.start.row + 1
}
pub fn col_span(&self) -> u32 {
debug_assert!(self.start.col <= self.end.col);
self.end.col - self.start.col + 1
}
}
/// A sheet's visibility (`workbook.xml`'s `<sheet state="...">`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SheetVisibility {
Visible,
Hidden,
VeryHidden,
}
/// Sparse matrix data for a single sheet.
#[derive(Debug, Clone)]
pub struct Sheet {
pub name: String,
pub visibility: SheetVisibility,
cells: HashMap<CellRef, Cell>,
/// origin cell coordinate -> merged region. Also doubles as the source
/// of truth for resolving a virtual coordinate to its origin (see
/// `resolve_origin`) via geometric containment, rather than
/// materializing an alias entry for every cell in the region up front.
/// An earlier design did the latter (`HashMap<CellRef, CellRef>`,
/// populated by iterating every row/col in the range inside
/// `insert_merge`), but that costs O(row_span * col_span) — unbounded
/// for a legitimate full-sheet merge like `A1:XFD1048576` (~17 billion
/// cells) — and was found to hang in practice (see the regression test
/// `insert_merge_on_huge_region_does_not_hang`).
merged_regions: HashMap<CellRef, MergedRegion>,
/// Union bounding box (min_row, max_row, min_col, max_col) across every
/// merged region's `start`/`end`. `None` when there are no merges. Lets
/// `resolve_origin` reject a coordinate clearly outside every merge in
/// O(1), before falling back to the O(N) per-region scan — most cells
/// on a sheet with merges concentrated in one area are outside all of
/// them (PR #23 review). May end up looser than the tightest possible
/// box after a same-origin `insert_merge` overwrite shrinks a region
/// (the old, larger bound is never retracted); that only costs a missed
/// early exit, never correctness, since the full scan below is still
/// authoritative.
merge_bounds: Option<(u32, u32, u32, u32)>,
/// The largest row/column number among inserted cells. Updated
/// incrementally on each cell insertion; does not depend on the
/// `<dimension>` element's value.
pub max_row: u32,
pub max_col: u32,
}
impl Sheet {
/// Constructs a new, empty sheet. Called by `pipeline::run` to build
/// sheets from `parse/workbook.rs`'s output.
pub(crate) fn new(name: String, visibility: SheetVisibility) -> Self {
Self {
name,
visibility,
cells: HashMap::new(),
merged_regions: HashMap::new(),
merge_bounds: None,
max_row: 0,
max_col: 0,
}
}
/// Resolves `r` to a merged region's origin coordinate, if `r` falls
/// inside one; otherwise returns `r` unchanged. First rejects `r` in
/// O(1) via `merge_bounds` if it falls outside every merge's combined
/// bounding box; otherwise falls back to a linear scan over
/// `merged_regions`. Real-world sheets have at most a few thousand
/// merged regions regardless of sheet dimensions, so even the fallback
/// scan stays cheap — the same "simple O(N) is fine for expected-small
/// N" tradeoff `resolve::merge`'s overlap validation already makes.
fn resolve_origin(&self, r: CellRef) -> CellRef {
let Some((min_row, max_row, min_col, max_col)) = self.merge_bounds else {
return r;
};
if r.row < min_row || r.row > max_row || r.col < min_col || r.col > max_col {
return r;
}
self.merged_regions
.values()
.find(|region| {
r.row >= region.start.row
&& r.row <= region.end.row
&& r.col >= region.start.col
&& r.col <= region.end.col
})
.map_or(r, |region| region.start)
}
/// Retrieves a cell, resolving the merged-cell alias if needed. Returns
/// the same `Cell` whether passed the origin or a virtual coordinate.
pub fn get(&self, r: CellRef) -> Option<&Cell> {
let origin = self.resolve_origin(r);
self.cells.get(&origin)
}
/// Retrieves a mutable reference to a cell, resolving the merged-cell
/// alias if needed. Used by `resolve/shared_strings.rs` and
/// `resolve/style.rs` to rewrite a cell's value/style with resolved
/// data.
pub(crate) fn get_mut(&mut self, r: CellRef) -> Option<&mut Cell> {
let origin = self.resolve_origin(r);
self.cells.get_mut(&origin)
}
/// Inserts a cell while updating max_row/max_col at the same time.
/// Writes to `cells` only ever go through this method, structurally
/// preventing the dimension fields from going out of sync.
pub(crate) fn insert_cell(&mut self, r: CellRef, cell: Cell) {
self.max_row = self.max_row.max(r.row);
self.max_col = self.max_col.max(r.col);
self.cells.insert(r, cell);
}
/// Registers a merged region, keyed by its origin cell, in
/// `merged_regions` (`get`/`get_mut`/`iter_cells` resolve membership
/// geometrically via `resolve_origin` — see that method and the
/// `merged_regions` field doc for why no per-cell alias is
/// materialized here). If the origin cell does not yet exist in
/// `cells` (a merged range with neither value nor formatting), a blank
/// placeholder cell is inserted first, so `iter_cells` always picks up
/// the origin cell. Calling this again for the same origin overwrites
/// the previous region (last-write-wins).
pub(crate) fn insert_merge(&mut self, region: MergedRegion) {
debug_assert!(region.start.row <= region.end.row);
debug_assert!(region.start.col <= region.end.col);
if !self.cells.contains_key(®ion.start) {
self.insert_cell(
region.start,
Cell {
value: None,
style: None,
},
);
}
self.merged_regions.insert(region.start, region);
let (min_row, max_row, min_col, max_col) = self.merge_bounds.unwrap_or((
region.start.row,
region.end.row,
region.start.col,
region.end.col,
));
self.merge_bounds = Some((
min_row.min(region.start.row),
max_row.max(region.end.row),
min_col.min(region.start.col),
max_col.max(region.end.col),
));
self.max_row = self.max_row.max(region.end.row);
self.max_col = self.max_col.max(region.end.col);
}
/// Retrieves, in O(1), the merged region an origin cell belongs to (used
/// by `json.rs` to compute row_span/col_span).
pub fn merged_region_at(&self, origin: CellRef) -> Option<&MergedRegion> {
self.merged_regions.get(&origin)
}
/// An iterator over origin cells only (for JSON generation). A
/// coordinate that falls inside a merged region but isn't that region's
/// origin is excluded even if `cells` holds an entry for it:
/// `parse/worksheet.rs` inserts a `Cell` for every `<c>` element it
/// streams, including ones inside a merged range that later turn out
/// not to be the origin (e.g. a virtual cell carrying only border
/// styling), so `cells` cannot be assumed to hold origin cells
/// exclusively (PR #20 review).
pub fn iter_cells(&self) -> impl Iterator<Item = (CellRef, &Cell)> {
self.cells
.iter()
.filter(|(&r, _)| self.resolve_origin(r) == r)
.map(|(&r, c)| (r, c))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn r(row: u32, col: u32) -> CellRef {
CellRef { row, col }
}
#[test]
fn get_on_blank_cell_returns_none() {
let sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
assert!(sheet.get(r(1, 1)).is_none());
}
#[test]
fn get_on_virtual_coordinate_returns_origin_cell() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(1, 1),
Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(3, 3),
});
let origin = sheet.get(r(1, 1)).cloned();
let virtual_cell = sheet.get(r(2, 2)).cloned();
assert_eq!(origin, virtual_cell);
assert!(virtual_cell.is_some());
}
#[test]
fn get_mut_resolves_alias_and_allows_mutation() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(1, 1),
Cell {
value: Some(crate::model::CellValue::Boolean(false)),
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 2),
});
// Mutating through a virtual coordinate rewrites the origin cell.
sheet.get_mut(r(2, 2)).unwrap().value = Some(crate::model::CellValue::Boolean(true));
assert_eq!(
sheet.get(r(1, 1)).unwrap().value,
Some(crate::model::CellValue::Boolean(true))
);
assert!(sheet.get_mut(r(9, 9)).is_none());
}
#[test]
fn merged_region_span() {
let one_by_one = MergedRegion {
start: r(1, 1),
end: r(1, 1),
};
assert_eq!(one_by_one.row_span(), 1);
assert_eq!(one_by_one.col_span(), 1);
let large = MergedRegion {
start: r(2, 3),
end: r(10, 20),
};
assert_eq!(large.row_span(), 9);
assert_eq!(large.col_span(), 18);
}
#[test]
fn merged_region_at_lookup() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
let region = MergedRegion {
start: r(1, 1),
end: r(2, 2),
};
sheet.insert_merge(region);
assert_eq!(sheet.merged_region_at(r(1, 1)), Some(®ion));
assert_eq!(sheet.merged_region_at(r(2, 2)), None);
}
#[test]
fn iter_cells_only_yields_origin_cells() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(1, 1),
Cell {
value: None,
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 2),
});
let coords: Vec<CellRef> = sheet.iter_cells().map(|(coord, _)| coord).collect();
assert_eq!(coords, vec![r(1, 1)]);
}
#[test]
fn iter_cells_excludes_cells_pre_inserted_at_alias_coordinates() {
// parse/worksheet.rs streams a `<c>` element for every cell it
// encounters, including ones inside a merged range that later turn
// out not to be the origin (e.g. border-only styling on B2 within
// A1:B2). insert_merge must not let a pre-existing `cells` entry at
// an alias coordinate leak into iter_cells.
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(1, 1),
Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
},
);
sheet.insert_cell(
r(2, 2),
Cell {
value: None,
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 2),
});
let coords: Vec<CellRef> = sheet.iter_cells().map(|(coord, _)| coord).collect();
assert_eq!(coords, vec![r(1, 1)]);
}
#[test]
fn insert_cell_updates_max_row_col() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(5, 2),
Cell {
value: None,
style: None,
},
);
assert_eq!(sheet.max_row, 5);
assert_eq!(sheet.max_col, 2);
sheet.insert_cell(
r(3, 9),
Cell {
value: None,
style: None,
},
);
assert_eq!(sheet.max_row, 5);
assert_eq!(sheet.max_col, 9);
}
#[test]
fn insert_merge_backfills_blank_origin_cell() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 2),
});
let origin_cell = sheet.get(r(1, 1));
assert_eq!(
origin_cell,
Some(&Cell {
value: None,
style: None,
})
);
assert_eq!(
sheet.merged_region_at(r(1, 1)),
Some(&MergedRegion {
start: r(1, 1),
end: r(2, 2),
})
);
let coords: Vec<CellRef> = sheet.iter_cells().map(|(coord, _)| coord).collect();
assert_eq!(coords, vec![r(1, 1)]);
}
#[test]
fn insert_merge_expands_used_range_from_only_a1_data() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(1, 1),
Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
},
);
assert_eq!(sheet.max_row, 1);
assert_eq!(sheet.max_col, 1);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(3, 3),
});
assert_eq!(sheet.max_row, 3);
assert_eq!(sheet.max_col, 3);
}
#[test]
fn insert_merge_on_huge_region_does_not_hang() {
// Regression test: a full-sheet merge (Excel's actual maximum
// dimensions, ~17 billion cells) must register in roughly constant
// time, not time proportional to its area. An earlier
// implementation populated a `HashMap<CellRef, CellRef>` alias
// entry for every cell in the region inside `insert_merge`, which
// hung in practice on a range this size.
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
let huge = MergedRegion {
start: r(1, 1),
end: r(1_048_576, 16_384),
};
sheet.insert_merge(huge);
assert_eq!(sheet.get(r(1, 1)), sheet.get(r(500_000, 8_000)));
assert_eq!(sheet.merged_region_at(r(1, 1)), Some(&huge));
assert_eq!(sheet.max_row, 1_048_576);
assert_eq!(sheet.max_col, 16_384);
}
#[test]
fn get_outside_any_merged_region_is_unaffected_by_other_regions() {
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(10, 10),
Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 2),
});
// r(10, 10) falls inside no merged region, so it must resolve to
// itself rather than being swept into the unrelated A1:B2 region.
assert_eq!(
sheet.get(r(10, 10)),
Some(&Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
})
);
}
#[test]
fn get_inside_bounding_box_but_outside_any_region_resolves_to_itself() {
// Two merges with a gap between them: their combined merge_bounds
// spans rows 1-10, but row 5 (inside the bounds, outside both
// regions) must still resolve to itself rather than being
// incorrectly swept into a region by the O(1) bounds pre-check.
let mut sheet = Sheet::new("Sheet1".into(), SheetVisibility::Visible);
sheet.insert_cell(
r(5, 1),
Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
},
);
sheet.insert_merge(MergedRegion {
start: r(1, 1),
end: r(2, 1),
});
sheet.insert_merge(MergedRegion {
start: r(9, 1),
end: r(10, 1),
});
assert_eq!(
sheet.get(r(5, 1)),
Some(&Cell {
value: Some(crate::model::CellValue::Boolean(true)),
style: None,
})
);
}
}