Drop label_depth, extra_depth, label_position.
authorBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 15:28:05 +0000 (08:28 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Thu, 10 Apr 2025 15:28:05 +0000 (08:28 -0700)
rust/pspp/src/output/pivot/mod.rs
rust/pspp/src/output/pivot/output.rs
rust/pspp/src/output/pivot/test.rs

index 122b30f85e1c6f82444731c4782519c4e0070c75..e0cb9be00a806b00261568a99bb54dfd03d7202d 100644 (file)
@@ -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<Weak<Group>>,
     name: Box<Value>,
-    label_depth: usize,
-    extra_depth: usize,
 
     /// The child categories.
     ///
@@ -385,8 +371,8 @@ pub struct Group {
     /// only one or even (pathologically) none.
     children: Vec<Category>,
 
-    /// Position for the group's own label, if any.
-    pub show_label: Option<LabelPosition>,
+    /// 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<Value>,
     children: Vec<CategoryBuilder>,
     show_label: Option<bool>,
-    label_depth: usize,
-    extra_depth: usize,
-    label_position: Option<LabelPosition>,
 }
 
 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<T>(&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<Weak<Group>>,
-        leaves: &mut Vec<Arc<Leaf>>,
-    ) -> Arc<Group> {
+    fn build(self, parent: Option<Weak<Group>>, leaves: &mut Vec<Arc<Leaf>>) -> Arc<Group> {
         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<Value>,
         class: Option<Class>,
-        extra_depth: usize,
     },
 }
 
@@ -548,25 +487,13 @@ impl CategoryBuilder {
             CategoryBuilder::Leaf { .. } => 1,
         }
     }
-    fn build(
-        self,
-        label_position: LabelPosition,
-        parent: Weak<Group>,
-        leaves: &mut Vec<Arc<Leaf>>,
-    ) -> Category {
+    fn build(self, parent: Weak<Group>, leaves: &mut Vec<Arc<Leaf>>) -> 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<GroupBuilder> for CategoryBuilder {
@@ -611,7 +514,6 @@ impl From<Value> 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<Group>,
     name: Box<Value>,
-    extra_depth: usize,
 
     /// Default format for values in this category.
     class: Option<Class>,
@@ -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<Arc<Group>>;
 }
 
@@ -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<Arc<Group>> {
         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<Arc<Group>> {
         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<Arc<Group>> {
         match self {
             Category::Group(group) => group.parent(),
index 86a81741af59f70fd95b42fe0b51f99a5716f4f4..a8f844fca43f1e13e63288dc59835271495b9d5b 100644 (file)
@@ -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::<SmallVec<_>>();
             groups.reverse();
             height = height.max(1 + groups.len());
index 5858b4f8c06442341d94cd1acf9bf04efc14bf66..0225f484dc597ccecd9cdcf80f034b846d697665 100644 (file)
@@ -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};