From 2a8a3b0918781476d882bb7077ca12ec4de0ca52 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 20 Apr 2019 15:17:32 +0100 Subject: [PATCH 01/11] Simplify element access --- src/arrayformat.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 35a8a2a20..0bd274f0f 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -31,13 +31,13 @@ fn get_highest_axis_to_skip(overflow_axes: &Vec, index: &[Ix], limit: &usize) -> Option { overflow_axes.iter() - .filter(|axis| { - if **axis == shape.len() - 1 { + .filter(|&axis| { + if *axis == shape.len() - 1 { return false }; - let sa_idx_max = shape.iter().skip(**axis).next().unwrap(); - let sa_idx_val = index.iter().skip(**axis).next().unwrap(); - sa_idx_val >= limit && sa_idx_val < &(sa_idx_max - limit) + let sa_idx_max = shape[*axis]; + let sa_idx_val = index[*axis]; + sa_idx_val >= *limit && sa_idx_val < sa_idx_max - *limit }) .min() .map(|v| *v) From 5fd9b2f77d7f447356fd1aa225296309ddd57449 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 20 Apr 2019 17:03:14 +0100 Subject: [PATCH 02/11] Almost there --- src/arrayformat.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 0bd274f0f..9b238aeeb 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -8,12 +8,14 @@ use std::fmt; use super::{ ArrayBase, + Axis, Data, Dimension, NdProducer, Ix }; use crate::dimension::IntoDimension; +use crate::aliases::ArrayViewD; const PRINT_ELEMENTS_LIMIT: Ix = 3; @@ -53,6 +55,73 @@ fn get_highest_changed_axis(index: &[Ix], prev_index: &[Ix]) -> Option { .next() } +fn format_1d_array( + view: &ArrayBase, + f: &mut fmt::Formatter, + mut format: F, + limit: Ix) -> fmt::Result + where + F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, + D: Dimension, + S: Data, +{ + unimplemented!() +} + +fn format_multidimensional_array( + view: &ArrayBase, + f: &mut fmt::Formatter, + mut format: F, + limit: Ix) -> fmt::Result + where + F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, + D: Dimension, + S: Data, +{ + unimplemented!() +} + +fn format_array_v2( + view: &ArrayBase, + f: &mut fmt::Formatter, + mut format: F, + limit: Ix) -> fmt::Result +where + F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, + D: Dimension, + S: Data, +{ + let view = view.view().into_dyn(); + match view.shape() { + [] => format(view.iter().next().unwrap(), f)?, + [_] => format_1d_array(&view, f, format, limit)?, + shape => { + let first_axis_length = shape[0]; + let indexes_to_be_printed: Vec> = if first_axis_length <= 2 * limit { + (0..first_axis_length).map(|x| Some(x)).collect() + } else { + let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); + v.push(None); + v.extend((first_axis_length-limit..first_axis_length).map(|x| Some(x))); + v + }; + write!(f, "[")?; + for index in indexes_to_be_printed { + match index { + Some(i) => format_array_v2( + &view.index_axis(Axis(0), i), f, format, limit + )?, + None => { + writeln!(f, "...,")? + } + } + } + write!(f, "]")?; + } + } + Ok(()) +} + fn format_array(view: &ArrayBase, f: &mut fmt::Formatter, mut format: F, From f60185ce59415de3e008f9ef4e36f238c127855e Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 20 Apr 2019 17:07:57 +0100 Subject: [PATCH 03/11] Clone for the win --- src/arrayformat.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 9b238aeeb..32bf90286 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -87,7 +87,7 @@ fn format_array_v2( mut format: F, limit: Ix) -> fmt::Result where - F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, + F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result + Clone, D: Dimension, S: Data, { @@ -109,7 +109,7 @@ where for index in indexes_to_be_printed { match index { Some(i) => format_array_v2( - &view.index_axis(Axis(0), i), f, format, limit + &view.index_axis(Axis(0), i), f, format.clone(), limit )?, None => { writeln!(f, "...,")? From 198d6899ac83347836143d921d10429bd3d3d4c7 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 20 Apr 2019 17:11:49 +0100 Subject: [PATCH 04/11] Cast to 1d array --- src/arrayformat.rs | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 32bf90286..a0a4b0c2c 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -15,7 +15,7 @@ use super::{ Ix }; use crate::dimension::IntoDimension; -use crate::aliases::ArrayViewD; +use crate::aliases::Ix1; const PRINT_ELEMENTS_LIMIT: Ix = 3; @@ -55,27 +55,13 @@ fn get_highest_changed_axis(index: &[Ix], prev_index: &[Ix]) -> Option { .next() } -fn format_1d_array( - view: &ArrayBase, - f: &mut fmt::Formatter, - mut format: F, - limit: Ix) -> fmt::Result - where - F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, - D: Dimension, - S: Data, -{ - unimplemented!() -} - -fn format_multidimensional_array( - view: &ArrayBase, +fn format_1d_array( + view: &ArrayBase, f: &mut fmt::Formatter, mut format: F, limit: Ix) -> fmt::Result where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, - D: Dimension, S: Data, { unimplemented!() @@ -94,7 +80,7 @@ where let view = view.view().into_dyn(); match view.shape() { [] => format(view.iter().next().unwrap(), f)?, - [_] => format_1d_array(&view, f, format, limit)?, + [_] => format_1d_array(&view.into_dimensionality::().unwrap(), f, format, limit)?, shape => { let first_axis_length = shape[0]; let indexes_to_be_printed: Vec> = if first_axis_length <= 2 * limit { From c1036fb273e017bce2d138b562d02020af531aeb Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sat, 20 Apr 2019 17:30:04 +0100 Subject: [PATCH 05/11] First and last elements have to be special cased. Done for multidimensional --- src/arrayformat.rs | 167 ++++++++------------------------------------- 1 file changed, 29 insertions(+), 138 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index a0a4b0c2c..d0dec54ed 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -14,47 +14,10 @@ use super::{ NdProducer, Ix }; -use crate::dimension::IntoDimension; use crate::aliases::Ix1; const PRINT_ELEMENTS_LIMIT: Ix = 3; -fn get_overflow_axes(shape: &[Ix], limit: usize) -> Vec { - shape.iter() - .enumerate() - .rev() - .filter(|(_, axis_size)| **axis_size > 2 * limit) - .map(|(axis, _)| axis) - .collect() -} - -fn get_highest_axis_to_skip(overflow_axes: &Vec, - shape: &[Ix], - index: &[Ix], - limit: &usize) -> Option { - overflow_axes.iter() - .filter(|&axis| { - if *axis == shape.len() - 1 { - return false - }; - let sa_idx_max = shape[*axis]; - let sa_idx_val = index[*axis]; - sa_idx_val >= *limit && sa_idx_val < sa_idx_max - *limit - }) - .min() - .map(|v| *v) -} - -fn get_highest_changed_axis(index: &[Ix], prev_index: &[Ix]) -> Option { - index.iter() - .take(index.len() - 1) - .zip(prev_index.iter()) - .enumerate() - .filter(|(_, (a, b))| a != b) - .map(|(i, _)| i) - .next() -} - fn format_1d_array( view: &ArrayBase, f: &mut fmt::Formatter, @@ -64,10 +27,27 @@ fn format_1d_array( F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, S: Data, { - unimplemented!() + let n = view.len(); + let indexes_to_be_printed: Vec> = if n <= 2 * limit { + (0..n).map(|x| Some(x)).collect() + } else { + let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); + v.push(None); + v.extend((n-limit..n).map(|x| Some(x))); + v + }; + write!(f, "[")?; + for index in indexes_to_be_printed { + match index { + Some(i) => format(&view[i], f)?, + None => write!(f, ", ..., ")?, + } + } + write!(f, "]")?; + Ok(()) } -fn format_array_v2( +fn format_array( view: &ArrayBase, f: &mut fmt::Formatter, mut format: F, @@ -91,113 +71,24 @@ where v.extend((first_axis_length-limit..first_axis_length).map(|x| Some(x))); v }; - write!(f, "[")?; + writeln!(f, "[")?; for index in indexes_to_be_printed { match index { - Some(i) => format_array_v2( - &view.index_axis(Axis(0), i), f, format.clone(), limit - )?, + Some(i) => { + write!(f, " ")?; + format_array( + &view.index_axis(Axis(0), i), f, format.clone(), limit + )?; + writeln!(f, ",")? + }, None => { - writeln!(f, "...,")? + writeln!(f, " ...,")? } } } - write!(f, "]")?; - } - } - Ok(()) -} - -fn format_array(view: &ArrayBase, - f: &mut fmt::Formatter, - mut format: F, - limit: Ix) -> fmt::Result - where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, - D: Dimension, - S: Data, -{ - if view.shape().is_empty() { - // Handle 0-dimensional array case first - return format(view.iter().next().unwrap(), f) - } - - let overflow_axes: Vec = get_overflow_axes(view.shape(), limit); - - let ndim = view.ndim(); - let nth_idx_max = view.shape()[ndim-1]; - - // None will be an empty iter. - let mut last_index = match view.dim().into_dimension().first_index() { - None => view.dim().into_dimension().clone(), - Some(ix) => ix, - }; - write!(f, "{}", "[".repeat(ndim))?; - // Shows if ellipses for horizontal split were printed. - let mut printed_ellipses_h = vec![false; ndim]; - // Shows if the row was printed for the first time after horizontal split. - let mut no_rows_after_skip_yet = false; - - // Simply use the indexed iterator, and take the index wraparounds - // as cues for when to add []'s and how many to add. - for (index, elt) in view.indexed_iter() { - let index = index.into_dimension(); - - let skip_row_for_axis = get_highest_axis_to_skip( - &overflow_axes, - view.shape(), - index.slice(), - &limit - ); - if skip_row_for_axis.is_some() { - no_rows_after_skip_yet = true; - } - - let max_changed_idx = get_highest_changed_axis(index.slice(), last_index.slice()); - if let Some(i) = max_changed_idx { - printed_ellipses_h.iter_mut().skip(i + 1).for_each(|e| { *e = false; }); - - if skip_row_for_axis.is_none() { - // New row. - // # of ['s needed - let n = ndim - i - 1; - if !no_rows_after_skip_yet { - write!(f, "{}", "]".repeat(n))?; - writeln!(f, ",")?; - } - no_rows_after_skip_yet = false; - write!(f, "{}", " ".repeat(ndim - n))?; - write!(f, "{}", "[".repeat(n))?; - } else if !printed_ellipses_h[skip_row_for_axis.unwrap()] { - let ax = skip_row_for_axis.unwrap(); - let n = ndim - i - 1; - write!(f, "{}", "]".repeat(n))?; - writeln!(f, ",")?; - write!(f, "{}", " ".repeat(ax + 1))?; - writeln!(f, "...,")?; - printed_ellipses_h[ax] = true; - } - last_index = index.clone(); - } - - if skip_row_for_axis.is_none() { - let nth_idx_op = index.slice().iter().last(); - if overflow_axes.contains(&(ndim - 1)) { - let nth_idx_val = nth_idx_op.unwrap(); - if nth_idx_val >= &limit && nth_idx_val < &(nth_idx_max - &limit) { - if nth_idx_val == &limit { - write!(f, ", ...")?; - } - continue; - } - } - - if max_changed_idx.is_none() && !index.slice().iter().all(|x| *x == 0) { - write!(f, ", ")?; - } - format(elt, f)?; + writeln!(f, "]")?; } } - write!(f, "{}", "]".repeat(ndim))?; Ok(()) } From 5336b1065db9b1f2434beab36ca23b0cb83a2a14 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 21 Apr 2019 09:56:13 +0100 Subject: [PATCH 06/11] Extract in a separate function --- src/arrayformat.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index d0dec54ed..1ebbc76a3 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -28,14 +28,7 @@ fn format_1d_array( S: Data, { let n = view.len(); - let indexes_to_be_printed: Vec> = if n <= 2 * limit { - (0..n).map(|x| Some(x)).collect() - } else { - let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); - v.push(None); - v.extend((n-limit..n).map(|x| Some(x))); - v - }; + let indexes_to_be_printed = indexes_to_be_printed(n, limit); write!(f, "[")?; for index in indexes_to_be_printed { match index { @@ -47,6 +40,17 @@ fn format_1d_array( Ok(()) } +fn indexes_to_be_printed(length: usize, limit: usize) -> Vec> { + if length <= 2 * limit { + (0..length).map(|x| Some(x)).collect() + } else { + let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); + v.push(None); + v.extend((length-limit..length).map(|x| Some(x))); + v + } +} + fn format_array( view: &ArrayBase, f: &mut fmt::Formatter, @@ -63,14 +67,7 @@ where [_] => format_1d_array(&view.into_dimensionality::().unwrap(), f, format, limit)?, shape => { let first_axis_length = shape[0]; - let indexes_to_be_printed: Vec> = if first_axis_length <= 2 * limit { - (0..first_axis_length).map(|x| Some(x)).collect() - } else { - let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); - v.push(None); - v.extend((first_axis_length-limit..first_axis_length).map(|x| Some(x))); - v - }; + let indexes_to_be_printed = indexes_to_be_printed(first_axis_length, limit); writeln!(f, "[")?; for index in indexes_to_be_printed { match index { From 4fa8a62d6295017e10549bbdc652e91841d1a27a Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 21 Apr 2019 15:56:34 +0100 Subject: [PATCH 07/11] Tests are passing --- src/arrayformat.rs | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 1ebbc76a3..b6018734d 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -29,11 +29,17 @@ fn format_1d_array( { let n = view.len(); let indexes_to_be_printed = indexes_to_be_printed(n, limit); + let last_index = indexes_to_be_printed.len(); write!(f, "[")?; - for index in indexes_to_be_printed { + for (j, index) in indexes_to_be_printed.into_iter().enumerate() { match index { - Some(i) => format(&view[i], f)?, - None => write!(f, ", ..., ")?, + Some(i) => { + format(&view[i], f)?; + if j != (last_index-1) { + write!(f, ", ")?; + } + }, + None => write!(f, "..., ")?, } } write!(f, "]")?; @@ -61,29 +67,35 @@ where D: Dimension, S: Data, { - let view = view.view().into_dyn(); + if view.shape().iter().any(|&x| x == 0) { + write!(f, "{}{}", "[".repeat(view.ndim()), "]".repeat(view.ndim()))?; + return Ok(()) + } match view.shape() { [] => format(view.iter().next().unwrap(), f)?, - [_] => format_1d_array(&view.into_dimensionality::().unwrap(), f, format, limit)?, + [_] => format_1d_array(&view.view().into_dimensionality::().unwrap(), f, format, limit)?, shape => { + let view = view.view().into_dyn(); let first_axis_length = shape[0]; let indexes_to_be_printed = indexes_to_be_printed(first_axis_length, limit); - writeln!(f, "[")?; - for index in indexes_to_be_printed { + let n_to_be_printed = indexes_to_be_printed.len(); + write!(f, "[")?; + for (j, index) in indexes_to_be_printed.into_iter().enumerate() { match index { Some(i) => { - write!(f, " ")?; format_array( &view.index_axis(Axis(0), i), f, format.clone(), limit )?; - writeln!(f, ",")? + if j != (n_to_be_printed -1) { + write!(f, ",\n ")? + } }, None => { - writeln!(f, " ...,")? + write!(f, "...,\n ")? } } } - writeln!(f, "]")?; + write!(f, "]")?; } } Ok(()) @@ -183,7 +195,8 @@ mod formatting_with_omit { let a: Array2 = arr2(&[[], []]); let actual_output = format!("{}", a); let expected_output = String::from("[[]]"); - assert_eq!(actual_output, expected_output); + print_output_diff(&expected_output, &actual_output); + assert_eq!(expected_output, actual_output); } #[test] @@ -191,7 +204,8 @@ mod formatting_with_omit { let a = Array3::::zeros((3, 0, 4)); let actual_output = format!("{}", a); let expected_output = String::from("[[[]]]"); - assert_eq!(actual_output, expected_output); + print_output_diff(&expected_output, &actual_output); + assert_eq!(expected_output, actual_output); } #[test] @@ -200,7 +214,8 @@ mod formatting_with_omit { let a = arr0(element); let actual_output = format!("{}", a); let expected_output = format!("{}", element); - assert_eq!(actual_output, expected_output); + print_output_diff(&expected_output, &actual_output); + assert_eq!(expected_output, actual_output); } #[test] From e659390e57827c1191d887ece1a21f542ed4db93 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 21 Apr 2019 16:10:01 +0100 Subject: [PATCH 08/11] Minor improvements --- src/arrayformat.rs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index b6018734d..a7e87f32f 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -28,14 +28,15 @@ fn format_1d_array( S: Data, { let n = view.len(); - let indexes_to_be_printed = indexes_to_be_printed(n, limit); - let last_index = indexes_to_be_printed.len(); + let to_be_printed = to_be_printed(n, limit); + let n_to_be_printed = to_be_printed.len(); + let is_last = |j| j == n_to_be_printed - 1; write!(f, "[")?; - for (j, index) in indexes_to_be_printed.into_iter().enumerate() { + for (j, index) in to_be_printed.into_iter().enumerate() { match index { Some(i) => { format(&view[i], f)?; - if j != (last_index-1) { + if !is_last(j) { write!(f, ", ")?; } }, @@ -46,7 +47,10 @@ fn format_1d_array( Ok(()) } -fn indexes_to_be_printed(length: usize, limit: usize) -> Vec> { +// Returns what indexes should be printed for a certain axis. +// If the axis is longer than 2 * limit, a `None` is inserted +// where indexes are being omitted. +fn to_be_printed(length: usize, limit: usize) -> Vec> { if length <= 2 * limit { (0..length).map(|x| Some(x)).collect() } else { @@ -67,32 +71,44 @@ where D: Dimension, S: Data, { + // If any of the axes has 0 length, we return the same empty array representation + // e.g. [[]] for 2-d arrays if view.shape().iter().any(|&x| x == 0) { write!(f, "{}{}", "[".repeat(view.ndim()), "]".repeat(view.ndim()))?; return Ok(()) } match view.shape() { + // If it's 0 dimensional, we just print out the scalar [] => format(view.iter().next().unwrap(), f)?, + // We delegate 1-dimensional arrays to a specialized function [_] => format_1d_array(&view.view().into_dimensionality::().unwrap(), f, format, limit)?, + // For n-dimensional arrays, we proceed recursively shape => { + // Cast into a dynamically dimensioned view + // This is required to be able to use `index_axis` let view = view.view().into_dyn(); - let first_axis_length = shape[0]; - let indexes_to_be_printed = indexes_to_be_printed(first_axis_length, limit); - let n_to_be_printed = indexes_to_be_printed.len(); + // We start by checking what indexes from the first axis should be printed + // We put a `None` in the middle if we are omitting elements + let to_be_printed = to_be_printed(shape[0], limit); + + let n_to_be_printed = to_be_printed.len(); + let is_last = |j| j == n_to_be_printed - 1; + write!(f, "[")?; - for (j, index) in indexes_to_be_printed.into_iter().enumerate() { + for (j, index) in to_be_printed.into_iter().enumerate() { match index { Some(i) => { + // Proceed recursively with the (n-1)-dimensional slice format_array( &view.index_axis(Axis(0), i), f, format.clone(), limit )?; - if j != (n_to_be_printed -1) { + // We need to add a separator after each slice, + // apart from the last one + if !is_last(j) { write!(f, ",\n ")? } }, - None => { - write!(f, "...,\n ")? - } + None => write!(f, "...,\n ")? } } write!(f, "]")?; From 0abff2ad521b115d519ae32f3f9abe957e3717f0 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 21 Apr 2019 16:12:42 +0100 Subject: [PATCH 09/11] Formatting --- src/arrayformat.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index a7e87f32f..c2af80ef7 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -27,10 +27,11 @@ fn format_1d_array( F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result, S: Data, { - let n = view.len(); - let to_be_printed = to_be_printed(n, limit); + let to_be_printed = to_be_printed(view.len(), limit); + let n_to_be_printed = to_be_printed.len(); let is_last = |j| j == n_to_be_printed - 1; + write!(f, "[")?; for (j, index) in to_be_printed.into_iter().enumerate() { match index { From e049abd1ddef2ae2e2ab9d50445ff04528d32270 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Tue, 23 Apr 2019 20:16:42 +0100 Subject: [PATCH 10/11] Remove closure --- src/arrayformat.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index c2af80ef7..7613e863b 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -30,14 +30,13 @@ fn format_1d_array( let to_be_printed = to_be_printed(view.len(), limit); let n_to_be_printed = to_be_printed.len(); - let is_last = |j| j == n_to_be_printed - 1; write!(f, "[")?; for (j, index) in to_be_printed.into_iter().enumerate() { match index { Some(i) => { format(&view[i], f)?; - if !is_last(j) { + if j != n_to_be_printed - 1 { write!(f, ", ")?; } }, @@ -93,7 +92,6 @@ where let to_be_printed = to_be_printed(shape[0], limit); let n_to_be_printed = to_be_printed.len(); - let is_last = |j| j == n_to_be_printed - 1; write!(f, "[")?; for (j, index) in to_be_printed.into_iter().enumerate() { @@ -105,7 +103,7 @@ where )?; // We need to add a separator after each slice, // apart from the last one - if !is_last(j) { + if j != n_to_be_printed - 1 { write!(f, ",\n ")? } }, From 83c9f08cbf332bff28f0f87b9642950fbacc8370 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Tue, 23 Apr 2019 20:22:11 +0100 Subject: [PATCH 11/11] Use custom enum --- src/arrayformat.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/arrayformat.rs b/src/arrayformat.rs index 7613e863b..894215d8c 100644 --- a/src/arrayformat.rs +++ b/src/arrayformat.rs @@ -34,29 +34,34 @@ fn format_1d_array( write!(f, "[")?; for (j, index) in to_be_printed.into_iter().enumerate() { match index { - Some(i) => { + PrintableCell::ElementIndex(i) => { format(&view[i], f)?; if j != n_to_be_printed - 1 { write!(f, ", ")?; } }, - None => write!(f, "..., ")?, + PrintableCell::Ellipses => write!(f, "..., ")?, } } write!(f, "]")?; Ok(()) } +enum PrintableCell { + ElementIndex(usize), + Ellipses, +} + // Returns what indexes should be printed for a certain axis. -// If the axis is longer than 2 * limit, a `None` is inserted +// If the axis is longer than 2 * limit, a `Ellipses` is inserted // where indexes are being omitted. -fn to_be_printed(length: usize, limit: usize) -> Vec> { +fn to_be_printed(length: usize, limit: usize) -> Vec { if length <= 2 * limit { - (0..length).map(|x| Some(x)).collect() + (0..length).map(|x| PrintableCell::ElementIndex(x)).collect() } else { - let mut v: Vec> = (0..limit).map(|x| Some(x)).collect(); - v.push(None); - v.extend((length-limit..length).map(|x| Some(x))); + let mut v: Vec = (0..limit).map(|x| PrintableCell::ElementIndex(x)).collect(); + v.push(PrintableCell::Ellipses); + v.extend((length-limit..length).map(|x| PrintableCell::ElementIndex(x))); v } } @@ -96,7 +101,7 @@ where write!(f, "[")?; for (j, index) in to_be_printed.into_iter().enumerate() { match index { - Some(i) => { + PrintableCell::ElementIndex(i) => { // Proceed recursively with the (n-1)-dimensional slice format_array( &view.index_axis(Axis(0), i), f, format.clone(), limit @@ -107,7 +112,7 @@ where write!(f, ",\n ")? } }, - None => write!(f, "...,\n ")? + PrintableCell::Ellipses => write!(f, "...,\n ")? } } write!(f, "]")?;