Skip to content

Commit e74834f

Browse files
committed
Re-define limit before using ellipsis in format
1 parent 76eaef4 commit e74834f

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

src/arrayformat.rs

+28-35
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ use super::{ArrayBase, Axis, Data, Dimension, Ix, NdProducer};
99
use crate::aliases::Ix1;
1010
use std::fmt;
1111

12-
const PRINT_ELEMENTS_LIMIT: Ix = 3;
12+
/// Maximum axis length before overflowing with an ellipsis.
13+
const AXIS_LEN_LIMIT: Ix = 6;
1314

15+
/// The string used as an ellipsis.
1416
const ELLIPSIS: &str = "...";
1517

1618
/// Formats the contents of a list of items, using an ellipsis to indicate when
17-
/// the length of the list is greater than `2 * limit`.
19+
/// the `length` of the list is greater than `limit`.
1820
///
1921
/// # Parameters
2022
///
2123
/// * `f`: The formatter.
2224
/// * `length`: The length of the list.
23-
/// * `limit`: Half the maximum number of items before indicating overflow with
24-
/// an ellipsis. Also, the number of items on either side of the ellipsis.
25+
/// * `limit`: The maximum number of items before overflow.
2526
/// * `separator`: Separator to write between items.
2627
/// * `ellipsis`: Ellipsis for indicating overflow.
2728
/// * `fmt_elem`: A function that formats an element in the list, given the
@@ -39,21 +40,22 @@ where
3940
{
4041
if length == 0 {
4142
// no-op
42-
} else if length <= 2 * limit {
43+
} else if length <= limit {
4344
fmt_elem(f, 0)?;
4445
(1..length).try_for_each(|i| {
4546
f.write_str(separator)?;
4647
fmt_elem(f, i)
4748
})?;
4849
} else {
50+
let edge = limit / 2;
4951
fmt_elem(f, 0)?;
50-
(1..limit).try_for_each(|i| {
52+
(1..edge).try_for_each(|i| {
5153
f.write_str(separator)?;
5254
fmt_elem(f, i)
5355
})?;
5456
f.write_str(separator)?;
5557
f.write_str(ellipsis)?;
56-
(length - limit..length).try_for_each(|i| {
58+
(length - edge..length).try_for_each(|i| {
5759
f.write_str(separator)?;
5860
fmt_elem(f, i)
5961
})?;
@@ -127,7 +129,7 @@ where
127129
S: Data<Elem = A>,
128130
{
129131
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
132+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
131133
}
132134
}
133135

@@ -141,7 +143,7 @@ where
141143
{
142144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143145
// Add extra information for Debug
144-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)?;
146+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)?;
145147
write!(
146148
f,
147149
", shape={:?}, strides={:?}, layout={:?}",
@@ -166,7 +168,7 @@ where
166168
S: Data<Elem = A>,
167169
{
168170
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
171+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
170172
}
171173
}
172174

@@ -179,7 +181,7 @@ where
179181
S: Data<Elem = A>,
180182
{
181183
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
184+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
183185
}
184186
}
185187
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -191,7 +193,7 @@ where
191193
S: Data<Elem = A>,
192194
{
193195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
196+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
195197
}
196198
}
197199

@@ -204,7 +206,7 @@ where
204206
S: Data<Elem = A>,
205207
{
206208
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
209+
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
208210
}
209211
}
210212

@@ -249,7 +251,7 @@ mod formatting_with_omit {
249251
#[test]
250252
fn dim_1() {
251253
let overflow: usize = 5;
252-
let a = Array1::from_elem((PRINT_ELEMENTS_LIMIT * 2 + overflow,), 1);
254+
let a = Array1::from_elem(AXIS_LEN_LIMIT + overflow, 1);
253255
let actual = format!("{}", a);
254256
let expected = "[1, 1, 1, ..., 1, 1, 1]";
255257
assert_str_eq(expected, &actual);
@@ -258,13 +260,13 @@ mod formatting_with_omit {
258260
#[test]
259261
fn dim_2_last_axis_overflow() {
260262
let overflow: usize = 3;
261-
let a = Array2::from_elem(
262-
(PRINT_ELEMENTS_LIMIT, PRINT_ELEMENTS_LIMIT * 2 + overflow),
263-
1,
264-
);
263+
let a = Array2::from_elem((AXIS_LEN_LIMIT, AXIS_LEN_LIMIT + overflow), 1);
265264
let actual = format!("{}", a);
266265
let expected = "\
267266
[[1, 1, 1, ..., 1, 1, 1],
267+
[1, 1, 1, ..., 1, 1, 1],
268+
[1, 1, 1, ..., 1, 1, 1],
269+
[1, 1, 1, ..., 1, 1, 1],
268270
[1, 1, 1, ..., 1, 1, 1],
269271
[1, 1, 1, ..., 1, 1, 1]]";
270272
assert_str_eq(expected, &actual);
@@ -273,32 +275,23 @@ mod formatting_with_omit {
273275
#[test]
274276
fn dim_2_non_last_axis_overflow() {
275277
let overflow: usize = 5;
276-
let a = Array2::from_elem(
277-
(PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT),
278-
1,
279-
);
278+
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT), 1);
280279
let actual = format!("{}", a);
281280
let expected = "\
282-
[[1, 1, 1],
283-
[1, 1, 1],
284-
[1, 1, 1],
281+
[[1, 1, 1, 1, 1, 1],
282+
[1, 1, 1, 1, 1, 1],
283+
[1, 1, 1, 1, 1, 1],
285284
...,
286-
[1, 1, 1],
287-
[1, 1, 1],
288-
[1, 1, 1]]";
285+
[1, 1, 1, 1, 1, 1],
286+
[1, 1, 1, 1, 1, 1],
287+
[1, 1, 1, 1, 1, 1]]";
289288
assert_str_eq(expected, &actual);
290289
}
291290

292291
#[test]
293292
fn dim_2_multi_directional_overflow() {
294293
let overflow: usize = 5;
295-
let a = Array2::from_elem(
296-
(
297-
PRINT_ELEMENTS_LIMIT * 2 + overflow,
298-
PRINT_ELEMENTS_LIMIT * 2 + overflow,
299-
),
300-
1,
301-
);
294+
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT + overflow), 1);
302295
let actual = format!("{}", a);
303296
let expected = "\
304297
[[1, 1, 1, ..., 1, 1, 1],

0 commit comments

Comments
 (0)