#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GridCell {
pub row: usize,
pub index: usize,
pub column: usize,
pub rowspan: usize,
pub colspan: usize,
}
impl GridCell {
#[must_use]
pub const fn rows(&self) -> std::ops::Range<usize> {
self.row..self.row + self.rowspan
}
#[must_use]
pub const fn columns(&self) -> std::ops::Range<usize> {
self.column..self.column + self.colspan
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct GridRect {
pub top: usize,
pub left: usize,
pub bottom: usize,
pub right: usize,
}
impl GridRect {
#[must_use]
pub const fn of(cell: &GridCell) -> Self {
Self {
top: cell.row,
left: cell.column,
bottom: cell.row + cell.rowspan,
right: cell.column + cell.colspan,
}
}
#[must_use]
pub fn union(self, other: Self) -> Self {
Self {
top: self.top.min(other.top),
left: self.left.min(other.left),
bottom: self.bottom.max(other.bottom),
right: self.right.max(other.right),
}
}
#[must_use]
pub const fn overlaps(&self, other: &Self) -> bool {
self.top < other.bottom
&& other.top < self.bottom
&& self.left < other.right
&& other.left < self.right
}
}
pub const MAX_COLSPAN: usize = 1_000;
pub const MAX_ROWSPAN: usize = 65_534;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TableMap {
width: usize,
height: usize,
cells: Vec<GridCell>,
slots: Vec<Vec<Option<usize>>>,
}
impl TableMap {
#[must_use]
pub fn new<R, C>(rows: R) -> Self
where
R: IntoIterator<Item = C>,
C: IntoIterator<Item = (usize, usize)>,
{
let rows: Vec<Vec<(usize, usize)>> = rows
.into_iter()
.map(|cells| cells.into_iter().collect())
.collect();
let height = rows.len();
let mut slots: Vec<Vec<Option<usize>>> = vec![Vec::new(); height];
let mut cells = Vec::new();
for (row, spans) in rows.iter().enumerate() {
let mut column = 0;
for (index, &(colspan, rowspan)) in spans.iter().enumerate() {
while slots[row].get(column).is_some_and(Option::is_some) {
column += 1;
}
let free = slots[row]
.get(column..)
.unwrap_or_default()
.iter()
.take_while(|slot| slot.is_none())
.count();
let colspan = if column + free < slots[row].len() {
colspan.clamp(1, free)
} else {
colspan.clamp(1, MAX_COLSPAN)
};
let cell = GridCell {
row,
index,
column,
rowspan: rowspan.clamp(1, height - row),
colspan,
};
for covered in cell.rows() {
let line = &mut slots[covered];
if line.len() < cell.column + cell.colspan {
line.resize(cell.column + cell.colspan, None);
}
for slot in &mut line[cell.columns()] {
*slot = Some(cells.len());
}
}
column += cell.colspan;
cells.push(cell);
}
}
let width = slots.iter().map(Vec::len).max().unwrap_or(0);
for line in &mut slots {
line.resize(width, None);
}
Self {
width,
height,
cells,
slots,
}
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
#[must_use]
pub const fn height(&self) -> usize {
self.height
}
#[must_use]
pub fn cells(&self) -> &[GridCell] {
&self.cells
}
#[must_use]
pub fn cell_at(&self, row: usize, column: usize) -> Option<usize> {
self.slots.get(row)?.get(column).copied().flatten()
}
#[must_use]
pub fn find(&self, row: usize, index: usize) -> Option<usize> {
let start = self.cells.partition_point(|cell| cell.row < row);
self.cells[start..]
.iter()
.position(|cell| cell.row == row && cell.index == index)
.map(|offset| start + offset)
}
#[must_use]
pub fn insert_index(&self, row: usize, column: usize) -> usize {
self.cells
.iter()
.filter(|cell| cell.row == row && cell.column < column)
.count()
}
#[must_use]
pub fn cells_in(&self, rect: GridRect) -> Vec<usize> {
(0..self.cells.len())
.filter(|&index| GridRect::of(&self.cells[index]).overlaps(&rect))
.collect()
}
#[must_use]
pub fn expand(&self, rect: GridRect) -> GridRect {
let mut rect = rect;
loop {
let grown = self
.cells_in(rect)
.into_iter()
.map(|index| GridRect::of(&self.cells[index]))
.fold(rect, GridRect::union);
if grown == rect {
return rect;
}
rect = grown;
}
}
#[must_use]
pub fn is_full(&self) -> bool {
self.slots.iter().flatten().all(Option::is_some)
}
#[must_use]
pub fn missing(&self) -> Vec<usize> {
self.slots
.iter()
.map(|line| line.iter().filter(|slot| slot.is_none()).count())
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn map(rows: &[&[(usize, usize)]]) -> TableMap {
TableMap::new(rows.iter().map(|row| row.iter().copied()))
}
#[test]
fn plain_cells_fill_a_grid() {
let table = map(&[&[(1, 1), (1, 1)], &[(1, 1), (1, 1)]]);
assert_eq!((table.width(), table.height()), (2, 2));
assert_eq!(table.cell_at(1, 1), Some(3));
assert!(table.is_full());
}
#[test]
fn rowspans_push_later_cells_right() {
let table = map(&[&[(1, 2), (1, 1)], &[(1, 1)]]);
assert_eq!(table.cell_at(1, 0), Some(0));
assert_eq!(table.cells()[2].column, 1);
assert_eq!(table.insert_index(1, 1), 0);
assert!(table.is_full());
}
#[test]
fn short_rows_leave_empty_slots_and_long_rowspans_are_clamped() {
let table = map(&[&[(2, 5)], &[]]);
assert_eq!(table.cells()[0].rowspan, 2);
assert!(table.is_full());
let ragged = map(&[&[(1, 1), (1, 1), (1, 1)], &[(1, 1)]]);
assert_eq!(ragged.missing(), vec![0, 2]);
assert!(!ragged.is_full());
}
#[test]
fn colspans_are_clamped_as_browsers_clamp_them() {
let table = map(&[&[(1, 1), (usize::MAX, 1)], &[(5_000, 1)]]);
assert_eq!(table.cells()[1].colspan, MAX_COLSPAN);
assert_eq!(table.cells()[2].colspan, MAX_COLSPAN);
assert_eq!(table.width(), 1 + MAX_COLSPAN);
}
#[test]
fn expand_grows_to_whole_merged_cells() {
let table = map(&[&[(2, 1), (1, 1)], &[(1, 1), (1, 1), (1, 1)]]);
let rect = GridRect {
top: 0,
left: 1,
bottom: 2,
right: 2,
};
assert_eq!(
table.expand(rect),
GridRect {
top: 0,
left: 0,
bottom: 2,
right: 2
}
);
assert_eq!(table.find(1, 2), Some(4));
}
}