Skip to content

Commit 5c97049

Browse files
committed
FIX: Reduce genericity of inner format_array function
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).
1 parent 1e7878f commit 5c97049

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

src/arrayformat.rs

+38-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
use super::{ArrayBase, Axis, Data, Dimension, NdProducer};
9-
use crate::aliases::Ix1;
8+
use super::{ArrayBase, ArrayView, Axis, Data, Dimension, NdProducer};
9+
use crate::aliases::{Ix1, IxDyn};
1010
use std::fmt;
1111

1212
/// Default threshold, below this element count, we don't ellipsize
@@ -84,9 +84,8 @@ fn format_with_overflow(
8484
limit: usize,
8585
separator: &str,
8686
ellipsis: &str,
87-
fmt_elem: &mut dyn FnMut(&mut fmt::Formatter, usize) -> fmt::Result
88-
) -> fmt::Result
89-
{
87+
fmt_elem: &mut dyn FnMut(&mut fmt::Formatter, usize) -> fmt::Result,
88+
) -> fmt::Result {
9089
if length == 0 {
9190
// no-op
9291
} else if length <= limit {
@@ -113,7 +112,30 @@ fn format_with_overflow(
113112
}
114113

115114
fn format_array<A, S, D, F>(
116-
view: &ArrayBase<S, D>,
115+
array: &ArrayBase<S, D>,
116+
f: &mut fmt::Formatter<'_>,
117+
format: F,
118+
fmt_opt: &FormatOptions,
119+
) -> fmt::Result
120+
where
121+
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
122+
D: Dimension,
123+
S: Data<Elem = A>,
124+
{
125+
// Cast into a dynamically dimensioned view
126+
// This is required to be able to use `index_axis` for the recursive case
127+
format_array_inner(
128+
array.view().into_dyn(),
129+
f,
130+
format,
131+
fmt_opt,
132+
0,
133+
array.ndim(),
134+
)
135+
}
136+
137+
fn format_array_inner<A, F>(
138+
view: ArrayView<A, IxDyn>,
117139
f: &mut fmt::Formatter<'_>,
118140
mut format: F,
119141
fmt_opt: &FormatOptions,
@@ -122,18 +144,16 @@ fn format_array<A, S, D, F>(
122144
) -> fmt::Result
123145
where
124146
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
125-
D: Dimension,
126-
S: Data<Elem = A>,
127147
{
128148
// If any of the axes has 0 length, we return the same empty array representation
129149
// e.g. [[]] for 2-d arrays
130-
if view.shape().iter().any(|&x| x == 0) {
150+
if view.is_empty() {
131151
write!(f, "{}{}", "[".repeat(view.ndim()), "]".repeat(view.ndim()))?;
132152
return Ok(());
133153
}
134154
match view.shape() {
135155
// If it's 0 dimensional, we just print out the scalar
136-
&[] => format(view.iter().next().unwrap(), f)?,
156+
&[] => format(&view[[]], f)?,
137157
// We handle 1-D arrays as a special case
138158
&[len] => {
139159
let view = view.view().into_dimensionality::<Ix1>().unwrap();
@@ -150,19 +170,15 @@ where
150170
}
151171
// For n-dimensional arrays, we proceed recursively
152172
shape => {
153-
// Cast into a dynamically dimensioned view
154-
// This is required to be able to use `index_axis`
155-
let view = view.view().into_dyn();
156-
157173
let blank_lines = "\n".repeat(shape.len() - 2);
158174
let indent = " ".repeat(depth + 1);
159175
let separator = format!(",\n{}{}", blank_lines, indent);
160176

161177
f.write_str("[")?;
162178
let limit = fmt_opt.collapse_limit(full_ndim - depth - 1);
163179
format_with_overflow(f, shape[0], limit, &separator, ELLIPSIS, &mut |f, index| {
164-
format_array(
165-
&view.index_axis(Axis(0), index),
180+
format_array_inner(
181+
view.index_axis(Axis(0), index),
166182
f,
167183
format.clone(),
168184
fmt_opt,
@@ -187,7 +203,7 @@ where
187203
{
188204
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189205
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
190-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
206+
format_array(self, f, <_>::fmt, &fmt_opt)
191207
}
192208
}
193209

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

206222
// Add extra information for Debug
207223
write!(
@@ -229,7 +245,7 @@ where
229245
{
230246
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231247
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
232-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
248+
format_array(self, f, <_>::fmt, &fmt_opt)
233249
}
234250
}
235251

@@ -243,7 +259,7 @@ where
243259
{
244260
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245261
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
246-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
262+
format_array(self, f, <_>::fmt, &fmt_opt)
247263
}
248264
}
249265
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -256,7 +272,7 @@ where
256272
{
257273
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258274
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
259-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
275+
format_array(self, f, <_>::fmt, &fmt_opt)
260276
}
261277
}
262278

@@ -270,7 +286,7 @@ where
270286
{
271287
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
272288
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
273-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
289+
format_array(self, f, <_>::fmt, &fmt_opt)
274290
}
275291
}
276292

0 commit comments

Comments
 (0)