pspp 0.4.0

Statistical analysis software
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
// PSPP - a program for statistical analysis.
// Copyright (C) 2025 Free Software Foundation, Inc.
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program.  If not, see <http://www.gnu.org/licenses/>.

//! Tables.
//!
//! A table is a rectangular grid of cells.  Cells can be joined to form larger
//! cells.  Rows and columns can be separated by rules of various types.  Rows
//! at the top and bottom of a table and columns at the left and right edges of
//! a table can be designated as headers, which means that if the table must be
//! broken across more than one page, those rows or columns are repeated on each
//! page.
//!
//! Some drivers use tables as an implementation detail of rendering pivot
//! tables.

use std::{ops::Range, sync::Arc};

use enum_map::{enum_map, EnumMap};
use ndarray::{Array, Array2};

use crate::output::pivot::{Coord2, DisplayValue, Footnote, HorzAlign, ValueInner};

use super::pivot::{
    Area, AreaStyle, Axis2, Border, BorderStyle, HeadingRegion, Rect2, Value, ValueOptions,
};

#[derive(Clone, Debug)]
pub struct CellRef<'a> {
    pub coord: Coord2,
    pub content: &'a Content,
}

impl CellRef<'_> {
    pub fn inner(&self) -> &CellInner {
        self.content.inner()
    }

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

    pub fn rect(&self) -> Rect2 {
        self.content.rect(self.coord)
    }

    pub fn next_x(&self) -> usize {
        self.content.next_x(self.coord.x())
    }

    pub fn is_top_left(&self) -> bool {
        self.content.is_top_left(self.coord)
    }

    pub fn span(&self, axis: Axis2) -> usize {
        self.content.span(axis)
    }
    pub fn col_span(&self) -> usize {
        self.span(Axis2::X)
    }
    pub fn row_span(&self) -> usize {
        self.span(Axis2::Y)
    }
}

#[derive(Clone, Debug)]
pub enum Content {
    Value(CellInner),
    Join(Arc<Cell>),
}

impl Content {
    pub fn inner(&self) -> &CellInner {
        match self {
            Content::Value(cell_inner) => cell_inner,
            Content::Join(cell) => &cell.inner,
        }
    }

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

    /// Returns the rectangle that this cell covers, only if the cell contains
    /// that information. (Joined cells always do, and other cells usually
    /// don't.)
    pub fn joined_rect(&self) -> Option<&Rect2> {
        match self {
            Content::Join(cell) => Some(&cell.region),
            _ => None,
        }
    }

    /// Returns the rectangle that this cell covers. If the cell doesn't contain
    /// that information, returns a rectangle containing `coord`.
    pub fn rect(&self, coord: Coord2) -> Rect2 {
        match self {
            Content::Join(cell) => cell.region.clone(),
            _ => Rect2::for_cell(coord),
        }
    }

    pub fn next_x(&self, x: usize) -> usize {
        self.joined_rect()
            .map_or(x + 1, |region| region[Axis2::X].end)
    }

    pub fn is_top_left(&self, coord: Coord2) -> bool {
        self.joined_rect().is_none_or(|r| coord == r.top_left())
    }

    pub fn span(&self, axis: Axis2) -> usize {
        self.joined_rect().map_or(1, |r| {
            let range = &r.0[axis];
            range.end - range.start
        })
    }

    pub fn col_span(&self) -> usize {
        self.span(Axis2::X)
    }
    pub fn row_span(&self) -> usize {
        self.span(Axis2::Y)
    }
}

impl Default for Content {
    fn default() -> Self {
        Self::Value(CellInner::default())
    }
}

#[derive(Clone, Debug)]
pub struct Cell {
    inner: CellInner,

    /// Occupied table region.
    region: Rect2,
}

impl Cell {
    fn new(inner: CellInner, region: Rect2) -> Self {
        Self { inner, region }
    }
}

#[derive(Clone, Debug, Default)]
pub struct CellInner {
    /// Rotate cell contents 90 degrees?
    pub rotate: bool,

    /// The area that the cell belongs to.
    pub area: Area,

    pub value: Box<Value>,
}

impl CellInner {
    pub fn new(area: Area, value: Box<Value>) -> Self {
        Self {
            rotate: false,
            area,
            value,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.value.inner.is_empty()
    }
}

/// A table.
#[derive(derive_more::Debug)]
pub struct Table {
    /// Number of rows and columns.
    pub n: Coord2,

    /// Table header rows and columns.
    pub h: Coord2,

    pub contents: Array2<Content>,

    /// Styles for areas of the table.
    #[debug(skip)]
    pub areas: EnumMap<Area, AreaStyle>,

    /// Styles for borders in the table.
    #[debug(skip)]
    pub borders: EnumMap<Border, BorderStyle>,

    /// Horizontal ([Axis2::Y]) and vertical ([Axis2::X]) rules.
    pub rules: EnumMap<Axis2, Array2<Border>>,

    /// How to present values.
    #[debug(skip)]
    pub value_options: ValueOptions,
}

impl Table {
    pub fn new(
        n: Coord2,
        headers: Coord2,
        areas: EnumMap<Area, AreaStyle>,
        borders: EnumMap<Border, BorderStyle>,
        value_options: ValueOptions,
    ) -> Self {
        Self {
            n,
            h: headers,
            contents: Array::default((n.x(), n.y())),
            areas,
            borders,
            rules: enum_map! {
                Axis2::X => Array::from_elem((n.x() + 1, n.y()), Border::Title),
                Axis2::Y => Array::from_elem((n.x(), n.y() + 1), Border::Title),
            },
            value_options,
        }
    }

