1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::fmt;
use super::{
ArrayBase,
Data,
Dimension,
};
fn format_array<A, S, D, F>(view: &ArrayBase<S, D>, f: &mut fmt::Formatter,
mut format: F)
-> fmt::Result
where F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result,
D: Dimension,
S: Data<Elem=A>,
{
let ndim = view.dim.slice().len();
let mut last_index = match view.dim.first_index() {
None => view.dim.clone(),
Some(ix) => ix,
};
for _ in 0..ndim {
try!(write!(f, "["));
}
let mut first = true;
for (index, elt) in view.indexed_iter() {
let take_n = if ndim == 0 { 1 } else { ndim - 1 };
let mut update_index = false;
for (i, (a, b)) in index.slice()
.iter()
.take(take_n)
.zip(last_index.slice().iter())
.enumerate() {
if a != b {
let n = ndim - i - 1;
for _ in 0..n {
try!(write!(f, "]"));
}
try!(write!(f, ","));
if !f.alternate() {
try!(write!(f, "\n"));
}
for _ in 0..ndim - n {
try!(write!(f, " "));
}
for _ in 0..n {
try!(write!(f, "["));
}
first = true;
update_index = true;
break;
}
}
if !first {
try!(write!(f, ", "));
}
first = false;
try!(format(elt, f));
if update_index {
last_index = index;
}
}
for _ in 0..ndim {
try!(write!(f, "]"));
}
Ok(())
}
impl<'a, A: fmt::Display, S, D: Dimension> fmt::Display for ArrayBase<S, D>
where S: Data<Elem=A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format_array(self, f, <_>::fmt)
}
}
impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
where S: Data<Elem=A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(format_array(self, f, <_>::fmt));
try!(write!(f, " shape={:?}, strides={:?}", self.shape(), self.strides()));
Ok(())
}
}
impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for ArrayBase<S, D>
where S: Data<Elem=A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format_array(self, f, <_>::fmt)
}
}
impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for ArrayBase<S, D>
where S: Data<Elem=A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format_array(self, f, <_>::fmt)
}
}
impl<'a, A: fmt::LowerHex, S, D: Dimension> fmt::LowerHex for ArrayBase<S, D>
where S: Data<Elem=A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format_array(self, f, <_>::fmt)
}
}