@@ -17,10 +17,10 @@ use crate::dimension::IntoDimension;
17
17
18
18
const PRINT_ELEMENTS_LIMIT : Ix = 3 ;
19
19
20
- fn format_array_v2 < A , S , D , F > ( view : & ArrayBase < S , D > ,
21
- f : & mut fmt:: Formatter ,
22
- mut format : F ,
23
- limit : Ix ) -> fmt:: Result
20
+ fn format_array < A , S , D , F > ( view : & ArrayBase < S , D > ,
21
+ f : & mut fmt:: Formatter ,
22
+ mut format : F ,
23
+ limit : Ix ) -> fmt:: Result
24
24
where F : FnMut ( & A , & mut fmt:: Formatter ) -> fmt:: Result ,
25
25
D : Dimension ,
26
26
S : Data < Elem =A > ,
@@ -37,8 +37,8 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
37
37
. map ( |( axis, _) | axis)
38
38
. collect ( ) ;
39
39
40
- let ndim = view. dim ( ) . into_dimension ( ) . slice ( ) . len ( ) ;
41
- let nth_idx_max = view. shape ( ) . iter ( ) . last ( ) . unwrap ( ) ;
40
+ let ndim = view. ndim ( ) ;
41
+ let nth_idx_max = view. shape ( ) [ ndim- 1 ] ;
42
42
43
43
// None will be an empty iter.
44
44
let mut last_index = match view. dim ( ) . into_dimension ( ) . first_index ( ) {
@@ -58,7 +58,6 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
58
58
// as cues for when to add []'s and how many to add.
59
59
for ( index, elt) in view. indexed_iter ( ) {
60
60
let index = index. into_dimension ( ) ;
61
- let take_n = if ndim == 0 { 1 } else { ndim - 1 } ;
62
61
let mut update_index = false ;
63
62
64
63
let skip_row_for_axis = overflow_axes. iter ( )
@@ -78,7 +77,7 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
78
77
79
78
for ( i, ( a, b) ) in index. slice ( )
80
79
. iter ( )
81
- . take ( take_n )
80
+ . take ( ndim- 1 )
82
81
. zip ( last_index. slice ( ) . iter ( ) )
83
82
. enumerate ( ) {
84
83
if a != b {
@@ -142,76 +141,6 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
142
141
Ok ( ( ) )
143
142
}
144
143
145
- #[ allow( dead_code) ]
146
- fn format_array < A , S , D , F > ( view : & ArrayBase < S , D > , f : & mut fmt:: Formatter ,
147
- mut format : F )
148
- -> fmt:: Result
149
- where F : FnMut ( & A , & mut fmt:: Formatter ) -> fmt:: Result ,
150
- D : Dimension ,
151
- S : Data < Elem =A > ,
152
- {
153
- let ndim = view. dim . slice ( ) . len ( ) ;
154
- /* private nowadays
155
- if ndim > 0 && f.width.is_none() {
156
- f.width = Some(4)
157
- }
158
- */
159
- // None will be an empty iter.
160
- let mut last_index = match view. dim . first_index ( ) {
161
- None => view. dim . clone ( ) ,
162
- Some ( ix) => ix,
163
- } ;
164
- for _ in 0 ..ndim {
165
- write ! ( f, "[" ) ?;
166
- }
167
- let mut first = true ;
168
- // Simply use the indexed iterator, and take the index wraparounds
169
- // as cues for when to add []'s and how many to add.
170
- for ( index, elt) in view. indexed_iter ( ) {
171
- let index = index. into_dimension ( ) ;
172
- let take_n = if ndim == 0 { 1 } else { ndim - 1 } ;
173
- let mut update_index = false ;
174
- for ( i, ( a, b) ) in index. slice ( )
175
- . iter ( )
176
- . take ( take_n)
177
- . zip ( last_index. slice ( ) . iter ( ) )
178
- . enumerate ( ) {
179
- if a != b {
180
- // New row.
181
- // # of ['s needed
182
- let n = ndim - i - 1 ;
183
- for _ in 0 ..n {
184
- write ! ( f, "]" ) ?;
185
- }
186
- write ! ( f, "," ) ?;
187
- write ! ( f, "\n " ) ?;
188
- for _ in 0 ..ndim - n {
189
- write ! ( f, " " ) ?;
190
- }
191
- for _ in 0 ..n {
192
- write ! ( f, "[" ) ?;
193
- }
194
- first = true ;
195
- update_index = true ;
196
- break ;
197
- }
198
- }
199
- if !first {
200
- write ! ( f, ", " ) ?;
201
- }
202
- first = false ;
203
- format ( elt, f) ?;
204
-
205
- if update_index {
206
- last_index = index;
207
- }
208
- }
209
- for _ in 0 ..ndim {
210
- write ! ( f, "]" ) ?;
211
- }
212
- Ok ( ( ) )
213
- }
214
-
215
144
// NOTE: We can impl other fmt traits here
216
145
/// Format the array using `Display` and apply the formatting parameters used
217
146
/// to each element.
@@ -221,7 +150,7 @@ impl<'a, A: fmt::Display, S, D: Dimension> fmt::Display for ArrayBase<S, D>
221
150
where S : Data < Elem =A > ,
222
151
{
223
152
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
224
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
153
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
225
154
}
226
155
}
227
156
@@ -234,7 +163,7 @@ impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
234
163
{
235
164
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
236
165
// Add extra information for Debug
237
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT ) ?;
166
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT ) ?;
238
167
write ! ( f, " shape={:?}, strides={:?}, layout={:?}" ,
239
168
self . shape( ) , self . strides( ) , layout=self . view( ) . layout( ) ) ?;
240
169
match D :: NDIM {
@@ -253,7 +182,7 @@ impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for ArrayBase<S, D>
253
182
where S : Data < Elem =A > ,
254
183
{
255
184
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
256
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
185
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
257
186
}
258
187
}
259
188
@@ -265,7 +194,7 @@ impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for ArrayBase<S, D>
265
194
where S : Data < Elem =A > ,
266
195
{
267
196
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
268
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
197
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
269
198
}
270
199
}
271
200
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -276,7 +205,7 @@ impl<'a, A: fmt::LowerHex, S, D: Dimension> fmt::LowerHex for ArrayBase<S, D>
276
205
where S : Data < Elem =A > ,
277
206
{
278
207
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
279
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
208
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
280
209
}
281
210
}
282
211
@@ -288,7 +217,7 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
288
217
where S : Data < Elem =A > ,
289
218
{
290
219
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
291
- format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
220
+ format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
292
221
}
293
222
}
294
223
@@ -301,6 +230,31 @@ mod formatting_with_omit {
301
230
println ! ( "Expected output:\n {}\n Actual output:\n {}" , expected, actual) ;
302
231
}
303
232
233
+ #[ test]
234
+ fn empty_arrays ( ) {
235
+ let a: Array2 < u32 > = arr2 ( & [ [ ] , [ ] ] ) ;
236
+ let actual_output = format ! ( "{}" , a) ;
237
+ let expected_output = String :: from ( "[[]]" ) ;
238
+ assert_eq ! ( actual_output, expected_output) ;
239
+ }
240
+
241
+ #[ test]
242
+ fn zero_length_axes ( ) {
243
+ let a = Array3 :: < f32 > :: zeros ( ( 3 , 0 , 4 ) ) ;
244
+ let actual_output = format ! ( "{}" , a) ;
245
+ let expected_output = String :: from ( "[[[]]]" ) ;
246
+ assert_eq ! ( actual_output, expected_output) ;
247
+ }
248
+
249
+ #[ test]
250
+ fn dim_0 ( ) {
251
+ let element = 12 ;
252
+ let a = arr0 ( element) ;
253
+ let actual_output = format ! ( "{}" , a) ;
254
+ let expected_output = format ! ( "{}" , element) ;
255
+ assert_eq ! ( actual_output, expected_output) ;
256
+ }
257
+
304
258
#[ test]
305
259
fn dim_1 ( ) {
306
260
let overflow: usize = 5 ;
0 commit comments