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

Introduce ArrayBase and provide both Array, OwnedArray, ArrayView and ArrayViewMut #8

Merged
merged 43 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5eba746
Convert Array into ArrayBase with pluggable storage type
bluss Dec 14, 2015
cc19ded
Rename trait to ArrayMut
bluss Dec 14, 2015
b4bd888
Update docs
bluss Dec 14, 2015
7401983
Update ArrayBase::map
bluss Dec 14, 2015
6ccc90e
Make ArrayBase::assign generic
bluss Dec 14, 2015
cce7ff6
Update Storage / StorageMut to be unsafe traits
bluss Dec 14, 2015
10d0f63
Generalize PartialEq
bluss Dec 14, 2015
ac1b457
Update IntoIterator, Hash
bluss Dec 14, 2015
d442bed
Generalize Index, IndexMut and Debug
bluss Dec 14, 2015
7bdc14a
Generalize mat_mul slightly
bluss Dec 15, 2015
1e7400c
Generalize iops
bluss Dec 15, 2015
88a90b6
Update arithmetic operations
bluss Dec 15, 2015
fe027cf
Adjust arithmetic ops impls
bluss Dec 15, 2015
46848ce
Add .view() and .view_mut()
bluss Dec 15, 2015
dd34139
Port assign ops to be generic
bluss Dec 15, 2015
9eef2ed
Fix doc for views
bluss Dec 15, 2015
8983e3f
Add test for view().diag()
bluss Dec 15, 2015
e78ca91
Impl Sync and Send for ArrayBase
bluss Dec 15, 2015
c1de0a5
Add broadcast() -> ArrayView method
bluss Dec 15, 2015
1515ceb
Merge ArrayMut and StorageMut traits
bluss Dec 15, 2015
4f0a8a7
impl IntoIterator for the array views
bluss Dec 15, 2015
43867d4
Update docs
bluss Dec 15, 2015
4bb1ba9
Update docs
bluss Dec 15, 2015
9491c87
Make Shared require Storage
bluss Dec 15, 2015
e7f91e7
Add aview1 and aview2 to create array views
bluss Dec 15, 2015
07f121c
Add StorageClone to fix Clone for ArrayBase
bluss Dec 15, 2015
4234b7f
Add ArrayBase::to_owned and make .sum() and .mean() generic
bluss Dec 15, 2015
11f3889
Fix bug in assign_scalar
bluss Dec 15, 2015
959f2f4
Edit docs
bluss Dec 15, 2015
668662b
Rename all Storage traits to Data.*
bluss Dec 15, 2015
9fe4dc7
Mark DataOwned unsafe too
bluss Dec 15, 2015
8a9be9f
Add .subview_mut()
bluss Dec 15, 2015
f550edc
Add aview0 and adjust docs
bluss Dec 15, 2015
e2935bd
Add .slice_mut()
bluss Dec 15, 2015
a22edc0
Remove explicit lifetime parameters where lifetime elision is already…
bluss Dec 15, 2015
01c7be1
Change .map() to return an OwnedArray
bluss Dec 15, 2015
4f12340
Add .to_shared() and .into_shared()
bluss Dec 15, 2015
518a888
Add .get() and .get_mut() and deprecate .at(), .at_mut()
bluss Dec 15, 2015
348993c
Edit docs for all arithmetic operators
bluss Dec 15, 2015
875ff8d
Use backticks (code) markup where applicable
bluss Dec 15, 2015
399e997
Use argument name `rhs` instead of `other`
bluss Dec 15, 2015
ed2eaf7
Generalize .allclose()
bluss Dec 15, 2015
5842355
Generalize Not and Neg
bluss Dec 15, 2015
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
15 changes: 11 additions & 4 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use std::fmt;
use super::{Array, Dimension};
use super::{
ArrayBase,
Data,
};

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, F>(view: &ArrayBase<S, D>, f: &mut fmt::Formatter,
mut format: F) -> fmt::Result
where F: FnMut(&mut fmt::Formatter, &A) -> fmt::Result,
D: Dimension,
S: Data<Elem=A>,
{
let ndim = view.dim.slice().len();
/* private nowadays
Expand Down Expand Up @@ -80,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 ArrayBase<S, D>
where S: Data<Elem=A>,
{
/// Format the array using `Debug` and apply the formatting parameters used
/// to each element.
Expand Down
123 changes: 90 additions & 33 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,63 @@ use std::ops::{
IndexMut,
};

use super::{Array, Dimension, Ix, Elements, ElementsMut};
use super::{
Array, Dimension, Ix, Elements, ElementsMut,
ArrayBase,
ArrayView,
ArrayViewMut,
Data,
DataMut,
};

impl<'a, A, D: Dimension> Index<D> for Array<A, D>
/// Access the element at **index**.
///
/// **Panics** if index is out of bounds.
impl<S, D> Index<D> for ArrayBase<S, D>
where D: Dimension,
S: Data,
{
type Output = A;
type Output = S::Elem;
#[inline]
/// Access the element at **index**.
///
/// **Panics** if index is out of bounds.
fn index(&self, index: D) -> &A {
self.at(index).expect("Array::index: out of bounds")
fn index(&self, index: D) -> &S::Elem {
self.get(index).expect("Array::index: out of bounds")
}
}

impl<'a, A: Clone, D: Dimension> IndexMut<D> for Array<A, D>
/// Access the element at **index** mutably.
///
/// **Panics** if index is out of bounds.
impl<S, D> IndexMut<D> for ArrayBase<S, D>
where D: Dimension,
S: DataMut,
{
#[inline]
/// Access the element at **index** mutably.
///
/// **Panics** if index is out of bounds.
fn index_mut(&mut self, index: D) -> &mut A {
self.at_mut(index).expect("Array::index_mut: out of bounds")
fn index_mut(&mut self, index: D) -> &mut S::Elem {
self.get_mut(index).expect("Array::index_mut: out of bounds")
}
}


impl<A: PartialEq, D: Dimension>
PartialEq for Array<A, D>
impl<S, S2, D> PartialEq<ArrayBase<S2, D>> for ArrayBase<S, D>
where D: Dimension,
S: Data,
S2: Data<Elem = S::Elem>,
S::Elem: PartialEq,
{
/// 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: &ArrayBase<S2, 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<S, D> Eq for ArrayBase<S, D>
where D: Dimension,
S: Data,
S::Elem: Eq,
{ }

impl<A> FromIterator<A> for Array<A, Ix>
{
Expand All @@ -58,35 +75,59 @@ impl<A> FromIterator<A> for Array<A, Ix>
}
}

impl<'a, A, D> IntoIterator for &'a Array<A, D> where
D: Dimension,
impl<'a, S, D> IntoIterator for &'a ArrayBase<S, D>
where D: Dimension,
S: Data,
{
type Item = &'a A;
type IntoIter = Elements<'a, A, D>;
type Item = &'a S::Elem;
type IntoIter = Elements<'a, S::Elem, D>;

fn into_iter(self) -> Self::IntoIter
{
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, A, D> IntoIterator for &'a mut Array<A, D> where
A: Clone,
D: Dimension,
impl<'a, S, D> IntoIterator for &'a mut ArrayBase<S, D>
where D: Dimension,
S: DataMut,
{
type Item = &'a mut A;
type IntoIter = ElementsMut<'a, A, D>;
type Item = &'a mut S::Elem;
type IntoIter = ElementsMut<'a, S::Elem, D>;

fn into_iter(self) -> Self::IntoIter
{
self.iter_mut()
}
}

impl<A: hash::Hash, D: Dimension>
hash::Hash for Array<A, D>
impl<'a, A, D> IntoIterator for ArrayView<'a, A, D>
where D: Dimension,
{
fn hash<S: hash::Hasher>(&self, state: &mut S)
type Item = &'a A;
type IntoIter = Elements<'a, A, D>;

fn into_iter(self) -> Self::IntoIter {
self.into_iter_()
}
}

impl<'a, A, D> IntoIterator for ArrayViewMut<'a, A, D>
where D: Dimension,
{
type Item = &'a mut A;
type IntoIter = ElementsMut<'a, A, D>;

fn into_iter(self) -> Self::IntoIter {
self.into_iter_()
}
}

impl<'a, S, D> hash::Hash for ArrayBase<S, D>
where D: Dimension,
S: Data,
S::Elem: hash::Hash,
{
fn hash<H: hash::Hasher>(&self, state: &mut H)
{
self.shape().hash(state);
for elt in self.iter() {
Expand All @@ -95,6 +136,22 @@ hash::Hash for Array<A, D>
}
}

// NOTE: ArrayBase keeps an internal raw pointer that always
// points into the storage. This is Sync & Send as long as we
// follow the usual inherited mutability rules, as we do with
// Vec, &[] and &mut []

/// `ArrayBase` is `Sync` when the storage type is.
unsafe impl<S, D> Sync for ArrayBase<S, D>
where S: Sync + Data, D: Sync
{ }

/// `ArrayBase` is `Send` when the storage type is.
unsafe impl<S, D> Send for ArrayBase<S, D>
where S: Send + Data, D: Send
{ }


#[cfg(feature = "rustc-serialize")]
// Use version number so we can add a packed format later.
static ARRAY_FORMAT_VERSION: u8 = 1u8;
Expand Down
Loading