Skip to content

Commit 6953d73

Browse files
authored
Merge pull request #808 from rust-ndarray/zip-minor-cleanups
Small refactoring and improvement around Zip
2 parents 9cba023 + 5481e25 commit 6953d73

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/indexes.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ where
157157
private_impl! {}
158158
}
159159

160+
// How the NdProducer for Indices works.
161+
//
162+
// NdProducer allows for raw pointers (Ptr), strides (Stride) and the produced
163+
// item (Item).
164+
//
165+
// Instead of Ptr, there is `IndexPtr<D>` which is an index value, like [0, 0, 0]
166+
// for the three dimensional case.
167+
//
168+
// The stride is simply which axis is currently being incremented. The stride for axis 1, is 1.
169+
//
170+
// .stride_offset(stride, index) simply computes the new index along that axis, for example:
171+
// [0, 0, 0].stride_offset(1, 10) => [0, 10, 0] axis 1 is incremented by 10.
172+
//
173+
// .as_ref() converts the Ptr value to an Item. For example [0, 10, 0] => (0, 10, 0)
160174
impl<D: Dimension + Copy> NdProducer for Indices<D> {
161175
type Item = D::Pattern;
162176
type Dim = D;

src/zip/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,9 @@ macro_rules! map_impl {
969969
pub fn and<P>(self, p: P) -> Zip<($($p,)* P::Output, ), D>
970970
where P: IntoNdProducer<Dim=D>,
971971
{
972-
let array = p.into_producer();
973-
self.check(&array);
974-
let part_layout = array.layout();
975-
let ($($p,)*) = self.parts;
976-
Zip {
977-
parts: ($($p,)* array, ),
978-
layout: self.layout.and(part_layout),
979-
dimension: self.dimension,
980-
}
972+
let part = p.into_producer();
973+
self.check(&part);
974+
self.build_and(part)
981975
}
982976

983977
/// Include the producer `p` in the Zip, broadcasting if needed.
@@ -990,11 +984,17 @@ macro_rules! map_impl {
990984
where P: IntoNdProducer<Dim=D2, Output=ArrayView<'a, Elem, D2>, Item=&'a Elem>,
991985
D2: Dimension,
992986
{
993-
let array = p.into_producer().broadcast_unwrap(self.dimension.clone());
994-
let part_layout = array.layout();
987+
let part = p.into_producer().broadcast_unwrap(self.dimension.clone());
988+
self.build_and(part)
989+
}
990+
991+
fn build_and<P>(self, part: P) -> Zip<($($p,)* P, ), D>
992+
where P: NdProducer<Dim=D>,
993+
{
994+
let part_layout = part.layout();
995995
let ($($p,)*) = self.parts;
996996
Zip {
997-
parts: ($($p,)* array, ),
997+
parts: ($($p,)* part, ),
998998
layout: self.layout.and(part_layout),
999999
dimension: self.dimension,
10001000
}

0 commit comments

Comments
 (0)