Skip to content

Commit a08b1fd

Browse files
committed
Merge pull request #93 from bluss/no-arrayviewmut-arithmetic
Arithmetic operations B @ A are now only implemented for owned storage `B`
2 parents 2da5bc7 + 7c337ca commit a08b1fd

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

benches/bench1.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ fn add_2d_regular(bench: &mut test::Bencher)
266266
let b = OwnedArray::<i32, _>::zeros((64, 64));
267267
let bv = b.view();
268268
bench.iter(|| {
269-
let _x = black_box(a.view_mut() + bv);
269+
a.iadd(&bv);
270270
});
271271
}
272272

@@ -292,7 +292,7 @@ fn add_2d_cutout(bench: &mut test::Bencher)
292292
let b = OwnedArray::<i32, _>::zeros((64, 64));
293293
let bv = b.view();
294294
bench.iter(|| {
295-
let _x = black_box(acut.view_mut() + bv);
295+
acut.iadd(&bv);
296296
});
297297
}
298298

@@ -303,7 +303,7 @@ fn add_2d_broadcast_1_to_2(bench: &mut test::Bencher)
303303
let b = OwnedArray::<i32, _>::zeros(64);
304304
let bv = b.view();
305305
bench.iter(|| {
306-
let _x = black_box(a.view_mut() + bv);
306+
a.iadd(&bv);
307307
});
308308
}
309309

@@ -314,7 +314,7 @@ fn add_2d_broadcast_0_to_2(bench: &mut test::Bencher)
314314
let b = OwnedArray::<i32, _>::zeros(());
315315
let bv = b.view();
316316
bench.iter(|| {
317-
let _x = black_box(a.view_mut() + bv);
317+
a.iadd(&bv);
318318
});
319319
}
320320

@@ -337,7 +337,7 @@ fn add_2d_transposed(bench: &mut test::Bencher)
337337
let b = OwnedArray::<i32, _>::zeros((64, 64));
338338
let bv = b.view();
339339
bench.iter(|| {
340-
let _x = black_box(a.view_mut() + bv);
340+
a.iadd(&bv);
341341
});
342342
}
343343

@@ -348,7 +348,7 @@ fn add_2d_f32_regular(bench: &mut test::Bencher)
348348
let b = OwnedArray::<f32, _>::zeros((64, 64));
349349
let bv = b.view();
350350
bench.iter(|| {
351-
let _x = black_box(a.view_mut() + bv);
351+
a.iadd(&bv);
352352
});
353353
}
354354

examples/life.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ fn iterate(z: &mut Board, scratch: &mut Board) {
4040
// compute number of neighbors
4141
let mut neigh = scratch.view_mut();
4242
neigh.assign_scalar(&0);
43-
let neigh = neigh
44-
+ z.slice(s![0..-2, 0..-2])
45-
+ z.slice(s![0..-2, 1..-1])
46-
+ z.slice(s![0..-2, 2.. ])
43+
neigh.iadd(&z.slice(s![0..-2, 0..-2]));
44+
neigh.iadd(&z.slice(s![0..-2, 1..-1]));
45+
neigh.iadd(&z.slice(s![0..-2, 2.. ]));
4746

48-
+ z.slice(s![1..-1, 0..-2])
49-
+ z.slice(s![1..-1, 2.. ])
47+
neigh.iadd(&z.slice(s![1..-1, 0..-2]));
48+
neigh.iadd(&z.slice(s![1..-1, 2.. ]));
5049

51-
+ z.slice(s![2.. , 0..-2])
52-
+ z.slice(s![2.. , 1..-1])
53-
+ z.slice(s![2.. , 2.. ]);
50+
neigh.iadd(&z.slice(s![2.. , 0..-2]));
51+
neigh.iadd(&z.slice(s![2.. , 1..-1]));
52+
neigh.iadd(&z.slice(s![2.. , 2.. ]));
5453

5554
// birth where n = 3 and z[i] = 0,
5655
// survive where n = 2 || n = 3 and z[i] = 1

src/arraytraits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ unsafe impl<S, D> Send for ArrayBase<S, D>
185185
// Use version number so we can add a packed format later.
186186
static ARRAY_FORMAT_VERSION: u8 = 1u8;
187187

188-
/// **Requires `feature = "rustc-serialize"`**
188+
/// **Requires crate feature `"rustc-serialize"`**
189189
#[cfg(feature = "rustc-serialize")]
190190
impl<A, S, D> Encodable for ArrayBase<S, D>
191191
where A: Encodable,
@@ -212,7 +212,7 @@ impl<A, S, D> Encodable for ArrayBase<S, D>
212212
}
213213
}
214214

