Skip to content

Commit

Permalink
FIX: Reduce genericity of inner format_array function
Browse files Browse the repository at this point in the history
This is a slight de-bloating of this function, by converting to a dyn
dimensioned array before entering the main code. This saves a lot of code size,
even though there is more than can be done.

This also allows setting the top level parameters in only one place
(starting at depth 0 etc).
  • Loading branch information
bluss committed Sep 18, 2019
1 parent 1e7878f commit 5c97049
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::{ArrayBase, Axis, Data, Dimension, NdProducer};
use crate::aliases::Ix1;
use super::{ArrayBase, ArrayView, Axis, Data, Dimension, NdProducer};
use crate::aliases::{Ix1, IxDyn};
use std::fmt;

/// Default threshold, below this element count, we don't ellipsize
Expand Down Expand Up @@ -84,9 +84,8 @@ fn format_with_overflow(
limit: usize,
separator: &str,
ellipsis: &str,
fmt_elem: &mut dyn FnMut(&mut fmt::Formatter, usize) -> fmt::Result
) -> fmt::Result
{
fmt_elem: &mut dyn FnMut(&mut fmt::Formatter, usize) -> fmt::Result,
) -> fmt::Result {
if length == 0 {
// no-op
} else if length <= limit {
Expand All @@ -113,7 +112,30 @@ fn format_with_overflow(
}

fn format_array<A, S, D, F>(
view: &ArrayBase<S, D>,
array: &ArrayBase<S, D>,
f: &mut fmt::Formatter<'_>,
format: F,
fmt_opt: &FormatOptions,
) -> fmt::Result
where
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
D: Dimension,
S: Data<Elem = A>,
{
// Cast into a dynamically dimensioned view
// This is required to be able to use `index_axis` for the recursive case
format_array_inner(
array.view().into_dyn(),
f,
format,
fmt_opt,
0,
array.ndim(),
)
}

fn format_array_inner<A, F>(
view: ArrayView<A, IxDyn>,
f: &mut fmt::Formatter<'_>,
mut format: F,
fmt_opt: &FormatOptions,
Expand All @@ -122,18 +144,16 @@ fn format_array<A, S, D, F>(
) -> fmt::Result
where
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
D: Dimension,
S: Data<Elem = A>,
{
// 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) {
if view.is_empty() {
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)?,
&[] => format(&view[[]], f)?,
// We handle 1-D arrays as a special case
&[len] => {
let view = view.view().into_dimensionality::<Ix1>().unwrap();
Expand All @@ -150,19 +170,15 @@ where
}
// 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 blank_lines = "\n".repeat(shape.len() - 2);
let indent = " ".repeat(depth + 1);
let separator = format!(",\n{}{}", blank_lines, indent);

f.write_str("[")?;
let limit = fmt_opt.collapse_limit(full_ndim - depth - 1);
format_with_overflow(f, shape[0], limit, &separator, ELLIPSIS, &mut |f, index| {
format_array(
&view.index_axis(Axis(0), index),
format_array_inner(
view.index_axis(Axis(0), index),
f,
format.clone(),
fmt_opt,
Expand All @@ -187,7 +203,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
format_array(self, f, <_>::fmt, &fmt_opt)
}
}

Expand All @@ -201,7 +217,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())?;
format_array(self, f, <_>::fmt, &fmt_opt)?;

// Add extra information for Debug
write!(
Expand Down Expand Up @@ -229,7 +245,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
format_array(self, f, <_>::fmt, &fmt_opt)
}
}

Expand All @@ -243,7 +259,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
format_array(self, f, <_>::fmt, &fmt_opt)
}
}
/// Format the array using `LowerHex` and apply the formatting parameters used
Expand All @@ -256,7 +272,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
format_array(self, f, <_>::fmt, &fmt_opt)
}
}

Expand All @@ -270,7 +286,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
format_array(self, f, <_>::fmt, &fmt_opt)
}
}

Expand Down

0 comments on commit 5c97049

Please sign in to comment.