Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic storage #3

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion examples/matmul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ndarray::Array;

fn main()
{
let mat = Array::range(0.0f32, 16.0).reshape((2, 4, 2));
let mat = Array::range(0.0f32, 16.0).reshape_clone((2, 4, 2));
println!("{a:?}\n times \n{b:?}\nis equal to:\n{c:?}",
a=mat.subview(2,1),
b=mat.subview(0,1),
Expand Down
20 changes: 13 additions & 7 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::fmt;
use super::{Array, Dimension};
use std::ops::Deref;

/// HACK: fmt::rt::FlagAlternate has been hidden away
const FLAG_ALTERNATE: usize = 2;

fn format_array<A, D: Dimension, F>(view: &Array<A, D>, f: &mut fmt::Formatter,
mut format: F) -> fmt::Result where
F: FnMut(&mut fmt::Formatter, &A) -> fmt::Result,
fn format_array<A, S, D: Dimension, F>(view: &Array<A, S, D>,
f: &mut fmt::Formatter,
mut format: F) -> fmt::Result where
F: FnMut(&mut fmt::Formatter, &A) -> fmt::Result, S: Deref<Target=[A]>
{
let ndim = view.dim.slice().len();
/* private nowadays
Expand Down Expand Up @@ -71,7 +73,8 @@ fn format_array<A, D: Dimension, F>(view: &Array<A, D>, f: &mut fmt::Formatter,
}

// NOTE: We can impl other fmt traits here
impl<'a, A: fmt::Display, D: Dimension> fmt::Display for Array<A, D>
impl<'a, A: fmt::Display, S, D: Dimension> fmt::Display for Array<A, S, D>
where S: Deref<Target=[A]>
{
/// Format the array using `Display` and apply the formatting parameters used
/// to each element.
Expand All @@ -83,7 +86,8 @@ impl<'a, A: fmt::Display, D: Dimension> fmt::Display for Array<A, D>
}
}

impl<'a, A: fmt::Debug, D: Dimension> fmt::Debug for Array<A, D>
impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for Array<A, S, D>
where S: Deref<Target=[A]>
{
/// Format the array using `Debug` and apply the formatting parameters used
/// to each element.
Expand All @@ -95,7 +99,8 @@ impl<'a, A: fmt::Debug, D: Dimension> fmt::Debug for Array<A, D>
}
}

impl<'a, A: fmt::LowerExp, D: Dimension> fmt::LowerExp for Array<A, D>
impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for Array<A, S, D>
where S: Deref<Target=[A]>
{
/// Format the array using `LowerExp` and apply the formatting parameters used
/// to each element.
Expand All @@ -107,7 +112,8 @@ impl<'a, A: fmt::LowerExp, D: Dimension> fmt::LowerExp for Array<A, D>
}
}

impl<'a, A: fmt::UpperExp, D: Dimension> fmt::UpperExp for Array<A, D>
impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for Array<A, S, D>
where S: Deref<Target=[A]>
{
/// Format the array using `UpperExp` and apply the formatting parameters used
/// to each element.
Expand Down
48 changes: 29 additions & 19 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use std::iter::IntoIterator;
use std::ops::{
Index,
IndexMut,
Deref,
DerefMut
};

use super::{Array, Dimension, Ix, Elements, ElementsMut};

impl<'a, A, D: Dimension> Index<D> for Array<A, D>
impl<'a, A, S, D: Dimension> Index<D> for Array<A, S, D>
where S: Deref<Target=[A]>
{
type Output = A;
#[inline]
Expand All @@ -23,7 +26,8 @@ impl<'a, A, D: Dimension> Index<D> for Array<A, D>
}
}

impl<'a, A: Clone, D: Dimension> IndexMut<D> for Array<A, D>
impl<'a, A: Clone, S, D: Dimension> IndexMut<D> for Array<A, S, D>
where S: DerefMut<Target=[A]>
{
#[inline]
/// Access the element at **index** mutably.
Expand All @@ -35,31 +39,34 @@ impl<'a, A: Clone, D: Dimension> IndexMut<D> for Array<A, D>
}


impl<A: PartialEq, D: Dimension>
PartialEq for Array<A, D>
impl<A: PartialEq, S, D: Dimension>
PartialEq for Array<A, S, D>
where S: Deref<Target=[A]>
{
/// Return `true` if the array shapes and all elements of `self` and
/// `other` are equal. Return `false` otherwise.
fn eq(&self, other: &Array<A, D>) -> bool
fn eq(&self, other: &Array<A, S, D>) -> bool
{
self.shape() == other.shape() &&
self.iter().zip(other.iter()).all(|(a, b)| a == b)
}
}

impl<A: Eq, D: Dimension>
Eq for Array<A, D> {}
impl<A: Eq, S, D: Dimension>
Eq for Array<A, S, D>
where S: Deref<Target=[A]>
{}

impl<A> FromIterator<A> for Array<A, Ix>
impl<A> FromIterator<A> for Array<A, Vec<A>, Ix>
{
fn from_iter<I: IntoIterator<Item=A>>(it: I) -> Array<A, Ix>
fn from_iter<I: IntoIterator<Item=A>>(it: I) -> Array<A, Vec<A>, Ix>
{
Array::from_iter(it.into_iter())
}
}

impl<'a, A, D> IntoIterator for &'a Array<A, D> where
D: Dimension,
impl<'a, A, S, D> IntoIterator for &'a Array<A, S, D> where
D: Dimension, S: Deref<Target=[A]>
{
type Item = &'a A;
type IntoIter = Elements<'a, A, D>;
Expand All @@ -70,9 +77,10 @@ impl<'a, A, D> IntoIterator for &'a Array<A, D> where
}
}

impl<'a, A, D> IntoIterator for &'a mut Array<A, D> where
impl<'a, A, S, D> IntoIterator for &'a mut Array<A, S, D> where
A: Clone,
D: Dimension,
S: Deref<Target=[A]>,
{
type Item = &'a mut A;
type IntoIter = ElementsMut<'a, A, D>;
Expand All @@ -83,10 +91,10 @@ impl<'a, A, D> IntoIterator for &'a mut Array<A, D> where
}
}

impl<A: hash::Hash, D: Dimension>
hash::Hash for Array<A, D>
impl<A: hash::Hash, S: Deref<Target=[A]>, D: Dimension>
hash::Hash for Array<A, S, D>
{
fn hash<S: hash::Hasher>(&self, state: &mut S)
fn hash<H: hash::Hasher>(&self, state: &mut H)
{
self.shape().hash(state);
for elt in self.iter() {
Expand All @@ -100,7 +108,8 @@ hash::Hash for Array<A, D>
static ARRAY_FORMAT_VERSION: u8 = 1u8;

#[cfg(feature = "rustc-serialize")]
impl<A: Encodable, D: Dimension + Encodable> Encodable for Array<A, D>
impl<A: Encodable, S, D: Dimension + Encodable> Encodable for Array<A, S, D>
where S: Deref<Target=[A]>
{
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>
{
Expand Down Expand Up @@ -129,10 +138,11 @@ impl<A: Encodable, D: Dimension + Encodable> Encodable for Array<A, D>
}

#[cfg(feature = "rustc-serialize")]
impl<A: Decodable, D: Dimension + Decodable>
Decodable for Array<A, D>
impl<A: Decodable, S, D: Dimension + Decodable>
Decodable for Array<A, S, D>
where S: Deref<Target=[A]>
{
fn decode<S: Decoder>(d: &mut S) -> Result<Array<A, D>, S::Error>
fn decode<Dec: Decoder>(d: &mut Dec) -> Result<Array<A, D>, Dec::Error>
{
d.read_struct("Array", 3, |d| {
let version: u8 = try!(d.read_struct_field("v", 0, Decodable::decode));
Expand Down
Loading