5
5
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6
6
// option. This file may not be copied, modified, or distributed
7
7
// 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 } ;
10
10
use std:: fmt;
11
11
12
12
/// Default threshold, below this element count, we don't ellipsize
@@ -84,9 +84,8 @@ fn format_with_overflow(
84
84
limit : usize ,
85
85
separator : & str ,
86
86
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 {
90
89
if length == 0 {
91
90
// no-op
92
91
} else if length <= limit {
@@ -113,7 +112,23 @@ fn format_with_overflow(
113
112
}
114
113
115
114
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 > ,
117
132
f : & mut fmt:: Formatter < ' _ > ,
118
133
mut format : F ,
119
134
fmt_opt : & FormatOptions ,
@@ -122,18 +137,16 @@ fn format_array<A, S, D, F>(
122
137
) -> fmt:: Result
123
138
where
124
139
F : FnMut ( & A , & mut fmt:: Formatter < ' _ > ) -> fmt:: Result + Clone ,
125
- D : Dimension ,
126
- S : Data < Elem = A > ,
127
140
{
128
141
// If any of the axes has 0 length, we return the same empty array representation
129
142
// e.g. [[]] for 2-d arrays
130
- if view. shape ( ) . iter ( ) . any ( | & x| x == 0 ) {
143
+ if view. is_empty ( ) {
131
144
write ! ( f, "{}{}" , "[" . repeat( view. ndim( ) ) , "]" . repeat( view. ndim( ) ) ) ?;
132
145
return Ok ( ( ) ) ;
133
146
}
134
147
match view. shape ( ) {
135
148
// If it's 0 dimensional, we just print out the scalar
136
- & [ ] => format ( view. iter ( ) . next ( ) . unwrap ( ) , f) ?,
149
+ & [ ] => format ( & view[ [ ] ] , f) ?,
137
150
// We handle 1-D arrays as a special case
138
151
& [ len] => {
139
152
let view = view. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
@@ -150,19 +163,15 @@ where
150
163
}
151
164
// For n-dimensional arrays, we proceed recursively
152
165
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
-
157
166
let blank_lines = "\n " . repeat ( shape. len ( ) - 2 ) ;
158
167
let indent = " " . repeat ( depth + 1 ) ;
159
168
let separator = format ! ( ",\n {}{}" , blank_lines, indent) ;
160
169
161
170
f. write_str ( "[" ) ?;
162
171
let limit = fmt_opt. collapse_limit ( full_ndim - depth - 1 ) ;
163
172
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) ,
166
175
f,
167
176
format. clone ( ) ,
168
177
fmt_opt,
@@ -187,7 +196,7 @@ where
187
196
{
188
197
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
189
198
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)
191
200
}
192
201
}
193
202
@@ -201,7 +210,7 @@ where
201
210
{
202
211
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
203
212
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) ?;
205
214
206
215
// Add extra information for Debug
207
216
write ! (
@@ -229,7 +238,7 @@ where
229
238
{
230
239
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
231
240
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)
233
242
}
234
243
}
235
244
@@ -243,7 +252,7 @@ where
243
252
{
244
253
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
245
254
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)
247
256
}
248
257
}
249
258
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -256,7 +265,7 @@ where
256
265
{
257
266
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
258
267
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)
260
269
}
261
270
}
262
271
@@ -270,7 +279,7 @@ where
270
279
{
271
280
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
272
281
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)
274
283
}
275
284
}
276
285
0 commit comments