Skip to content

Commit 7df47d2

Browse files
authored
Merge pull request #502 from jturner314/refactor-iterators
Refactor and improve iterators
2 parents 408f42b + 0948409 commit 7df47d2

File tree

7 files changed

+370
-369
lines changed

7 files changed

+370
-369
lines changed

src/impl_methods.rs

+15-23
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 {
@@ -676,7 +668,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
676668
pub fn genrows(&self) -> Lanes<A, D::Smaller> {
677669
let mut n = self.ndim();
678670
if n == 0 { n += 1; }
679-
new_lanes(self.view(), Axis(n - 1))
671+
Lanes::new(self.view(), Axis(n - 1))
680672
}
681673

682674
/// Return a producer and iterable that traverses over the *generalized*
@@ -688,7 +680,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
688680
{
689681
let mut n = self.ndim();
690682
if n == 0 { n += 1; }
691-
new_lanes_mut(self.view_mut(), Axis(n - 1))
683+
LanesMut::new(self.view_mut(), Axis(n - 1))
692684
}
693685

694686
/// Return a producer and iterable that traverses over the *generalized*
@@ -718,7 +710,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
718710
/// }
719711
/// ```
720712
pub fn gencolumns(&self) -> Lanes<A, D::Smaller> {
721-
new_lanes(self.view(), Axis(0))
713+
Lanes::new(self.view(), Axis(0))
722714
}
723715

724716
/// Return a producer and iterable that traverses over the *generalized*
@@ -728,7 +720,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
728720
pub fn gencolumns_mut(&mut self) -> LanesMut<A, D::Smaller>
729721
where S: DataMut
730722
{
731-
new_lanes_mut(self.view_mut(), Axis(0))
723+
LanesMut::new(self.view_mut(), Axis(0))
732724
}
733725

734726
/// Return a producer and iterable that traverses over all 1D lanes
@@ -760,7 +752,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
760752
/// assert_eq!(inner2.into_iter().next().unwrap(), aview1(&[0, 1, 2]));
761753
/// ```
762754
pub fn lanes(&self, axis: Axis) -> Lanes<A, D::Smaller> {
763-
new_lanes(self.view(), axis)
755+
Lanes::new(self.view(), axis)
764756
}
765757

766758
/// Return a producer and iterable that traverses over all 1D lanes
@@ -770,7 +762,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
770762
pub fn lanes_mut(&mut self, axis: Axis) -> LanesMut<A, D::Smaller>
771763
where S: DataMut
772764
{
773-
new_lanes_mut(self.view_mut(), axis)
765+
LanesMut::new(self.view_mut(), axis)
774766
}
775767

776768

@@ -819,7 +811,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
819811
pub fn axis_iter(&self, axis: Axis) -> AxisIter<A, D::Smaller>
820812
where D: RemoveAxis,
821813
{
822-
iterators::new_axis_iter(self.view(), axis.index())
814+
AxisIter::new(self.view(), axis)
823815
}
824816

825817

@@ -834,7 +826,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
834826
where S: DataMut,
835827
D: RemoveAxis,
836828
{
837-
iterators::new_axis_iter_mut(self.view_mut(), axis.index())
829+
AxisIterMut::new(self.view_mut(), axis)
838830
}
839831

840832

@@ -865,7 +857,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
865857
/// [[26, 27]]]));
866858
/// ```
867859
pub fn axis_chunks_iter(&self, axis: Axis, size: usize) -> AxisChunksIter<A, D> {
868-
iterators::new_chunk_iter(self.view(), axis.index(), size)
860+
AxisChunksIter::new(self.view(), axis, size)
869861
}
870862

871863
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -878,7 +870,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
878870
-> AxisChunksIterMut<A, D>
879871
where S: DataMut
880872
{
881-
iterators::new_chunk_iter_mut(self.view_mut(), axis.index(), size)
873+
AxisChunksIterMut::new(self.view_mut(), axis, size)
882874
}
883875

884876
/// Return an exact chunks producer (and iterable).
@@ -895,7 +887,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
895887
pub fn exact_chunks<E>(&self, chunk_size: E) -> ExactChunks<A, D>
896888
where E: IntoDimension<Dim=D>,
897889
{
898-
exact_chunks_of(self.view(), chunk_size)
890+
ExactChunks::new(self.view(), chunk_size)
899891
}
900892

901893
/// Return an exact chunks producer (and iterable).
@@ -934,7 +926,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
934926
where E: IntoDimension<Dim=D>,
935927
S: DataMut
936928
{
937-
exact_chunks_mut_of(self.view_mut(), chunk_size)
929+
ExactChunksMut::new(self.view_mut(), chunk_size)
938930
}
939931

940932
/// Return a window producer and iterable.
@@ -954,7 +946,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
954946
pub fn windows<E>(&self, window_size: E) -> Windows<A, D>
955947
where E: IntoDimension<Dim=D>
956948
{
957-
windows(self.view(), window_size)
949+
Windows::new(self.view(), window_size)
958950
}
959951

960952
// Return (length, stride) for diagonal
@@ -1597,8 +1589,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
15971589
// break the arrays up into their inner rows
15981590
let n = self.ndim();
15991591
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)))
1592+
Zip::from(LanesMut::new(self.view_mut(), Axis(n - 1)))
1593+
.and(Lanes::new(rhs.broadcast_assume(dim), Axis(n - 1)))
16021594
.apply(move |s_row, r_row| {
16031595
Zip::from(s_row).and(r_row).apply(|a, b| f(a, b))
16041596
});

