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)
}
}