Skip to content

Commit b3a1915

Browse files
committed
view macro to ease indexing operations using seq, Array or both
assignment operations support is not added yet.
1 parent 3ab7158 commit b3a1915

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/macros.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,65 @@ macro_rules! seq {
165165
$crate::Seq::<i32>::new($start, $end, $step)
166166
};
167167
}
168+
169+
/// Indexing into an existing Array
170+
///
171+
/// This macro call with return an Array that has a view of another Array. The Array returned due to
172+
/// the indexing operation will follow copy-on-write semantics. The Array identifier taken by this
173+
/// macro is passed to the relevant internal functions as a borrowed reference. Thus, this identifier
174+
/// will be still available for futher use after the macro call.
175+
///
176+
/// The following types of inputs are matched by this macro.
177+
///
178+
/// - A simple Array identifier.
179+
/// - An Array with slicing info for indexing.
180+
/// - An Array with slicing info and other arrays used for indexing.
181+
#[macro_export]
182+
macro_rules! view {
183+
(@af_max_dims) => {
184+
4
185+
};
186+
( $array_ident:ident ) => {
187+
$array_ident.clone()
188+
};
189+
( $array_ident:ident [ $($start:literal : $end:literal : $step:literal),+ ] ) => {
190+
{
191+
let AF_MAX_DIMS: usize = view!(@af_max_dims);
192+
let mut seq_vec = Vec::<$crate::Seq<i32>>::with_capacity(AF_MAX_DIMS);
193+
$(
194+
seq_vec.push($crate::seq!($start:$end:$step));
195+
)*
196+
for span_place_holder in seq_vec.len()..AF_MAX_DIMS {
197+
seq_vec.push($crate::seq!());
198+
}
199+
$crate::index(&$array_ident, &seq_vec)
200+
}
201+
};
202+
(@set_indexer $idim:expr, $idxr:ident, $lterm:expr) => {
203+
{
204+
$idxr.set_index(&$lterm, $idim, None);
205+
}
206+
};
207+
(@set_indexer $idim:expr, $idxr:ident, $hterm:expr, $($tterm:expr),*) => {
208+
{
209+
$idxr.set_index(&$hterm, $idim, None);
210+
view!(@set_indexer $idim + 1, $idxr, $($tterm),*);
211+
}
212+
};
213+
($array_ident:ident [ $($_e:expr),+ ]) => {
214+
{
215+
let AF_MAX_DIMS: u32 = view!(@af_max_dims);
216+
let span = $crate::seq!();
217+
let mut idxrs = $crate::Indexer::default();
218+
219+
view!(@set_indexer 0, idxrs, $($_e),*);
220+
221+
let mut dimIx = idxrs.len() as u32;
222+
while dimIx < AF_MAX_DIMS {
223+
idxrs.set_index(&span, dimIx, None);
224+
dimIx += 1;
225+
}
226+
$crate::index_gen(&$array_ident, idxrs)
227+
}
228+
};
229+
}

tests/index_macro.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use arrayfire::{af_print, randu, seq, view, Array, Dim4};
2+
3+
#[test]
4+
fn array_view() {
5+
let dims = Dim4::new(&[5, 5, 2, 1]);
6+
let a = randu::<f32>(dims);
7+
let b = a.clone();
8+
let c = a.clone();
9+
let d = a.clone();
10+
let e = a.clone();
11+
12+
let v = view!(a);
13+
af_print!("v = a[None]", v);
14+
15+
let m = view!(c[1:3:1, 1:3:2]);
16+
af_print!("m = c[:, :]", m);
17+
18+
let x = seq!(1:3:1);
19+
let y = seq!(1:3:2);
20+
let u = view!(b[x, y]);
21+
af_print!("u = b[seq(), seq()]", u);
22+
23+
let values: [u32; 3] = [1, 2, 3];
24+
let indices = Array::new(&values, Dim4::new(&[3, 1, 1, 1]));
25+
let indices2 = Array::new(&values, Dim4::new(&[3, 1, 1, 1]));
26+
27+
let w = view!(d[indices, indices2]);
28+
af_print!("w = d[Array, Array]", w);
29+
30+
let z = view!(e[indices, y]);
31+
af_print!("z = e[Array, Seq]", z);
32+
}

0 commit comments

Comments
 (0)