From 0dce3d88e44597db165d42ebc8de989be548bfac Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 10 Apr 2025 08:28:05 -0700 Subject: [PATCH] Drop label_depth, extra_depth, label_position. --- rust/pspp/src/output/pivot/mod.rs | 157 ++------------------------- rust/pspp/src/output/pivot/output.rs | 2 +- rust/pspp/src/output/pivot/test.rs | 9 +- 3 files changed, 15 insertions(+), 153 deletions(-) diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index 122b30f85e..e0cb9be00a 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -309,10 +309,6 @@ impl PivotTable { fn axis_extent(&self, axis: Axis3) -> usize { self.axis_dimensions(axis).map(|d| d.len()).product() } - - fn axis_label_depth(&self, axis: Axis3) -> usize { - self.axis_dimensions(axis).map(|d| d.label_depth()).sum() - } } /// Dimensions. @@ -362,22 +358,12 @@ impl Dimension { pub fn len(&self) -> usize { self.data_leaves.len() } - - pub fn label_depth(&self) -> usize { - if self.hide_all_labels { - 0 - } else { - self.root.label_depth - } - } } #[derive(Clone, Debug)] pub struct Group { parent: Option>, name: Box, - label_depth: usize, - extra_depth: usize, /// The child categories. /// @@ -385,8 +371,8 @@ pub struct Group { /// only one or even (pathologically) none. children: Vec, - /// Position for the group's own label, if any. - pub show_label: Option, + /// Whether to show the group's label. + pub show_label: bool, } impl Group { @@ -417,10 +403,9 @@ impl DimensionBuilder { self.hide_all_labels = true; self } - fn build(mut self, label_position: LabelPosition) -> Dimension { + fn build(mut self) -> Dimension { let mut leaves = Vec::with_capacity(self.len); - self.root.assign_label_depth(label_position, true); - let root = self.root.build(label_position, None, &mut leaves); + let root = self.root.build(None, &mut leaves); Dimension { root, presentation_order: (0..leaves.len()).collect(), @@ -435,9 +420,6 @@ pub struct GroupBuilder { name: Box, children: Vec, show_label: Option, - label_depth: usize, - extra_depth: usize, - label_position: Option, } impl GroupBuilder { @@ -446,9 +428,6 @@ impl GroupBuilder { name: Box::new(name), children: Vec::new(), show_label: None, - label_depth: 0, - extra_depth: 0, - label_position: None, } } pub fn push(&mut self, value: T) @@ -475,56 +454,17 @@ impl GroupBuilder { fn len(&self) -> usize { self.children.iter().map(|category| category.len()).sum() } - fn assign_label_depth(&mut self, label_position: LabelPosition, is_root: bool) { - for child in self.children.iter_mut() { - child.assign_label_depth(label_position); - } - let depth = self - .children - .iter() - .map(|c| c.label_depth()) - .max() - .unwrap_or_default(); - for child in self.children.iter_mut() { - let extra_depth = depth - child.label_depth(); - if extra_depth > 0 { - child.distribute_extra_depth(extra_depth); - } - child.set_label_depth(depth); - } - // By default, nested group labels are shown, but not dimension root labels. - let show_label = self.show_label.unwrap_or(!is_root); - self.label_position = show_label.then_some(label_position); - self.label_depth = depth + (self.label_position == Some(LabelPosition::Nested)) as usize; - } - fn distribute_extra_depth(&mut self, extra_depth: usize) { - if self.children.is_empty() { - self.extra_depth += extra_depth; - } else { - for child in self.children.iter_mut() { - child.distribute_extra_depth(extra_depth); - } - } - } - fn build( - self, - label_position: LabelPosition, - parent: Option>, - leaves: &mut Vec>, - ) -> Arc { + fn build(self, parent: Option>, leaves: &mut Vec>) -> Arc { Arc::new_cyclic(|weak| Group { name: self.name, - label_depth: self.label_depth, - extra_depth: self.extra_depth, children: self .children .into_iter() - .map(|c| c.build(label_position, weak.clone(), leaves)) + .map(|c| c.build(weak.clone(), leaves)) .collect(), show_label: { // By default, nested group labels are shown, but not dimension root labels. - let show_label = self.show_label.unwrap_or(parent.is_some()); - show_label.then_some(label_position) + self.show_label.unwrap_or(parent.is_some()) }, parent, }) @@ -537,7 +477,6 @@ pub enum CategoryBuilder { Leaf { name: Box, class: Option, - extra_depth: usize, }, } @@ -548,25 +487,13 @@ impl CategoryBuilder { CategoryBuilder::Leaf { .. } => 1, } } - fn build( - self, - label_position: LabelPosition, - parent: Weak, - leaves: &mut Vec>, - ) -> Category { + fn build(self, parent: Weak, leaves: &mut Vec>) -> Category { match self { - Self::Group(group) => { - Category::Group(group.build(label_position, Some(parent), leaves)) - } - Self::Leaf { - name, - class, - extra_depth, - } => { + Self::Group(group) => Category::Group(group.build(Some(parent), leaves)), + Self::Leaf { name, class } => { let leaf = Arc::new(Leaf { parent, name, - extra_depth, class, }); leaves.push(leaf.clone()); @@ -574,30 +501,6 @@ impl CategoryBuilder { } } } - fn assign_label_depth(&mut self, label_position: LabelPosition) { - match self { - CategoryBuilder::Group(group) => group.assign_label_depth(label_position, false), - CategoryBuilder::Leaf { .. } => (), - } - } - fn distribute_extra_depth(&mut self, extra: usize) { - match self { - CategoryBuilder::Group(group) => group.distribute_extra_depth(extra), - CategoryBuilder::Leaf { extra_depth, .. } => *extra_depth += extra, - } - } - fn label_depth(&self) -> usize { - match self { - CategoryBuilder::Group(group) => group.label_depth, - CategoryBuilder::Leaf { .. } => 1, - } - } - fn set_label_depth(&mut self, depth: usize) { - match self { - CategoryBuilder::Group(group) => group.label_depth = depth, - CategoryBuilder::Leaf { .. } => (), - } - } } impl From for CategoryBuilder { @@ -611,7 +514,6 @@ impl From for CategoryBuilder { Self::Leaf { name: Box::new(name), class: None, - extra_depth: 0, } } } @@ -621,7 +523,6 @@ impl From<(Value, Class)> for CategoryBuilder { Self::Leaf { name: Box::new(name), class: Some(class), - extra_depth: 0, } } } @@ -665,7 +566,7 @@ impl PivotTableBuilder { } else { LabelPosition::Nested }; - let d = d.build(label_position); + let d = d.build(); axes[axis].dimensions.push(dimensions.len()); dimensions.push(d); } @@ -681,7 +582,6 @@ impl PivotTableBuilder { pub struct Leaf { parent: Weak, name: Box, - extra_depth: usize, /// Default format for values in this category. class: Option, @@ -692,7 +592,6 @@ impl Leaf { Self { parent: Weak::new(), name: Box::new(name), - extra_depth: 0, class: None, } } @@ -746,7 +645,7 @@ impl Category { fn show_label(&self) -> bool { match self { - Category::Group(group) => group.show_label.is_some(), + Category::Group(group) => group.show_label, Category::Leaf(_) => true, } } @@ -762,8 +661,6 @@ impl Category { trait CategoryTrait { fn name(&self) -> &Value; - fn label_depth(&self) -> usize; - fn extra_depth(&self) -> usize; fn parent(&self) -> Option>; } @@ -772,14 +669,6 @@ impl CategoryTrait for Group { &self.name } - fn label_depth(&self) -> usize { - self.label_depth - } - - fn extra_depth(&self) -> usize { - self.extra_depth - } - fn parent(&self) -> Option> { self.parent.as_ref().and_then(|parent| parent.upgrade()) } @@ -790,14 +679,6 @@ impl CategoryTrait for Leaf { &self.name } - fn label_depth(&self) -> usize { - 1 - } - - fn extra_depth(&self) -> usize { - self.extra_depth - } - fn parent(&self) -> Option> { Some(self.parent.upgrade().unwrap()) } @@ -811,20 +692,6 @@ impl CategoryTrait for Category { } } - fn label_depth(&self) -> usize { - match self { - Category::Group(group) => group.label_depth(), - Category::Leaf(leaf) => leaf.label_depth(), - } - } - - fn extra_depth(&self) -> usize { - match self { - Category::Group(group) => group.extra_depth(), - Category::Leaf(leaf) => leaf.extra_depth(), - } - } - fn parent(&self) -> Option> { match self { Category::Group(group) => group.parent(), diff --git a/rust/pspp/src/output/pivot/output.rs b/rust/pspp/src/output/pivot/output.rs index 86a81741af..a8f844fca4 100644 --- a/rust/pspp/src/output/pivot/output.rs +++ b/rust/pspp/src/output/pivot/output.rs @@ -354,7 +354,7 @@ impl<'a> Heading<'a> { let leaf = &*dimension.data_leaves[dimension.presentation_order[indexes[dim_index]]]; let mut groups = leaf .ancestors() - .filter(|group| group.show_label.is_some()) + .filter(|group| group.show_label) .collect::>(); groups.reverse(); height = height.max(1 + groups.len()); diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index 5858b4f8c0..0225f484dc 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -1,11 +1,6 @@ -use std::{fs::File, sync::Arc}; +use std::sync::Arc; -use crate::output::{ - driver::Driver, - pivot::{Area, Axis2, Color, Look, PivotTable}, - text::TextDriver, - Details, Item, -}; +use crate::output::pivot::{Area, Color, Look, PivotTable}; use super::{Axis3, DimensionBuilder, GroupBuilder, PivotTableBuilder, Value}; -- 2.30.2