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,30 @@ 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 (
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 > ,
117
139
f : & mut fmt:: Formatter < ' _ > ,
118
140
mut format : F ,
119
141
fmt_opt : & FormatOptions ,
@@ -122,18 +144,16 @@ fn format_array<A, S, D, F>(
122
144
) -> fmt:: Result
123
145
where
124
146
F : FnMut ( & A , & mut fmt:: Formatter < ' _ > ) -> fmt:: Result + Clone ,
125
- D : Dimension ,
126
- S : Data < Elem = A > ,
127
147
{
128
148
// If any of the axes has 0 length, we return the same empty array representation
129
149
// e.g. [[]] for 2-d arrays
130
- if view. shape ( ) . iter ( ) . any ( | & x| x == 0 ) {
150
+ if view. is_empty ( ) {
131
151
write ! ( f, "{}{}" , "[" . repeat( view. ndim( ) ) , "]" . repeat( view. ndim( ) ) ) ?;
132
152
return Ok ( ( ) ) ;
133
153
}
134
154
match view. shape ( ) {
135
155
// If it's 0 dimensional, we just print out the scalar
136
- & [ ] => format ( view. iter ( ) . next ( ) . unwrap ( ) , f) ?,
156
+ & [ ] => format ( & view[ [ ] ] , f) ?,
137
157
// We handle 1-D arrays as a special case
138
158
& [ len] => {
139
159
let view = view. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) ;
@@ -150,19 +170,15 @@ where
150
170
}
151
171
// For n-dimensional arrays, we proceed recursively
152
172
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
173
let blank_lines = "\n " . repeat ( shape. len ( ) - 2 ) ;
158
174
let indent = " " . repeat ( depth + 1 ) ;
159
175
let separator = format ! ( ",\n {}{}" , blank_lines, indent) ;
160
176
161
177
f. write_str ( "[" ) ?;
162
178
let limit = fmt_opt. collapse_limit ( full_ndim - depth - 1 ) ;
163
179
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) ,
166
182
f,
167
183
format. clone ( ) ,
168
184
fmt_opt,
@@ -187,7 +203,7 @@ where
187
203
{
188
204
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
189
205
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)
191
207
}
192
208
}
193
209
@@ -201,7 +217,7 @@ where
201
217
{
202
218
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
203
219
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) ?;
205
221
206
222
// Add extra information for Debug
207
223
write ! (
@@ -229,7 +245,7 @@ where
229
245
{
230
246
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
231
247
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)
233
249
}
234
250
}
235
251
@@ -243,7 +259,7 @@ where
243
259
{
244
260
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
245
261
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)
247
263
}
248
264
}
249
265
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -256,7 +272,7 @@ where
256
272
{
257
273
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
258
274
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)
260
276
}
261
277
}
262
278
@@ -270,7 +286,7 @@ where
270
286
{
271
287
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
272
288
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)
274
290
}
275
291
}
276
292
0 commit comments