Skip to content

Commit 4fe6e07

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 4fe6e07

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/arrayformat.rs

+31-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,23 @@ 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(array.view().into_dyn(), f, format, fmt_opt, 0, array.ndim())
128+
}
129+
130+
fn format_array_inner<A, F>(
131+
view: ArrayView<A, IxDyn>,
117132
f: &mut fmt::Formatter<'_>,
118133
mut format: F,
119134
fmt_opt: &FormatOptions,
@@ -122,18 +137,16 @@ fn format_array<A, S, D, F>(
122137
) -> fmt::Result
123138
where
124139
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
125-
D: Dimension,
126-
S: Data<Elem = A>,
127140
{
128141
// If any of the axes has 0 length, we return the same empty array representation
129142
// e.g. [[]] for 2-d arrays
130-
if view.shape().iter().any(|&x| x == 0) {
143+
if view.is_empty() {
131144
write!(f, "{}{}", "[".repeat(view.ndim()), "]".repeat(view.ndim()))?;
132145
return Ok(());
133146
}
134147
match view.shape() {
135148
// If it's 0 dimensional, we just print out the scalar
136-
&[] => format(view.iter().next().unwrap(), f)?,
149+
&[] => format(&view[[]], f)?,
137150
// We handle 1-D arrays as a special case
138151
&[len] => {
139152
let view = view.view().into_dimensionality::<Ix1>().unwrap();
@@ -150,19 +163,15 @@ where
150163
}
151164
// For n-dimensional arrays, we proceed recursively
152165
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-
157166
let blank_lines = "\n".repeat(shape.len() - 2);
158167
let indent = " ".repeat(depth + 1);
159168
let separator = format!(",\n{}{}", blank_lines, indent);
160169

161170
f.write_str("[")?;
162171
let limit = fmt_opt.collapse_limit(full_ndim - depth - 1);
163172
format_with_overflow(f, shape[0], limit, &separator, ELLIPSIS, &mut |f, index| {
164-
format_array(
165-
&view.index_axis(Axis(0), index),
173+
format_array_inner(
174+
view.index_axis(Axis(0), index),
166175
f,
167176
format.clone(),
168177
fmt_opt,
@@ -187,7 +196,7 @@ where
187196
{
188197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189198
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
190-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
199+
format_array(self, f, <_>::fmt, &fmt_opt)
191200
}
192201
}
193202

@@ -201,7 +210,7 @@ where
201210
{
202211
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203212
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
204-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())?;
213+
format_array(self, f, <_>::fmt, &fmt_opt)?;
205214

206215
// Add extra information for Debug
207216
write!(
@@ -229,7 +238,7 @@ where
229238
{
230239
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
231240
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
232-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
241+
format_array(self, f, <_>::fmt, &fmt_opt)
233242
}
234243
}
235244

@@ -243,7 +252,7 @@ where
243252
{
244253
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245254
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
246-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
255+
format_array(self, f, <_>::fmt, &fmt_opt)
247256
}
248257
}
249258
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -256,7 +265,7 @@ where
256265
{
257266
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258267
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
259-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
268+
format_array(self, f, <_>::fmt, &fmt_opt)
260269
}
261270
}
262271

@@ -270,7 +279,7 @@ where
270279
{
271280
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
272281
let fmt_opt = FormatOptions::default_for_array(self.len(), f.alternate());
273-
format_array(self, f, <_>::fmt, &fmt_opt, 0, self.ndim())
282+
format_array(self, f, <_>::fmt, &fmt_opt)
274283
}
275284
}
276285

0 commit comments

Comments
 (0)