@@ -28,14 +28,15 @@ fn format_1d_array<A, S, F>(
2828 S : Data < Elem =A > ,
2929{
3030 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 ;
3334 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 ( ) {
3536 match index {
3637 Some ( i) => {
3738 format ( & view[ i] , f) ?;
38- if j != ( last_index- 1 ) {
39+ if ! is_last ( j ) {
3940 write ! ( f, ", " ) ?;
4041 }
4142 } ,
@@ -46,7 +47,10 @@ fn format_1d_array<A, S, F>(
4647 Ok ( ( ) )
4748}
4849
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 > > {
5054 if length <= 2 * limit {
5155 ( 0 ..length) . map ( |x| Some ( x) ) . collect ( )
5256 } else {
@@ -67,32 +71,44 @@ where
6771 D : Dimension ,
6872 S : Data < Elem =A > ,
6973{
74+ // If any of the axes has 0 length, we return the same empty array representation
75+ // e.g. [[]] for 2-d arrays
7076 if view. shape ( ) . iter ( ) . any ( |& x| x == 0 ) {
7177 write ! ( f, "{}{}" , "[" . repeat( view. ndim( ) ) , "]" . repeat( view. ndim( ) ) ) ?;
7278 return Ok ( ( ) )
7379 }
7480 match view. shape ( ) {
81+ // If it's 0 dimensional, we just print out the scalar
7582 [ ] => format ( view. iter ( ) . next ( ) . unwrap ( ) , f) ?,
83+ // We delegate 1-dimensional arrays to a specialized function
7684 [ _] => format_1d_array ( & view. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) , f, format, limit) ?,
85+ // For n-dimensional arrays, we proceed recursively
7786 shape => {
87+ // Cast into a dynamically dimensioned view
88+ // This is required to be able to use `index_axis`
7889 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+
8297 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 ( ) {
8499 match index {
85100 Some ( i) => {
101+ // Proceed recursively with the (n-1)-dimensional slice
86102 format_array (
87103 & view. index_axis ( Axis ( 0 ) , i) , f, format. clone ( ) , limit
88104 ) ?;
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) {
90108 write ! ( f, ",\n " ) ?
91109 }
92110 } ,
93- None => {
94- write ! ( f, "...,\n " ) ?
95- }
111+ None => write ! ( f, "...,\n " ) ?
96112 }
97113 }
98114 write ! ( f, "]" ) ?;
0 commit comments