8000 Merge branch 'master' of https://github.com/bluss/ndarray · rust-ndarray/ndarray@8048a41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8048a41

Browse files
committed
Merge branch 'master' of https://github.com/bluss/ndarray
2 parents dbbf47c + b980ab9 commit 8048a41

File tree

9 files changed

+410
-401
lines changed

9 files changed

+410
-401
lines changed

src/dimension/dimension_trait.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
7171
type Smaller: Dimension;
7272
/// Next larger dimension
7373
type Larger: Dimension;
74-
#[doc(hidden)]
74+
75+
/// Returns the number of dimensions (number of axes).
7576
fn ndim(&self) -> usize;
7677

7778
/// Convert the dimension into a pattern matching friendly value.
@@ -150,20 +151,13 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
150151
strides
151152
}
152153

153-
#[doc(hidden)]
154-
// Return an index of same dimensionality
155-
fn zero_index(&self) -> Self {
156-
Self::default()
157-
}
158-
159-
#[doc(hidden)]
160-
/// Return an index of same type and with the specified dimensionality.
154+
/// Creates a dimension of all zeros with the specified ndim.
161155
///
162156
/// This method is useful for generalizing over fixed-size and
163157
/// variable-size dimension representations.
164158
///
165159
/// **Panics** if `Self` has a fixed size that is not `ndim`.
166-
fn zero_index_with_ndim(ndim: usize) -> Self;
160+
fn zeros(ndim: usize) -> Self;
167161

168162
#[doc(hidden)]
169163
#[inline]
@@ -173,11 +167,7 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
173167
return None;
174168
}
175169
}
176-
let mut index = self.clone();
177-
for rr in index.slice_mut().iter_mut() {
178-
*rr = 0;
179-
}
180-
Some(index)
170+
Some(Self::zeros(self.ndim()))
181171
}
182172

