From: Ben Pfaff Date: Sat, 12 Apr 2025 15:25:36 +0000 (-0700) Subject: final removal of builders X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56bfc726be1172591b4a7393f515d85b467ffd3a;p=pspp final removal of builders --- diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index 212c950ddd..0eefb99b16 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -25,8 +25,6 @@ //! - Data. A table's data consists of zero or more cells. Each cell maps from //! a category for each dimension to a value, which is commonly a number but //! could also be a variable name or an arbitrary text string. -//! -//! Use [PivotTable::builder] to create a pivot table. use std::{ collections::HashMap, @@ -476,43 +474,6 @@ impl Footnotes { } } -pub struct PivotTableBuilder { - look: Arc, - title: Box, - axes: EnumMap, - dimensions: Vec, - cells: HashMap, - footnotes: Footnotes, -} - -impl PivotTableBuilder { - pub fn new(title: impl Into, dimensions_and_axes: Vec<(Axis3, Dimension)>) -> Self { - let mut dimensions = Vec::new(); - let mut axes = EnumMap::::default(); - for (axis, dimension) in dimensions_and_axes { - axes[axis].dimensions.push(dimensions.len()); - dimensions.push(dimension); - } - Self { - look: Settings::global().look.clone(), - title: Box::new(title.into()), - axes, - dimensions, - cells: HashMap::new(), - footnotes: Footnotes::new(), - } - } - pub fn build(self) -> PivotTable { - let mut table = PivotTable::new(self.title, self.look.clone()); - table.dimensions = self.dimensions; - table.axes = self.axes; - table.cells = self.cells; - table.current_layer = repeat_n(0, table.axes[Axis3::Z].dimensions.len()).collect(); - table.footnotes = self.footnotes; - table - } -} - #[derive(Clone, Debug)] pub struct Leaf { name: Box, @@ -1447,12 +1408,29 @@ where } impl PivotTable { + /* fn new(title: Box, look: Arc) -> Self { Self { title: Some(title), look, ..Self::default() } + }*/ + pub fn new(title: impl Into, dimensions_and_axes: Vec<(Axis3, Dimension)>) -> Self { + let mut dimensions = Vec::new(); + let mut axes = EnumMap::::default(); + for (axis, dimension) in dimensions_and_axes { + axes[axis].dimensions.push(dimensions.len()); + dimensions.push(dimension); + } + Self { + look: Settings::global().look.clone(), + title: Some(Box::new(title.into())), + current_layer: repeat_n(0, axes[Axis3::Z].dimensions.len()).collect(), + axes, + dimensions, + ..Self::default() + } } fn cell_index(&self, data_indexes: &[usize]) -> usize { cell_index(data_indexes, self.dimensions.iter().map(|d| d.len())) diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index e3a50ff2e0..3c0dfa5fed 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -7,7 +7,7 @@ use crate::output::pivot::{ HeadingRegion, LabelPosition, Look, PivotTable, RowColBorder, Stroke, }; -use super::{Axis3, PivotTableBuilder, Value}; +use super::{Axis3, Value}; #[test] fn color() { @@ -30,8 +30,7 @@ fn d1(title: &str, axis: Axis3) -> PivotTable { .with("a2") .with("a3"), ); - let mut pt = PivotTableBuilder::new(Value::new_text(title), vec![(axis, dimension)]) - .build() + let mut pt = PivotTable::new(Value::new_text(title), vec![(axis, dimension)]) .with_look(Arc::new(test_look())); for i in 0..3 { pt.insert(&[i], Value::new_integer(Some(i as f64))); @@ -97,8 +96,7 @@ fn d2(title: &str, axes: [Axis3; 2], dimension_labels: Option) -> .with("b3"), ); - let mut pt = - PivotTableBuilder::new(Value::new_text(title), vec![(axes[0], d1), (axes[1], d2)]).build(); + let mut pt = PivotTable::new(Value::new_text(title), vec![(axes[0], d1), (axes[1], d2)]); let mut i = 0; for b in 0..3 { for a in 0..3 { @@ -575,11 +573,10 @@ fn footnotes() { ), ); let look = test_look().with_row_label_position(LabelPosition::Nested); - let mut pt = PivotTableBuilder::new( + let mut pt = PivotTable::new( Value::new_text("Pivot Table with Alphabetic Subscript Footnotes").with_footnote(&f0), vec![a, d], - ) - .build(); + ); pt.insert(&[0, 0], Value::new_number(Some(0.0))); pt.insert(&[1, 0], Value::new_number(Some(1.0)).with_footnote(&f0)); pt.insert(&[0, 1], Value::new_number(Some(2.0)).with_footnote(&f1)); @@ -619,9 +616,7 @@ b. Second footnote #[test] fn no_dimension() { - let pivot_table = PivotTableBuilder::new("No Dimensions", vec![]) - .build() - .with_look(Arc::new(test_look())); + let pivot_table = PivotTable::new("No Dimensions", vec![]).with_look(Arc::new(test_look())); assert_rendering( &pivot_table, "No Dimensions @@ -636,15 +631,12 @@ fn empty_dimensions() { let look = Arc::new(test_look().with_omit_empty(false)); let d1 = (Axis3::X, Dimension::new(Group::new("a"))); - let pivot_table = PivotTableBuilder::new("One Empty Dimension", vec![d1]) - .build() - .with_look(look.clone()); + let pivot_table = PivotTable::new("One Empty Dimension", vec![d1]).with_look(look.clone()); assert_rendering(&pivot_table, "One Empty Dimension\n"); let d1 = (Axis3::X, Dimension::new(Group::new("a"))); let d2 = (Axis3::X, Dimension::new(Group::new("b").with_label_shown())); - let pivot_table = PivotTableBuilder::new(Value::new_text("Two Empty Dimensions"), vec![d1, d2]) - .build() + let pivot_table = PivotTable::new(Value::new_text("Two Empty Dimensions"), vec![d1, d2]) .with_look(look.clone()); assert_rendering(&pivot_table, "Two Empty Dimensions\n"); @@ -654,9 +646,8 @@ fn empty_dimensions() { Axis3::X, Dimension::new(Group::new("c").with("c1").with("c2")), ); - let pivot_table = PivotTableBuilder::new("Three Dimensions, Two Empty", vec![d1, d2, d3]) - .build() - .with_look(look.clone()); + let pivot_table = + PivotTable::new("Three Dimensions, Two Empty", vec![d1, d2, d3]).with_look(look.clone()); assert_rendering(&pivot_table, "Three Dimensions, Two Empty\n"); } @@ -672,7 +663,7 @@ fn empty_groups() { Dimension::new(Group::new("b").with(Group::new("b1")).with("b2").with("b3")), ); - let mut pt = PivotTableBuilder::new("Empty Groups", vec![d1, d2]).build(); + let mut pt = PivotTable::new("Empty Groups", vec![d1, d2]); let mut i = 0; for b in 0..2 { for a in 0..2 { @@ -736,8 +727,7 @@ fn d4( .with("d3"), ), ); - let mut pivot_table = PivotTableBuilder::new(title, vec![a, b, c, d]) - .build() + let mut pivot_table = PivotTable::new(title, vec![a, b, c, d]) .with_look(Arc::new(test_look().with_borders(borders))); let mut i = 0; for d in 0..3 { @@ -1083,7 +1073,7 @@ fn small_numbers() { .with_label_shown(), ), ); - let mut pt = PivotTableBuilder::new("small numbers", vec![exponent, sign, rc]).build(); + let mut pt = PivotTable::new("small numbers", vec![exponent, sign, rc]); pt.insert_number(&[0, 0, 0], Some(1.0), Class::Other); pt.insert_number(&[1, 0, 0], Some(0.1), Class::Other); pt.insert_number(&[2, 0, 0], Some(0.01), Class::Other);