cleanup
authorBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 02:10:12 +0000 (19:10 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 02:10:12 +0000 (19:10 -0700)
rust/pspp/src/output/pivot/mod.rs
rust/pspp/src/output/pivot/output.rs

index 096691764c264970e2dff92d37b94d6f2e27c446..122b30f85e1c6f82444731c4782519c4e0070c75 100644 (file)
@@ -1013,6 +1013,15 @@ pub enum HeadingRegion {
     Columns,
 }
 
+impl From<Axis2> for HeadingRegion {
+    fn from(axis: Axis2) -> Self {
+        match axis {
+            Axis2::X => HeadingRegion::Columns,
+            Axis2::Y => HeadingRegion::Rows,
+        }
+    }
+}
+
 #[derive(Clone, Debug)]
 pub struct AreaStyle {
     pub cell_style: CellStyle,
index 3499cf547a3f2e84f1f96391eebef4edefb9ca92..86a81741af59f70fd95b42fe0b51f99a5716f4f4 100644 (file)
@@ -157,42 +157,25 @@ impl PivotTable {
     }
 
     pub fn output_body(&self, layer_indexes: &[usize], printing: bool) -> Table {
-        let column_enumeration = self.enumerate_axis(Axis3::X, layer_indexes, self.look.omit_empty);
-        let column_headings = Headings::new(self, Axis2::X, &column_enumeration);
+        let headings =
+            EnumMap::from_fn(|axis| Headings::new(self, axis, layer_indexes, self.look.omit_empty));
 
-        let row_enumeration = self.enumerate_axis(Axis3::Y, layer_indexes, self.look.omit_empty);
-        let row_headings = Headings::new(self, Axis2::Y, &row_enumeration);
-
-        let data = Coord2::new(column_enumeration.len(), row_enumeration.len());
-        let stub = Coord2::new(row_headings.height(), column_headings.height());
-        let n = EnumMap::from_fn(|axis| data[axis] + stub[axis]).into();
+        let data = Coord2::from_fn(|axis| headings[axis].width());
+        let stub = Coord2::from_fn(|axis| headings[!axis].height());
         let mut body = Table::new(
-            n,
+            Coord2::from_fn(|axis| data[axis] + stub[axis]),
             stub,
             self.look.areas.clone(),
             self.borders(printing),
             self.as_value_options(),
         );
 
-        column_headings.render(
-            &mut body,
-            row_headings.height(),
-            HeadingRegion::Columns,
-            self.rotate_outer_row_labels,
-            false,
-        );
-        row_headings.render(
-            &mut body,
-            column_headings.height(),
-            HeadingRegion::Rows,
-            false,
-            self.rotate_inner_column_labels,
-        );
+        for h in [Axis2::X, Axis2::Y] {
+            headings[h].render(&mut body, headings[!h].height(), h.into(), false, false);
+        }
 
-        for (y, row_indexes) in row_enumeration.iter().enumerate() {
-            let y = y + stub[Axis2::Y];
-            for (x, column_indexes) in column_enumeration.iter().enumerate() {
-                let x = x + stub[Axis2::X];
+        for (row_indexes, y) in headings[Axis2::Y].values.iter().zip(stub[Axis2::Y]..) {
+            for (column_indexes, x) in headings[Axis2::X].values.iter().zip(stub[Axis2::X]..) {
                 let presentation_indexes = enum_map! {
                     Axis3::X => &column_indexes,
                     Axis3::Y => &row_indexes,
@@ -499,11 +482,12 @@ impl<'a> Heading<'a> {
 struct Headings<'a> {
     headings: Vec<Heading<'a>>,
     h: Axis2,
-    column_enumeration: &'a AxisEnumeration,
+    values: AxisEnumeration,
 }
 
 impl<'a> Headings<'a> {
-    fn new(pt: &'a PivotTable, h: Axis2, column_enumeration: &'a AxisEnumeration) -> Self {
+    fn new(pt: &'a PivotTable, h: Axis2, layer_indexes: &[usize], omit_empty: bool) -> Self {
+        let column_enumeration = pt.enumerate_axis(h.into(), layer_indexes, omit_empty);
         Self {
             headings: pt.axes[h.into()]
                 .dimensions
@@ -512,11 +496,11 @@ impl<'a> Headings<'a> {
                 .enumerate()
                 .rev()
                 .filter_map(|(axis_index, dim_index)| {
-                    Heading::new(&pt.dimensions[dim_index], axis_index, column_enumeration)
+                    Heading::new(&pt.dimensions[dim_index], axis_index, &column_enumeration)
                 })
                 .collect(),
             h,
-            column_enumeration,
+            values: column_enumeration,
         }
     }
 
@@ -525,7 +509,7 @@ impl<'a> Headings<'a> {
     }
 
     fn width(&self) -> usize {
-        self.headings.first().map_or(0, |h| h.width())
+        self.values.len()
     }
 
     fn render(