Skip to content

Commit c1036fb

Browse files
author
LukeMathWalker
committed
First and last elements have to be special cased. Done for multidimensional
1 parent 198d689 commit c1036fb

1 file changed

Lines changed: 29 additions & 138 deletions

File tree

src/arrayformat.rs

Lines changed: 29 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,10 @@ use super::{
1414
NdProducer,
1515
Ix
1616
};
17-
use crate::dimension::IntoDimension;
1817
use crate::aliases::Ix1;
1918

2019
const PRINT_ELEMENTS_LIMIT: Ix = 3;
2120

22-
fn get_overflow_axes(shape: &[Ix], limit: usize) -> Vec<usize> {
23-
shape.iter()
24-
.enumerate()
25-
.rev()
26-
.filter(|(_, axis_size)| **axis_size > 2 * limit)
27-
.map(|(axis, _)| axis)
28-
.collect()
29-
}
30-
31-
fn get_highest_axis_to_skip(overflow_axes: &Vec<usize>,
32-
shape: &[Ix],
33-
index: &[Ix],
34-
limit: &usize) -> Option<usize> {
35-
overflow_axes.iter()
36-
.filter(|&axis| {
37-
if *axis == shape.len() - 1 {
38-
return false
39-
};
40-
let sa_idx_max = shape[*axis];
41-
let sa_idx_val = index[*axis];
42-
sa_idx_val >= *limit && sa_idx_val < sa_idx_max - *limit
43-
})
44-
.min()
45-
.map(|v| *v)
46-
}
47-
48-
fn get_highest_changed_axis(index: &[Ix], prev_index: &[Ix]) -> Option<usize> {
49-
index.iter()
50-
.take(index.len() - 1)
51-
.zip(prev_index.iter())
52-
.enumerate()
53-
.filter(|(_, (a, b))| a != b)
54-
.map(|(i, _)| i)
55-
.next()
56-
}
57-
5821
fn format_1d_array<A, S, F>(
5922
view: &ArrayBase<S, Ix1>,
6023
f: &mut fmt::Formatter,
@@ -64,10 +27,27 @@ fn format_1d_array<A, S, F>(
6427
F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
6528
S: Data<Elem=A>,
6629
{
67-
unimplemented!()
30+
let n = view.len();
31+
let indexes_to_be_printed: Vec<Option<usize>> = if n <= 2 * limit {
32+
(0..n).map(|x| Some(x)).collect()
33+
} else {
34+
let mut v: Vec<Option<usize>> = (0..limit).map(|x| Some(x)).collect();
35+
v.push(None);
36+
v.extend((n-limit..n).map(|x| Some(x)));
37+
v
38+
};
39+
write!(f, "[")?;
40+
for index in indexes_to_be_printed {
41+
match index {
42+
Some(i) => format(&view[i], f)?,
43+
None => write!(f, ", ..., ")?,
44+
}
45+
}
46+
write!(f, "]")?;
47+
Ok(())
6848
}
6949

70-
fn format_array_v2<A, S, D, F>(
50+
fn format_array<A, S, D, F>(
7151
view: &ArrayBase<S, D>,
7252
f: &mut fmt::Formatter,
7353
mut format: F,
@@ -91,113 +71,24 @@ where
9171
v.extend((first_axis_length-limit..first_axis_length).map(|x| Some(x)));
9272
v
9373
};
94-
write!(f, "[")?;
74+
writeln!(f, "[")?;
9575
for index in indexes_to_be_printed {
9676
match index {
97-
Some(i) => format_array_v2(
98-
&view.index_axis(Axis(0), i), f, format.clone(), limit
99-
)?,
77+
Some(i) => {
78+
write!(f, " ")?;
79+
format_array(
80+
&view.index_axis(Axis(0), i), f, format.clone(), limit
81+
)?;
82+
writeln!(f, ",")?
83+
},
10084
None => {
101-
writeln!(f, "...,")?
85+
writeln!(f, " ...,")?
10286
}
10387
}
10488
}
105-
write!(f, "]")?;
106-
}
107-
}
108-
Ok(())
109-
}
110-
111-
fn format_array<A, S, D, F>(view: &ArrayBase<S, D>,
112-
f: &mut fmt::Formatter,
113-
mut format: F,
114-
limit: Ix) -> fmt::Result
115-
where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
116-
D: Dimension,
117-
S: Data<Elem=A>,
118-
{
119-
if view.shape().is_empty() {
120-
// Handle 0-dimensional array case first
121-
return format(view.iter().next().unwrap(), f)
122-
}
123-
124-
let overflow_axes: Vec<Ix> = get_overflow_axes(view.shape(), limit);
125-
126-
let ndim = view.ndim();
127-
let nth_idx_max = view.shape()[ndim-1];
128-
129-
// None will be an empty iter.
130-
let mut last_index = match view.dim().into_dimension().first_index() {
131-
None => view.dim().into_dimension().clone(),
132-
Some(ix) => ix,
133-
};
134-
write!(f, "{}", "[".repeat(ndim))?;
135-
// Shows if ellipses for horizontal split were printed.
136-
let mut printed_ellipses_h = vec![false; ndim];
137-
// Shows if the row was printed for the first time after horizontal split.
138-
let mut no_rows_after_skip_yet = false;
139-
140-
// Simply use the indexed iterator, and take the index wraparounds
141-
// as cues for when to add []'s and how many to add.
142-
for (index, elt) in view.indexed_iter() {
143-
let index = index.into_dimension();
144-
145-
let skip_row_for_axis = get_highest_axis_to_skip(
146-
&overflow_axes,
147-
view.shape(),
148-
index.slice(),
149-
&limit
150-
);
151-
if skip_row_for_axis.is_some() {
152-
no_rows_after_skip_yet = true;
153-
}
154-
155-
let max_changed_idx = get_highest_changed_axis(index.slice(), last_index.slice());
156-
if let Some(i) = max_changed_idx {
157-
printed_ellipses_h.iter_mut().skip(i + 1).for_each(|e| { *e = false; });
158-
159-
if skip_row_for_axis.is_none() {
160-
// New row.
161-
// # of ['s needed
162-
let n = ndim - i - 1;
163-
if !no_rows_after_skip_yet {
164-
write!(f, "{}", "]".repeat(n))?;
165-
writeln!(f, ",")?;
166-
}
167-
no_rows_after_skip_yet = false;
168-
write!(f, "{}", " ".repeat(ndim - n))?;
169-
write!(f, "{}", "[".repeat(n))?;
170-
} else if !printed_ellipses_h[skip_row_for_axis.unwrap()] {
171-
let ax = skip_row_for_axis.unwrap();
172-
let n = ndim - i - 1;
173-
write!(f, "{}", "]".repeat(n))?;
174-
writeln!(f, ",")?;
175-
write!(f, "{}", " ".repeat(ax + 1))?;
176-
writeln!(f, "...,")?;
177-
printed_ellipses_h[ax] = true;
178-
}
179-
last_index = index.clone();
180-
}
181-
182-
if skip_row_for_axis.is_none() {
183-
let nth_idx_op = index.slice().iter().last();
184-
if overflow_axes.contains(&(ndim - 1)) {
185-
let nth_idx_val = nth_idx_op.unwrap();
186-
if nth_idx_val >= &limit && nth_idx_val < &(nth_idx_max - &limit) {
187-
if nth_idx_val == &limit {
188-
write!(f, ", ...")?;
189-
}
190-
continue;
191-
}
192-
}
193-
194-
if max_changed_idx.is_none() && !index.slice().iter().all(|x| *x == 0) {
195-
write!(f, ", ")?;
196-
}
197-
format(elt, f)?;
89+
writeln!(f, "]")?;
19890
}
19991
}
200-
write!(f, "{}", "]".repeat(ndim))?;
20192
Ok(())
20293
}
20394

0 commit comments

Comments
 (0)