Skip to content

Commit 49b4338

Browse files
author
Andrew
authored
Merge pull request #2 from LukeMathWalker/smart-debug-formatting-review
Smart debug formatting review
2 parents 18c868a + 27c4a92 commit 49b4338

File tree

1 file changed

+38
-84
lines changed

1 file changed

+38
-84
lines changed

src/arrayformat.rs

Lines changed: 38 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crate::dimension::IntoDimension;
1717

1818
const PRINT_ELEMENTS_LIMIT: Ix = 3;
1919

20-
fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
21-
f: &mut fmt::Formatter,
22-
mut format: F,
23-
limit: Ix) -> fmt::Result
20+
fn format_array<A, S, D, F>(view: &ArrayBase<S, D>,
21+
f: &mut fmt::Formatter,
22+
mut format: F,
23+
limit: Ix) -> fmt::Result
2424
where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
2525
D: Dimension,
2626
S: Data<Elem=A>,
@@ -37,8 +37,8 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
3737
.map(|(axis, _)| axis)
3838
.collect();
3939

40-
let ndim = view.dim().into_dimension().slice().len();
41-
let nth_idx_max = view.shape().iter().last().unwrap();
40+
let ndim = view.ndim();
41+
let nth_idx_max = view.shape()[ndim-1];
4242

4343
// None will be an empty iter.
4444
let mut last_index = match view.dim().into_dimension().first_index() {
@@ -58,7 +58,6 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
5858
// as cues for when to add []'s and how many to add.
5959
for (index, elt) in view.indexed_iter() {
6060
let index = index.into_dimension();
61-
let take_n = if ndim == 0 { 1 } else { ndim - 1 };
6261
let mut update_index = false;
6362

6463
let skip_row_for_axis = overflow_axes.iter()
@@ -78,7 +77,7 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
7877

7978
for (i, (a, b)) in index.slice()
8079
.iter()
81-
.take(take_n)
80+
.take(ndim-1)
8281
.zip(last_index.slice().iter())
8382
.enumerate() {
8483
if a != b {
@@ -142,76 +141,6 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
142141
Ok(())
143142
}
144143

145-
#[allow(dead_code)]
146-
fn format_array<A, S, D, F>(view: &ArrayBase<S, D>, f: &mut fmt::Formatter,
147-
mut format: F)
148-
-> fmt::Result
149-
where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
150-
D: Dimension,
151-
S: Data<Elem=A>,
152-
{
153-
let ndim = view.dim.slice().len();
154-
/* private nowadays
155-
if ndim > 0 && f.width.is_none() {
156-
f.width = Some(4)
157-
}
158-
*/
159-
// None will be an empty iter.
160-
let mut last_index = match view.dim.first_index() {
161-
None => view.dim.clone(),
162-
Some(ix) => ix,
163-
};
164-
for _ in 0..ndim {
165-
write!(f, "[")?;
166-
}
167-
let mut first = true;
168-
// Simply use the indexed iterator, and take the index wraparounds
169-
// as cues for when to add []'s and how many to add.
170-
for (index, elt) in view.indexed_iter() {
171-
let index = index.into_dimension();
172-
let take_n = if ndim == 0 { 1 } else { ndim - 1 };
173-
let mut update_index = false;
174-
for (i, (a, b)) in index.slice()
175-
.iter()
176-
.take(take_n)
177-
.zip(last_index.slice().iter())
178-
.enumerate() {
179-
if a != b {
180-
// New row.
181-
// # of ['s needed
182-
let n = ndim - i - 1;
183-
for _ in 0..n {
184-
write!(f, "]")?;
185-
}
186-
write!(f, ",")?;
187-
write!(f, "\n")?;
188-
for _ in 0..ndim - n {
189-
write!(f, " ")?;
190-
}
191-
for _ in 0..n {
192-
write!(f, "[")?;
193-
}
194-
first = true;
195-
update_index = true;
196-
break;
197-
}
198-
}
199-
if !first {
200-
write!(f, ", ")?;
201-
}
202-
first = false;
203-
format(elt, f)?;
204-
205-
if update_index {
206-
last_index = index;
207-
}
208-
}
209-
for _ in 0..ndim {
210-
write!(f, "]")?;
211-
}
212-
Ok(())
213-
}
214-
215144
// NOTE: We can impl other fmt traits here
216145
/// Format the array using `Display` and apply the formatting parameters used
217146
/// to each element.
@@ -221,7 +150,7 @@ impl<'a, A: fmt::Display, S, D: Dimension> fmt::Display for ArrayBase<S, D>
221150
where S: Data<Elem=A>,
222151
{
223152
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
224-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
153+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
225154
}
226155
}
227156

@@ -234,7 +163,7 @@ impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
234163
{
235164
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
236165
// Add extra information for Debug
237-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)?;
166+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)?;
238167
write!(f, " shape={:?}, strides={:?}, layout={:?}",
239168
self.shape(), self.strides(), layout=self.view().layout())?;
240169
match D::NDIM {
@@ -253,7 +182,7 @@ impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for ArrayBase<S, D>
253182
where S: Data<Elem=A>,
254183
{
255184
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
185+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
257186
}
258187
}
259188

@@ -265,7 +194,7 @@ impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for ArrayBase<S, D>
265194
where S: Data<Elem=A>,
266195
{
267196
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
268-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
197+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
269198
}
270199
}
271200
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -276,7 +205,7 @@ impl<'a, A: fmt::LowerHex, S, D: Dimension> fmt::LowerHex for ArrayBase<S, D>
276205
where S: Data<Elem=A>,
277206
{
278207
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
279-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
208+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
280209
}
281210
}
282211

@@ -288,7 +217,7 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
288217
where S: Data<Elem=A>,
289218
{
290219
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
291-
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
220+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
292221
}
293222
}
294223

@@ -301,6 +230,31 @@ mod formatting_with_omit {
301230
println!("Expected output:\n{}\nActual output:\n{}", expected, actual);
302231
}
303232

233+
#[test]
234+
fn empty_arrays() {
235+
let a: Array2<u32> = arr2(&[[], []]);
236+
let actual_output = format!("{}", a);
237+
let expected_output = String::from("[[]]");
238+
assert_eq!(actual_output, expected_output);
239+
}
240+
241+
#[test]
242+
fn zero_length_axes() {
243+
let a = Array3::<f32>::zeros((3, 0, 4));
244+
let actual_output = format!("{}", a);
245+
let expected_output = String::from("[[[]]]");
246+
assert_eq!(actual_output, expected_output);
247+
}
248+
249+
#[test]
250+
fn dim_0() {
251+
let element = 12;
252+
let a = arr0(element);
253+
let actual_output = format!("{}", a);
254+
let expected_output = format!("{}", element);
255+
assert_eq!(actual_output, expected_output);
256+
}
257+
304258
#[test]
305259
fn dim_1() {
306260
let overflow: usize = 5;

0 commit comments

Comments
 (0)