src/impl_views.rs

+7-8
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/iterators/chunks.rs

+46-39
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,30 @@ pub struct ExactChunks<'a, A: 'a, D> {
3838
inner_strides: D,
3939
}
4040

41-
/// **Panics** if any chunk dimension is zero<br>
42-
pub fn exact_chunks_of<A, D, E>(mut a: ArrayView<A, D>, chunk: E) -> ExactChunks<A, D>
43-
where D: Dimension,
44-
E: IntoDimension<Dim=D>,
45-
{
46-
let chunk = chunk.into_dimension();
47-
ndassert!(a.ndim() == chunk.ndim(),
48-
concat!("Chunk dimension {} does not match array dimension {} ",
49-
"(with array of shape {:?})"),
50-
chunk.ndim(), a.ndim(), a.shape());
51-
for i in 0..a.ndim() {
52-
a.dim[i] /= chunk[i];
53-
}
54-
let inner_strides = a.raw_strides();
55-
a.strides *= &chunk;
41+
impl<'a, A, D: Dimension> ExactChunks<'a, A, D> {
42+
/// Creates a new exact chunks producer.
43+
///
44+
/// **Panics** if any chunk dimension is zero
45+
pub(crate) fn new<E>(mut a: ArrayView<'a, A, D>, chunk: E) -> Self
46+
where
47+
E: IntoDimension<Dim = D>,
48+
{
49+
let chunk = chunk.into_dimension();
50+
ndassert!(a.ndim() == chunk.ndim(),
51+
concat!("Chunk dimension {} does not match array dimension {} ",
52+
"(with array of shape {:?})"),
53+
chunk.ndim(), a.ndim(), a.shape());
54+
for i in 0..a.ndim() {
55+
a.dim[i] /= chunk[i];
56+
}
57+
let inner_strides = a.raw_strides();
58+
a.strides *= &chunk;
5659

57-
ExactChunks {
58-
base: a,
59-
chunk: chunk,
60-
inner_strides: inner_strides,
60+
ExactChunks {
61+
base: a,
62+
chunk: chunk,
63+
inner_strides: inner_strides,
64+
}
6165
}
6266
}
6367

@@ -117,27 +121,30 @@ pub struct ExactChunksMut<'a, A: 'a, D> {
117121
inner_strides: D,
118122
}
119123

120-
/// **Panics** if any chunk dimension is zero<br>
121-
pub fn exact_chunks_mut_of<A, D, E>(mut a: ArrayViewMut<A, D>, chunk: E)
122-
-> ExactChunksMut<A, D>
123-
where D: Dimension,
124-
E: IntoDimension<Dim=D>,
125-
{
126-
let chunk = chunk.into_dimension();
127-
ndassert!(a.ndim() == chunk.ndim(),
128-
concat!("Chunk dimension {} does not match array dimension {} ",
129-
"(with array of shape {:?})"),
130-
chunk.ndim(), a.ndim(), a.shape());
131-
for i in 0..a.ndim() {
132-
a.dim[i] /= chunk[i];
133-
}
134-
let inner_strides = a.raw_strides();
135-
a.strides *= &chunk;
124+
impl<'a, A, D: Dimension> ExactChunksMut<'a, A, D> {
125+
/// Creates a new exact chunks producer.
126+
///
127+
/// **Panics** if any chunk dimension is zero
128+
pub(crate) fn new<E>(mut a: ArrayViewMut<'a, A, D>, chunk: E) -> Self
129+
where
130+
E: IntoDimension<Dim = D>,
131+
{
132+
let chunk = chunk.into_dimension();
133+
ndassert!(a.ndim() == chunk.ndim(),
134+
concat!("Chunk dimension {} does not match array dimension {} ",
135+
"(with array of shape {:?})"),
136+
chunk.ndim(), a.ndim(), a.shape());
137+
for i in 0..a.ndim() {
138+
a.dim[i] /= chunk[i];
139+
}
140+
let inner_strides = a.raw_strides();
141+
a.strides *= &chunk;
136142

137-
ExactChunksMut {
138-
base: a,
139-
chunk: chunk,
140-
inner_strides: inner_strides,
143+
ExactChunksMut {
144+
base: a,
145+
chunk: chunk,
146+
inner_strides: inner_strides,
147+
}
141148
}
142149
}
143150

0 commit comments

Comments
 (0)