215-
/// **Requires `feature = "rustc-serialize"`**
215+
/// **Requires crate feature `"rustc-serialize"`**
216216
#[cfg(feature = "rustc-serialize")]
217217
impl<A, S, D> Decodable for ArrayBase<S, D>
218218
where A: Decodable,

src/blas.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Experimental BLAS (Basic Linear Algebra Subprograms) integration
22
//!
3-
//! ***Requires `features = "rblas"`***
3+
//! ***Requires crate feature `"rblas"`***
44
//!
55
//! Depends on crate [`rblas`], ([docs]).
66
//!
@@ -70,7 +70,7 @@ use super::{
7070
};
7171

7272

73-
/// ***Requires `features = "rblas"`***
73+
/// ***Requires crate feature `"rblas"`***
7474
pub struct BlasArrayView<'a, A: 'a, D>(ArrayView<'a, A, D>);
7575
impl<'a, A, D: Copy> Copy for BlasArrayView<'a, A, D> { }
7676
impl<'a, A, D: Clone> Clone for BlasArrayView<'a, A, D> {
@@ -79,7 +79,7 @@ impl<'a, A, D: Clone> Clone for BlasArrayView<'a, A, D> {
7979
}
8080
}
8181

82-
/// ***Requires `features = "rblas"`***
82+
/// ***Requires crate feature `"rblas"`***
8383
pub struct BlasArrayViewMut<'a, A: 'a, D>(ArrayViewMut<'a, A, D>);
8484

8585
impl<S, D> ArrayBase<S, D>
@@ -137,7 +137,7 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
137137
/// Note that `blas` suppors four different element types: `f32`, `f64`,
138138
/// `Complex<f32>`, and `Complex<f64>`.
139139
///
140-
/// ***Requires `features = "rblas"`***
140+
/// ***Requires crate feature `"rblas"`***
141141
pub trait AsBlas<A, S, D> {
142142
/// Return an array view implementing Vector (1D) or Matrix (2D)
143143
/// traits.
@@ -222,7 +222,7 @@ pub trait AsBlas<A, S, D> {
222222
*/
223223
}
224224

225-
/// ***Requires `features = "rblas"`***
225+
/// ***Requires crate feature `"rblas"`***
226226
impl<A, S, D> AsBlas<A, S, D> for ArrayBase<S, D>
227227
where S: Data<Elem=A>,
228228
D: Dimension,

src/lib.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
//!
3939
//! ## Crate Feature Flags
4040
//!
41+
//! The following crate feature flags are available. The are specified in
42+
//! `Cargo.toml`.
43+
//!
4144
//! - `assign_ops`
4245
//! - Optional, requires nightly
4346
//! - Enables the compound assignment operators
@@ -141,6 +144,16 @@ pub type Ixs = isize;
141144
/// [`ArrayView`]: type.ArrayView.html
142145
/// [`ArrayViewMut`]: type.ArrayViewMut.html
143146
///
147+
/// ## Contents
148+
///
149+
/// + [OwnedArray and RcArray](#ownedarray-and-rcarray)
150+
/// + [Indexing and Dimension](#indexing-and-dimension)
151+
/// + [Slicing](#slicing)
152+
/// + [Subviews](#subviews)
153+
/// + [Arithmetic Operations](#arithmetic-operations)
154+
/// + [Broadcasting](#broadcasting)
155+
/// + [Methods](#methods)
156+
///
144157
/// ## `OwnedArray` and `RcArray`
145158
///
146159
/// `OwnedArray` owns the underlying array elements directly (just like
@@ -296,25 +309,27 @@ pub type Ixs = isize;
296309
///
297310
/// Since the trait implementations are hard to overview, here is a summary.
298311
///
299-
/// Let `A` be an array or view of any kind. Let `B` be a mutable
300-
/// array (that is, either `OwnedArray`, `RcArray`, or `ArrayViewMut`)
312+
/// Let `A` be an array or view of any kind. Let `B` be an array
313+
/// with owned storage (either `OwnedArray` or `RcArray`).
314+
/// Let `C` be an array with mutable data (either `OwnedArray`, `RcArray`
315+
/// or `ArrayViewMut`).
301316
/// The following combinations of operands
302317
/// are supported for an arbitrary binary operator denoted by `@`.
303318
///
304319
/// - `&A @ &A` which produces a new `OwnedArray`
305320
/// - `B @ A` which consumes `B`, updates it with the result, and returns it
306321
/// - `B @ &A` which consumes `B`, updates it with the result, and returns it
307-
/// - `B @= &A` which performs an arithmetic operation in place
308-
/// (requires `features = "assign_ops"`)
322+
/// - `C @= &A` which performs an arithmetic operation in place
323+
/// (requires crate feature `"assign_ops"`)
309324
///
310325
/// The trait [`Scalar`](trait.Scalar.html) marks types that can be used in arithmetic
311326
/// with arrays directly. For a scalar `K` the following combinations of operands
312327
/// are supported (scalar can be on either side).
313328
///
314329
/// - `&A @ K` or `K @ &A` which produces a new `OwnedArray`
315330
/// - `B @ K` or `K @ B` which consumes `B`, updates it with the result and returns it
316-
/// - `B @= K` which performs an arithmetic operation in place
317-
/// (requires `features = "assign_ops"`)
331+
/// - `C @= K` which performs an arithmetic operation in place
332+
/// (requires crate feature `"assign_ops"`)
318333
///
319334
/// ## Broadcasting
320335
///
@@ -2549,12 +2564,14 @@ macro_rules! impl_binary_op(
25492564
/// between `self` and `rhs`,
25502565
/// and return the result (based on `self`).
25512566
///
2567+
/// `self` must be an `OwnedArray` or `RcArray`.
2568+
///
25522569
/// If their shapes disagree, `rhs` is broadcast to the shape of `self`.
25532570
///
25542571
/// **Panics** if broadcasting isn’t possible.
25552572
impl<A, S, S2, D, E> $trt<ArrayBase<S2, E>> for ArrayBase<S, D>
25562573
where A: Clone + $trt<A, Output=A>,
2557-
S: DataMut<Elem=A>,
2574+
S: DataOwned<Elem=A> + DataMut,
25582575
S2: Data<Elem=A>,
25592576
D: Dimension,
25602577
E: Dimension,
@@ -2616,9 +2633,11 @@ impl<'a, A, S, S2, D, E> $trt<&'a ArrayBase<S2, E>> for &'a ArrayBase<S, D>
26162633
#[doc=$doc]
26172634
/// between `self` and the scalar `x`,
26182635
/// and return the result (based on `self`).
2636+
///
2637+
/// `self` must be an `OwnedArray` or `RcArray`.
26192638
impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
26202639
where A: Clone + $trt<B, Output=A>,
2621-
S: DataMut<Elem=A>,
2640+
S: DataOwned<Elem=A> + DataMut,
26222641
D: Dimension,
26232642
B: Clone + Scalar,
26242643
{
@@ -2793,7 +2812,7 @@ mod assign_ops {
27932812
///
27942813
/// **Panics** if broadcasting isn’t possible.
27952814
///
2796-
/// **Requires `feature = "assign_ops"`**
2815+
/// **Requires crate feature `"assign_ops"`**
27972816
impl<'a, A, S, S2, D, E> $trt<&'a ArrayBase<S2, E>> for ArrayBase<S, D>
27982817
where A: Clone + $trt<A>,
27992818
S: DataMut<Elem=A>,
@@ -2809,7 +2828,7 @@ mod assign_ops {
28092828
}
28102829

28112830
#[doc=$doc]
2812-
/// **Requires `feature = "assign_ops"`**
2831+
/// **Requires crate feature `"assign_ops"`**
28132832
impl<A, S, D, B> $trt<B> for ArrayBase<S, D>
28142833
where A: $trt<B>,
28152834
S: DataMut<Elem=A>,

0 commit comments

Comments
 (0)