183173
#[doc(hidden)]
@@ -380,7 +370,7 @@ impl Dimension for Dim<[Ix; 0]> {
380370
#[inline]
381371
fn into_pattern(self) -> Self::Pattern { }
382372
#[inline]
383-
fn zero_index_with_ndim(ndim: usize) -> Self {
373+
fn zeros(ndim: usize) -> Self {
384374
assert_eq!(ndim, 0);
385375
Self::default()
386376
}
@@ -416,7 +406,7 @@ impl Dimension for Dim<[Ix; 1]> {
416406
get!(&self, 0)
417407
}
418408
#[inline]
419-
fn zero_index_with_ndim(ndim: usize) -> Self {
409+
fn zeros(ndim: usize) -> Self {
420410
assert_eq!(ndim, 1);
421411
Self::default()
422412
}
@@ -510,7 +500,7 @@ impl Dimension for Dim<[Ix; 2]> {
510500
#[inline]
511501
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
512502
#[inline]
513-
fn zero_index_with_ndim(ndim: usize) -> Self {
503+
fn zeros(ndim: usize) -> Self {
514504
assert_eq!(ndim, 2);
515505
Self::default()
516506
}
@@ -655,7 +645,7 @@ impl Dimension for Dim<[Ix; 3]> {
655645
}
656646

657647
#[inline]
658-
fn zero_index_with_ndim(ndim: usize) -> Self {
648+
fn zeros(ndim: usize) -> Self {
659649
assert_eq!(ndim, 3);
660650
Self::default()
661651
}
@@ -764,7 +754,7 @@ macro_rules! large_dim {
764754
#[inline]
765755
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
766756
#[inline]
767-
fn zero_index_with_ndim(ndim: usize) -> Self {
757+
fn zeros(ndim: usize) -> Self {
768758
assert_eq!(ndim, $n);
769759
Self::default()
770760
}
@@ -817,12 +807,7 @@ impl Dimension for IxDyn
817807
}
818808

819809
#[inline]
820-
fn zero_index(&self) -> Self {
821-
IxDyn::zeros(self.ndim())
822-
}
823-
824-
#[inline]
825-
fn zero_index_with_ndim(ndim: usize) -> Self {
810+
fn zeros(ndim: usize) -> Self {
826811
IxDyn::zeros(ndim)
827812
}
828813

src/impl_methods.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@ use imp_prelude::*;
1616

1717
use arraytraits;
1818
use dimension;
19-
use iterators;
2019
use error::{self, ShapeError, ErrorKind};
2120
use dimension::IntoDimension;
2221
use dimension::{abs_index, axes_of, Axes, do_slice, merge_axes, stride_offset};
23-
use iterators::{
24-
new_lanes,
25-
new_lanes_mut,
26-
exact_chunks_of,
27-
exact_chunks_mut_of,
28-
windows
29-
};
3022
use zip::Zip;
3123

3224
use {
@@ -199,6 +191,29 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
199191
}
200192
}
201193

194+
/// Returns a reference to the first element of the array, or `None` if it
195+
/// is empty.
196+
pub fn first(&self) -> Option<&A> {
197+
if self.is_empty() {
198+
None
199+
} else {
200+
Some(unsafe { &*self.as_ptr() })
201+
}
202+
}
203+
204+
/// Returns a mutable reference to the first element of the array, or
205+
/// `None` if it is empty.
206+
pub fn first_mut(&mut self) -> Option<&mut A>
207+
where
208+
S: DataMut,
209+
{
210+
if self.is_empty() {
211+
None
212+
} else {
213+
Some(unsafe { &mut *self.as_mut_ptr() })
214+
}
215+
}
216+
202217
/// Return an iterator of references to the elements of the array.
203218
///
204219
/// Elements are visited in the *logical order* of the array, which
@@ -303,8 +318,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
303318

304319
// Copy the dim and strides that remain after removing the subview axes.
305320
let out_ndim = info.out_ndim();
306-
let mut new_dim = Do::zero_index_with_ndim(out_ndim);
307-
let mut new_strides = Do::zero_index_with_ndim(out_ndim);
321+
let mut new_dim = Do::zeros(out_ndim);
322+
let mut new_strides = Do::zeros(out_ndim);
308323
izip!(self.dim.slice(), self.strides.slice(), indices)
309324
.filter_map(|(d, s, slice_or_index)| match slice_or_index {
310325
&SliceOrIndex::Slice {..} => Some((d, s)),
@@ -676,7 +691,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
676691
pub fn genrows(&self) -> Lanes<A, D::Smaller> {
677692
let mut n = self.ndim();
678693
if n == 0 { n += 1; }
679-
new_lanes(self.view(), Axis(n - 1))
694+
Lanes::new(self.view(), Axis(n - 1))
680695
}
681696

682697
/// Return a producer and iterable that traverses over the *generalized*
@@ -688,7 +703,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
688703
{
689704
let mut n = self.ndim();
690705
if n == 0 { n += 1; }
691-
new_lanes_mut(self.view_mut(), Axis(n - 1))
706+
LanesMut::new(self.view_mut(), Axis(n - 1))
692707
}
693708

694709
/// Return a producer and iterable that traverses over the *generalized*
@@ -718,7 +733,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
718733
/// }
719734
/// ```
720735
pub fn gencolumns(&self) -> Lanes<A, D::Smaller> {
721-
new_lanes(self.view(), Axis(0))
736+
Lanes::new(self.view(), Axis(0))
722737
}
723738

724739
/// Return a producer and iterable that traverses over the *generalized*
@@ -728,7 +743,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
728743
pub fn gencolumns_mut(&mut self) -> LanesMut<A, D::Smaller>
729744
where S: DataMut
730745
{
731-
new_lanes_mut(self.view_mut(), Axis(0))
746+
LanesMut::new(self.view_mut(), Axis(0))
732747
}
733748

734749
/// Return a producer and iterable that traverses over all 1D lanes
@@ -760,7 +775,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
760775
/// assert_eq!(inner2.into_iter().next().unwrap(), aview1(&[0, 1, 2]));
761776
/// ```
762777
pub fn lanes(&self, axis: Axis) -> Lanes<A, D::Smaller> {
763-
new_lanes(self.view(), axis)
778+
Lanes::new(self.view(), axis)
764779
}
765780

766781
/// Return a producer and iterable that traverses over all 1D lanes
@@ -770,7 +785,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
770785
pub fn lanes_mut(&mut self, axis: Axis) -> LanesMut<A, D::Smaller>
771786
where S: DataMut
772787
{
773-
new_lanes_mut(self.view_mut(), axis)
788+
LanesMut::new(self.view_mut(), axis)
774789
}
775790

776791

@@ -819,7 +834,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
819834
pub fn axis_iter(&self, axis: Axis) -> AxisIter<A, D::Smaller>
820835
where D: RemoveAxis,
821836
{
822-
iterators::new_axis_iter(self.view(), axis.index())
837+
AxisIter::new(self.view(), axis)
823838
}
824839

825840

@@ -834,7 +849,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimens 10000 ion
834849
where S: DataMut,
835850
D: RemoveAxis,
836851
{
837-
iterators::new_axis_iter_mut(self.view_mut(), axis.index())
852+
AxisIterMut::new(self.view_mut(), axis)
838853
}
839854

840855

@@ -865,7 +880,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
865880
/// [[26, 27]]]));
866881
/// ```
867882
pub fn axis_chunks_iter(&self, axis: Axis, size: usize) -> AxisChunksIter<A, D> {
868-
iterators::new_chunk_iter(self.view(), axis.index(), size)
883+
AxisChunksIter::new(self.view(), axis, size)
869884
}
870885

871886
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -878,7 +893,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
878893
-> AxisChunksIterMut<A, D>
879894
where S: DataMut
880895
{
881-
iterators::new_chunk_iter_mut(self.view_mut(), axis.index(), size)
896+
AxisChunksIterMut::new(self.view_mut(), axis, size)
882897
}
883898

884899
/// Return an exact chunks producer (and iterable).
@@ -895,7 +910,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
895910
pub fn exact_chunks<E>(&self, chunk_size: E) -> ExactChunks<A, D>
896911
where E: IntoDimension<Dim=D>,
897912
{
898-
exact_chunks_of(self.view(), chunk_size)
913+
ExactChunks::new(self.view(), chunk_size)
899914
}
900915

901916
/// Return an exact chunks producer (and iterable).
@@ -934,7 +949,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
934949
where E: IntoDimension<Dim=D>,
935950
S: DataMut
936951
{
937-
exact_chunks_mut_of(self.view_mut(), chunk_size)
952+
ExactChunksMut::new(self.view_mut(), chunk_size)
938953
}
939954

940955
/// Return a window producer and iterable.
@@ -954,7 +969,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
954969
pub fn windows<E>(&self, window_size: E) -> Windows<A, D>
955970
where E: IntoDimension<Dim=D>
956971
{
957-
windows(self.view(), window_size)
972+
Windows::new(self.view(), window_size)
958973
}
959974

960975
// Return (length, stride) for diagonal
@@ -1386,7 +1401,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
13861401
{
13871402
let axes = axes.into_dimension();
13881403
// Ensure that each axis is used exactly once.
1389-
let mut usage_counts = D::zero_index_with_ndim(self.ndim());
1404+
let mut usage_counts = D::zeros(self.ndim());
13901405
for axis in axes.slice() {
13911406
usage_counts[*axis] += 1;
13921407
}
@@ -1395,7 +1410,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
13951410
}
13961411
// Determine the new shape and strides.
13971412
let mut new_dim = usage_counts; // reuse to avoid an allocation
1398-
let mut new_strides = D::zero_index_with_ndim(self.ndim());
1413+
let mut new_strides = D::zeros(self.ndim());
13991414
{
14001415
let dim = self.dim.slice();
14011416
let strides = self.strides.slice();
@@ -1597,8 +1612,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
15971612
// break the arrays up into their inner rows
15981613
let n = self.ndim();
15991614
let dim = self.raw_dim();
1600-
Zip::from(new_lanes_mut(self.view_mut(), Axis(n - 1)))
1601-
.and(new_lanes(rhs.broadcast_assume(dim), Axis(n - 1)))
1615+
Zip::from(LanesMut::new(self.view_mut(), Axis(n - 1)))
1616+
.and(Lanes::new(rhs.broadcast_assume(dim), Axis(n - 1)))
16021617
.apply(move |s_row, r_row| {
16031618
Zip::from(s_row).and(r_row).apply(|a, b| f(a, b))
16041619
});

src/impl_views.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use {
2323
Baseiter,
2424
};
2525

26-
use iter;
27-
use iterators;
26+
use iter::{self, AxisIter, AxisIterMut};
2827

2928
/// Methods for read-only array views.
3029
impl<'a, A, D> ArrayView<'a, A, D>
@@ -469,15 +468,15 @@ impl<'a, A, D> ArrayView<'a, A, D>
469468
}
470469

471470
#[inline]
472-
pub(crate) fn into_base_iter(self) -> Baseiter<'a, A, D> {
471+
pub(crate) fn into_base_iter(self) -> Baseiter<A, D> {
473472
unsafe {
474473
Baseiter::new(self.ptr, self.dim, self.strides)
475474
}
476475
}
477476

478477
#[inline]
479478
pub(crate) fn into_elements_base(self) -> ElementsBase<'a, A, D> {
480-
ElementsBase { inner: self.into_base_iter() }
479+
ElementsBase::new(self)
481480
}
482481

483482
pub(crate) fn into_iter_(self) -> Iter<'a, A, D> {
@@ -490,7 +489,7 @@ impl<'a, A, D> ArrayView<'a, A, D>
490489
pub fn into_outer_iter(self) -> iter::AxisIter<'a, A, D::Smaller>
491490
where D: RemoveAxis,
492491
{
493-
iterators::new_outer_iter(self)
492+
AxisIter::new(self, Axis(0))
494493
}
495494

496495
}
@@ -519,15 +518,15 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
519518
}
520519

521520
#[inline]
522-
pub(crate) fn into_base_iter(self) -> Baseiter<'a, A, D> {
521+
pub(crate) fn into_base_iter(self) -> Baseiter<A, D> {
523522
unsafe {
524523
Baseiter::new(self.ptr, self.dim, self.strides)
525524
}
526525
}
527526

528527
#[inline]
529528
pub(crate) fn into_elements_base(self) -> ElementsBaseMut<'a, A, D> {
530-
ElementsBaseMut { inner: self.into_base_iter() }
529+
ElementsBaseMut::new(self)
531530
}
532531

533532
pub(crate) fn into_slice_(self) -> Result<&'a mut [A], Self> {
@@ -550,7 +549,7 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
550549
pub fn into_outer_iter(self) -> iter::AxisIterMut<'a, A, D::Smaller>
551550
where D: RemoveAxis,
552551
{
553-
iterators::new_outer_iter_mut(self)
552+
AxisIterMut::new(self, Axis(0))
554553
}
555554
}
556555

src/indexes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn indices<E>(shape: E) -> Indices<E::Dim>
3131
{
3232
let dim = shape.into_dimension();
3333
Indices {
34-
start: dim.zero_index(),
34+
start: E::Dim::zeros(dim.ndim()),
3535
dim: dim,
3636
}
3737
}
@@ -210,7 +210,7 @@ pub fn indices_iter_f<E>(shape: E) -> IndicesIterF<E::Dim>
210210
where E: IntoDimension,
211211
{
212212
let dim = shape.into_dimension();
213-
let zero = dim.zero_index();
213+
let zero = E::Dim::zeros(dim.ndim());
214214
IndicesIterF {
215215
has_remaining: dim.size_checked() != Some(0),
216216
index: zero,

0 commit comments

Comments
 (0)
0