Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/linalg/basic/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ pub trait ArrayView1<T: Debug + Display + Copy + Sized>: Array<T, usize> {
}
}

/// Format a 1D view as a list, for example `[1.0, 2.5]`.
/// Elements use their `Debug` format. The elements are streamed into the
/// formatter and are not copied. Formatter flags such as `{:.2}` apply to each element.
impl<T: Debug + Display + Copy + Sized> fmt::Display for dyn ArrayView1<T> + '_ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iterator(0)).finish()
}
}

/// Trait for 2D-array
pub trait ArrayView2<T: Debug + Display + Copy + Sized>: Array<T, (usize, usize)> {
/// return max value in array
Expand Down Expand Up @@ -727,6 +736,15 @@ pub trait MutArrayView1<T: Debug + Display + Copy + Sized>:
}
}

/// Format a mutable 1D view as a list, for example `[1.0, 2.5]`.
/// Elements use their `Debug` format. The elements are streamed into the
/// formatter and are not copied. Formatter flags such as `{:.2}` apply to each element.
impl<T: Debug + Display + Copy + Sized> fmt::Display for dyn MutArrayView1<T> + '_ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iterator(0)).finish()
}
}

/// Trait for mutable 2D-array views
pub trait MutArrayView2<T: Debug + Display + Copy + Sized>:
MutArray<T, (usize, usize)> + ArrayView2<T>
Expand Down
67 changes: 66 additions & 1 deletion src/linalg/basic/vector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{Debug, Display};
use std::fmt::{self, Debug, Display};
use std::ops::Range;

use crate::linalg::basic::arrays::{Array, Array1, ArrayView1, MutArray, MutArrayView1};
Expand Down Expand Up @@ -152,6 +152,12 @@ impl<T: Debug + Display + Copy + Sized> MutArray<T, usize> for VecMutView<'_, T>
impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for VecMutView<'_, T> {}
impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for VecMutView<'_, T> {}

impl<T: Debug + Display + Copy + Sized> fmt::Display for VecMutView<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iterator(0)).finish()
}
}

impl<T: Debug + Display + Copy + Sized> Array<T, usize> for VecView<'_, T> {
fn get(&self, i: usize) -> &T {
&self.ptr[i]
Expand All @@ -173,6 +179,12 @@ impl<T: Debug + Display + Copy + Sized> Array<T, usize> for VecView<'_, T> {

impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for VecView<'_, T> {}

impl<T: Debug + Display + Copy + Sized> fmt::Display for VecView<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iterator(0)).finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -345,4 +357,57 @@ mod tests {

vector_ops(&x);
}

#[test]
fn test_display() {
let mut x = vec![1, 2, 3, 4];

let view = VecView { ptr: &x[1..3] };
assert_eq!("[2, 3]", format!("{view}"));

let mut_view = VecMutView { ptr: &mut x[..2] };
assert_eq!("[1, 2]", format!("{mut_view}"));
}

#[test]
fn test_display_slice() {
let x = vec![1, 2, 3, 4];
assert_eq!("[2, 3]", format!("{}", x.slice(1..3)));
assert_eq!("[]", format!("{}", x.slice(2..2)));
assert_eq!("[4]", format!("{}", x.slice(3..4)));

// Elements use the Debug format: 1.0 prints as "1.0", not "1".
let y = vec![1.0f64, 2.5, -0.5];
assert_eq!("[1.0, 2.5, -0.5]", format!("{}", y.slice(0..3)));
assert_eq!("[1.0]", format!("{}", y.slice(0..1)));
assert_eq!("[1.00, 2.50]", format!("{:.2}", y.slice(0..2)));
}

#[test]
fn test_display_slice_mut() {
let mut x = vec![1, 2, 3, 4];
assert_eq!("[2, 3]", format!("{}", x.slice_mut(1..3)));
assert_eq!("[]", format!("{}", x.slice_mut(2..2)));
assert_eq!("[4]", format!("{}", x.slice_mut(3..4)));

// Elements use the Debug format: 1.0 prints as "1.0", not "1".
let mut y = vec![1.0f64, 2.5, -0.5];
assert_eq!("[1.0, 2.5, -0.5]", format!("{}", y.slice_mut(0..3)));
assert_eq!("[1.0]", format!("{}", y.slice_mut(0..1)));
assert_eq!("[1.00, 2.50]", format!("{:.2}", y.slice_mut(0..2)));
}

#[test]
fn test_display_concrete_views() {
let mut y = vec![1.0f64, 2.5];
assert_eq!("[]", format!("{}", VecView { ptr: &y[..0] }));
assert_eq!("[1.0]", format!("{}", VecView { ptr: &y[..1] }));
assert_eq!("[1.00, 2.50]", format!("{:.2}", VecView { ptr: &y[..] }));
assert_eq!("[]", format!("{}", VecMutView { ptr: &mut y[..0] }));
assert_eq!("[2.5]", format!("{}", VecMutView { ptr: &mut y[1..] }));
assert_eq!(
"[1.00, 2.50]",
format!("{:.2}", VecMutView { ptr: &mut y[..] })
);
}
}
Loading