    pub fn get(&self, coord: Coord2) -> CellRef<'_> {
        CellRef {
            coord,
            content: &self.contents[[coord.x(), coord.y()]],
        }
    }

    pub fn get_rule(&self, axis: Axis2, pos: Coord2) -> BorderStyle {
        self.borders[self.rules[axis][[pos.x(), pos.y()]]]
    }

    pub fn put(&mut self, region: Rect2, inner: CellInner) {
        use Axis2::*;
        if region[X].len() == 1 && region[Y].len() == 1 {
            self.contents[[region[X].start, region[Y].start]] = Content::Value(inner);
        } else {
            let cell = Arc::new(Cell::new(inner, region.clone()));
            for y in region[Y].clone() {
                for x in region[X].clone() {
                    self.contents[[x, y]] = Content::Join(cell.clone())
                }
            }
        }
    }

    pub fn h_line(&mut self, border: Border, x: Range<usize>, y: usize) {
        for x in x {
            self.rules[Axis2::Y][[x, y]] = border;
        }
    }

    pub fn v_line(&mut self, border: Border, x: usize, y: Range<usize>) {
        for y in y {
            self.rules[Axis2::X][[x, y]] = border;
        }
    }

    pub fn draw_line(
        &mut self,
        border: Border,
        (a, a_value): (Axis2, usize),
        b_range: Range<usize>,
    ) {
        match a {
            Axis2::X => self.h_line(border, b_range, a_value),
            Axis2::Y => self.v_line(border, a_value, b_range),
        }
    }

    pub fn iter_x(&self, y: usize) -> XIter<'_> {
        XIter {
            table: self,
            x: None,
            y,
        }
    }

    /// The heading region that `pos` is part of, if any.
    pub fn heading_region(&self, pos: Coord2) -> Option<HeadingRegion> {
        if pos.x() < self.h.x() {
            Some(HeadingRegion::Rows)
        } else if pos.y() < self.h.y() {
            Some(HeadingRegion::Columns)
        } else {
            None
        }
    }

    /// Iterates across all of the cells in the table, visiting each of them
    /// once in top-down, left-to-right order. Spanned cells are visited once,
    /// at the point in the iteration where their top-left cell would appear if
    /// they were not spanned.
    pub fn cells(&self) -> Cells<'_> {
        Cells::new(self)
    }

    pub fn is_empty(&self) -> bool {
        self.n[Axis2::X] == 0 || self.n[Axis2::Y] == 0
    }
}

pub struct XIter<'a> {
    table: &'a Table,
    x: Option<usize>,
    y: usize,
}

impl Iterator for XIter<'_> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        let next_x = self
            .x
            .map_or(0, |x| self.table.get(Coord2::new(x, self.y)).next_x());
        if next_x >= self.table.n.x() {
            None
        } else {
            self.x = Some(next_x);
            Some(next_x)
        }
    }
}

/// Iterator for all of the cells in a table (see [Table::cells]).
pub struct Cells<'a> {
    table: &'a Table,
    next: Option<CellRef<'a>>,
}

impl<'a> Cells<'a> {
    fn new(table: &'a Table) -> Self {
        Self {
            table,
            next: if table.is_empty() {
                None
            } else {
                Some(table.get(Coord2::new(0, 0)))
            },
        }
    }
}

impl<'a> Iterator for Cells<'a> {
    type Item = CellRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        use Axis2::*;
        let this = self.next.as_ref()?.clone();

        let mut next = this.clone();
        self.next = loop {
            let next_x = next.next_x();
            let coord = if next_x < self.table.n[X] {
                Coord2::new(next_x, next.coord.y())
            } else if next.coord.y() + 1 < self.table.n[Y] {
                Coord2::new(0, next.coord.y() + 1)
            } else {
                break None;
            };
            next = self.table.get(coord);
            if next.is_top_left() {
                break Some(next);
            }
        };
        Some(this)
    }
}

pub struct DrawCell<'a> {
    pub rotate: bool,
    pub inner: &'a ValueInner,
    pub style: &'a AreaStyle,
    pub subscripts: &'a [String],
    pub footnotes: &'a [Arc<Footnote>],
    pub value_options: &'a ValueOptions,
}

impl<'a> DrawCell<'a> {
    pub fn new(inner: &'a CellInner, table: &'a Table) -> Self {
        let default_area_style = &table.areas[inner.area];
        let (style, subscripts, footnotes) = if let Some(styling) = &inner.value.styling {
            (
                styling.style.as_ref().unwrap_or(default_area_style),
                styling.subscripts.as_slice(),
                styling.footnotes.as_slice(),
            )
        } else {
            (default_area_style, [].as_slice(), [].as_slice())
        };
        Self {
            rotate: inner.rotate,
            inner: &inner.value.inner,
            style,
            subscripts,
            footnotes,
            value_options: &table.value_options,
        }
    }

    pub fn display(&self) -> DisplayValue<'a> {
        self.inner
            .display(self.value_options)
            .with_font_style(&self.style.font_style)
            .with_subscripts(self.subscripts)
            .with_footnotes(self.footnotes)
    }

    pub fn horz_align(&self, display: &DisplayValue) -> HorzAlign {
        self.style
            .cell_style
            .horz_align
            .unwrap_or_else(|| HorzAlign::for_mixed(display.var_type()))
    }
}