@@ -17,7 +17,7 @@ use crate::dimension::IntoDimension;
17
17
18
18
#[ derive( Debug ) ]
19
19
enum ArrayDisplayMode {
20
- // Array is small enough to me printed without omitting any values.
20
+ // Array is small enough to be printed without omitting any values.
21
21
Full ,
22
22
// Omit central values of the nth axis. Since we print that axis horizontally, ellipses
23
23
// on each row do something like a split of the array into 2 parts vertically.
@@ -32,62 +32,66 @@ enum ArrayDisplayMode {
32
32
const PRINT_ELEMENTS_LIMIT : Ix = 5 ;
33
33
34
34
impl ArrayDisplayMode {
35
- fn from_array < A , S , D > ( arr : & ArrayBase < S , D > , limit : usize ) -> ArrayDisplayMode
36
- where S : Data < Elem =A > ,
37
- D : Dimension
35
+ fn from_shape ( shape : & [ Ix ] , limit : Ix ) -> ArrayDisplayMode
38
36
{
39
- let last_dim = match arr . shape ( ) . len ( ) . checked_sub ( 1 ) {
37
+ let last_dim = match shape. len ( ) . checked_sub ( 1 ) {
40
38
Some ( v) => v,
41
39
None => {
42
40
return ArrayDisplayMode :: Full ;
43
41
}
44
42
} ;
45
43
46
- let mut overflow_axis_pair : ( Option < usize > , Option < usize > ) = ( None , None ) ;
47
- for ( axis, axis_size) in arr . shape ( ) . iter ( ) . enumerate ( ) . rev ( ) {
44
+ let mut overflow_axes : Vec < Ix > = Vec :: with_capacity ( shape . len ( ) ) ;
45
+ for ( axis, axis_size) in shape. iter ( ) . enumerate ( ) . rev ( ) {
48
46
if * axis_size >= 2 * limit + 1 {
49
- match overflow_axis_pair. 0 {
50
- Some ( _) => {
51
- if let None = overflow_axis_pair. 1 {
52
- overflow_axis_pair. 1 = Some ( axis) ;
53
- }
54
- } ,
55
- None => {
56
- if axis != last_dim {
57
- return ArrayDisplayMode :: HSplit ( axis) ;
58
- }
59
- overflow_axis_pair. 0 = Some ( axis) ;
60
- }
61
- }
47
+ overflow_axes. push ( axis) ;
62
48
}
63
49
}
64
50
65
- match overflow_axis_pair {
66
- ( Some ( _) , Some ( h_axis) ) => ArrayDisplayMode :: DoubleSplit ( h_axis) ,
67
- ( Some ( _) , None ) => ArrayDisplayMode :: VSplit ,
68
- ( None , _) => ArrayDisplayMode :: Full ,
51
+ if overflow_axes. is_empty ( ) {
52
+ return ArrayDisplayMode :: Full ;
53
+ }
54
+
55
+ let min_ovf_axis = * overflow_axes. iter ( ) . min ( ) . unwrap ( ) ;
56
+ let max_ovf_axis = * overflow_axes. iter ( ) . max ( ) . unwrap ( ) ;
57
+
58
+ if max_ovf_axis == last_dim {
59
+ if min_ovf_axis != max_ovf_axis {
60
+ ArrayDisplayMode :: DoubleSplit ( min_ovf_axis)
61
+ } else {
62
+ ArrayDisplayMode :: VSplit
63
+ }
64
+ } else {
65
+ ArrayDisplayMode :: HSplit ( min_ovf_axis)
69
66
}
70
67
}
71
68
72
- fn h_split_offset ( & self ) -> Option < Ix > {
69
+ fn h_split_axis ( & self ) -> Option < Ix > {
73
70
match self {
74
71
ArrayDisplayMode :: DoubleSplit ( axis) | ArrayDisplayMode :: HSplit ( axis) => {
75
- Some ( axis + 1usize )
72
+ Some ( * axis)
76
73
} ,
77
74
_ => None
78
75
}
79
76
}
77
+
78
+ fn h_split_offset ( & self ) -> Option < Ix > {
79
+ match self . h_split_axis ( ) {
80
+ Some ( axis) => Some ( axis + 1usize ) ,
81
+ None => None
82
+ }
83
+ }
80
84
}
81
85
82
86
fn format_array_v2 < A , S , D , F > ( view : & ArrayBase < S , D > ,
83
- f : & mut fmt:: Formatter ,
84
- mut format : F ,
85
- limit : usize ) -> fmt:: Result
87
+ f : & mut fmt:: Formatter ,
88
+ mut format : F ,
89
+ limit : Ix ) -> fmt:: Result
86
90
where F : FnMut ( & A , & mut fmt:: Formatter ) -> fmt:: Result ,
87
91
D : Dimension ,
88
92
S : Data < Elem =A > ,
89
93
{
90
- let display_mode = ArrayDisplayMode :: from_array ( view, limit) ;
94
+ let display_mode = ArrayDisplayMode :: from_shape ( view. shape ( ) , limit) ;
91
95
92
96
let ndim = view. dim ( ) . into_dimension ( ) . slice ( ) . len ( ) ;
93
97
let nth_idx_max = if ndim > 0 { Some ( view. shape ( ) . iter ( ) . last ( ) . unwrap ( ) ) } else { None } ;
@@ -134,6 +138,12 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
134
138
. zip ( last_index. slice ( ) . iter ( ) )
135
139
. enumerate ( ) {
136
140
if a != b {
141
+ if let Some ( axis) = display_mode. h_split_axis ( ) {
142
+ if i < axis {
143
+ printed_ellipses_h = false ;
144
+ }
145
+ }
146
+
137
147
if print_row {
138
148
printed_ellipses_v = false ;
139
149
// New row.
@@ -356,3 +366,19 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
356
366
format_array ( self , f, <_ >:: fmt)
357
367
}
358
368
}
369
+
370
+ #[ cfg( test) ]
371
+ mod format_tests {
372
+ use super :: * ;
373
+
374
+ fn test_array_display_mode_from_shape ( ) {
375
+ let mode = ArrayDisplayMode :: from_shape ( & [ 4 , 4 ] , 2 ) ;
376
+ assert_eq ! ( mode, ArrayDisplayMode :: Full ) ;
377
+
378
+ let mode = ArrayDisplayMode :: from_shape ( & [ 3 , 6 ] , 2 ) ;
379
+ assert_eq ! ( mode, ArrayDisplayMode :: VSplit ) ;
380
+
381
+ let mode = ArrayDisplayMode :: from_shape ( & [ 5 , 6 , 3 ] , 2 ) ;
382
+ assert_eq ! ( mode, ArrayDisplayMode :: HSplit ( 1 ) ) ;
383
+ }
384
+ }
0 commit comments