@@ -28,14 +28,15 @@ fn format_1d_array<A, S, F>(
28
28
S : Data < Elem =A > ,
29
29
{
30
30
let n = view. len ( ) ;
31
- let indexes_to_be_printed = indexes_to_be_printed ( n, limit) ;
32
- let last_index = indexes_to_be_printed. len ( ) ;
31
+ let to_be_printed = to_be_printed ( n, limit) ;
32
+ let n_to_be_printed = to_be_printed. len ( ) ;
33
+ let is_last = |j| j == n_to_be_printed - 1 ;
33
34
write ! ( f, "[" ) ?;
34
- for ( j, index) in indexes_to_be_printed . into_iter ( ) . enumerate ( ) {
35
+ for ( j, index) in to_be_printed . into_iter ( ) . enumerate ( ) {
35
36
match index {
36
37
Some ( i) => {
37
38
format ( & view[ i] , f) ?;
38
- if j != ( last_index- 1 ) {
39
+ if ! is_last ( j ) {
39
40
write ! ( f, ", " ) ?;
40
41
}
41
42
} ,
@@ -46,7 +47,10 @@ fn format_1d_array<A, S, F>(
46
47
Ok ( ( ) )
47
48
}
48
49
49
- fn indexes_to_be_printed ( length : usize , limit : usize ) -> Vec < Option < usize > > {
50
+ // Returns what indexes should be printed for a certain axis.
51
+ // If the axis is longer than 2 * limit, a `None` is inserted
52
+ // where indexes are being omitted.
53
+ fn to_be_printed ( length : usize , limit : usize ) -> Vec < Option < usize > > {
50
54
if length <= 2 * limit {
51
55
( 0 ..length) . map ( |x| Some ( x) ) . collect ( )
52
56
} else {
@@ -67,32 +71,44 @@ where
67
71
D : Dimension ,
68
72
S : Data < Elem =A > ,
69
73
{
74
+ // If any of the axes has 0 length, we return the same empty array representation
75
+ // e.g. [[]] for 2-d arrays
70
76
if view. shape ( ) . iter ( ) . any ( |& x| x == 0 ) {
71
77
write ! ( f, "{}{}" , "[" . repeat( view. ndim( ) ) , "]" . repeat( view. ndim( ) ) ) ?;
72
78
return Ok ( ( ) )
73
79
}
74
80
match view. shape ( ) {
81
+ // If it's 0 dimensional, we just print out the scalar
75
82
[ ] => format ( view. iter ( ) . next ( ) . unwrap ( ) , f) ?,
83
+ // We delegate 1-dimensional arrays to a specialized function
76
84
[ _] => format_1d_array ( & view. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) , f, format, limit) ?,
85
+ // For n-dimensional arrays, we proceed recursively
77
86
shape => {
87
+ // Cast into a dynamically dimensioned view
88
+ // This is required to be able to use `index_axis`
78
89
let view = view. view ( ) . into_dyn ( ) ;
79
- let first_axis_length = shape[ 0 ] ;
80
- let indexes_to_be_printed = indexes_to_be_printed ( first_axis_length, limit) ;
81
- let n_to_be_printed = indexes_to_be_printed. len ( ) ;
90
+ // We start by checking what indexes from the first axis should be printed
91
+ // We put a `None` in the middle if we are omitting elements
92
+ let to_be_printed = to_be_printed ( shape[ 0 ] , limit) ;
93
+
94
+ let n_to_be_printed = to_be_printed. len ( ) ;
95
+ let is_last = |j| j == n_to_be_printed - 1 ;
96
+
82
97
write ! ( f, "[" ) ?;
83
- for ( j, index) in indexes_to_be_printed . into_iter ( ) . enumerate ( ) {
98
+ for ( j, index) in to_be_printed . into_iter ( ) . enumerate ( ) {
84
99
match index {
85
100
Some ( i) => {
101
+ // Proceed recursively with the (n-1)-dimensional slice
86
102
format_array (
87
103
& view. index_axis ( Axis ( 0 ) , i) , f, format. clone ( ) , limit
88
104
) ?;
89
- if j != ( n_to_be_printed -1 ) {
105
+ // We need to add a separator after each slice,
106
+ // apart from the last one
107
+ if !is_last ( j) {
90
108
write ! ( f, ",\n " ) ?
91
109
}
92
110
} ,
93
- None => {
94
- write ! ( f, "...,\n " ) ?
95
- }
111
+ None => write ! ( f, "...,\n " ) ?
96
112
}
97
113
}
98
114
write ! ( f, "]" ) ?;
0 commit comments