Skip to content

Commit 3474d3f

Browse files
author
andrei-papou
committed
Some fixes:
- fixed typo in ArrayDisplayMode comment - updated ArrayDisplayMode constructor to use shape instead of array - fixed output for complex dimensions
1 parent 2450f50 commit 3474d3f

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

src/arrayformat.rs

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::dimension::IntoDimension;
1717

1818
#[derive(Debug)]
1919
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.
2121
Full,
2222
// Omit central values of the nth axis. Since we print that axis horizontally, ellipses
2323
// on each row do something like a split of the array into 2 parts vertically.
@@ -32,62 +32,66 @@ enum ArrayDisplayMode {
3232
const PRINT_ELEMENTS_LIMIT: Ix = 5;
3333

3434
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
3836
{
39-
let last_dim = match arr.shape().len().checked_sub(1) {
37+
let last_dim = match shape.len().checked_sub(1) {
4038
Some(v) => v,
4139
None => {
4240
return ArrayDisplayMode::Full;
4341
}
4442
};
4543

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() {
4846
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);
6248
}
6349
}
6450

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)
6966
}
7067
}
7168

72-
fn h_split_offset(&self) -> Option<Ix> {
69+
fn h_split_axis(&self) -> Option<Ix> {
7370
match self {
7471
ArrayDisplayMode::DoubleSplit(axis) | ArrayDisplayMode::HSplit(axis) => {
75-
Some(axis + 1usize)
72+
Some(*axis)
7673
},
7774
_ => None
7875
}
7976
}
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+
}
8084
}
8185

8286
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
8690
where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
8791
D: Dimension,
8892
S: Data<Elem=A>,
8993
{
90-
let display_mode = ArrayDisplayMode::from_array(view, limit);
94+
let display_mode = ArrayDisplayMode::from_shape(view.shape(), limit);
9195

9296
let ndim = view.dim().into_dimension().slice().len();
9397
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>,
134138
.zip(last_index.slice().iter())
135139
.enumerate() {
136140
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+
137147
if print_row {
138148
printed_ellipses_v = false;
139149
// New row.
@@ -356,3 +366,19 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
356366
format_array(self, f, <_>::fmt)
357367
}
358368
}
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

Comments
 (0)