Skip to content

Commit 3ab7158

Browse files
committed
Add dim4 and seq macros for ease of creating the associated objects
1 parent 1154739 commit 3ab7158

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/macros.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,38 @@ macro_rules! eval {
130130
}
131131
};
132132
}
133+
134+
/// Create a dim4 object from provided dimensions
135+
///
136+
/// The user can pass 1 or more sizes and the left over values will default to 1.
137+
#[macro_export]
138+
macro_rules! dim4 {
139+
($dim0:literal) => {
140+
$crate::Dim4::new(&[$dim0, 1, 1, 1])
141+
};
142+
($dim0:literal, $dim1:literal) => {
143+
$crate::Dim4::new(&[$dim0, $dim1, 1, 1])
144+
};
145+
($dim0:literal, $dim1:literal, $dim2:literal) => {
146+
$crate::Dim4::new(&[$dim0, $dim1, $dim2, 1])
147+
};
148+
($dim0:literal, $dim1:literal, $dim2:literal, $dim3:literal) => {
149+
$crate::Dim4::new(&[$dim0, $dim1, $dim2, $dim3])
150+
};
151+
}
152+
153+
/// Create a sequence object
154+
///
155+
/// If type is not provided, then the Seq will default to i32 type
156+
#[macro_export]
157+
macro_rules! seq {
158+
() => {
159+
$crate::Seq::<i32>::default()
160+
};
161+
($sty:ty; $start:literal : $end:literal : $step:literal) => {
162+
$crate::Seq::<$sty>::new($start, $end, $step)
163+
};
164+
($start:literal : $end:literal : $step:literal) => {
165+
$crate::Seq::<i32>::new($start, $end, $step)
166+
};
167+
}

tests/seq_macro.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use arrayfire::{af_print, dim4, index, randu, seq};
2+
3+
#[test]
4+
fn array_view() {
5+
let _dim1d = dim4!(2);
6+
let _dim2d = dim4!(2, 3);
7+
let _dim3d = dim4!(2, 3, 4);
8+
9+
let mut dim4d = dim4!(5, 3, 2, 1);
10+
dim4d[2] = 1;
11+
12+
let a = randu::<f32>(dim4d);
13+
let seqs = &[seq!(1:3:1), seq!()];
14+
let sub = index(&a, seqs);
15+
af_print!("A", a);
16+
af_print!("Indexed A", sub);
17+
}

0 commit comments

Comments
 (0)