Skip to content

Commit 80651c9

Browse files
committed
Make Seq generic and make use of generic Seq in helloworld example
1 parent 86fc951 commit 80651c9

File tree

4 files changed

+60
-32
lines changed

4 files changed

+60
-32
lines changed

examples/helloworld.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ fn main() {
4242
println!("a + b"); print(&test);
4343

4444
// Index array using sequences
45-
let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
45+
let seqs = &[Seq::new(1u32, 3, 1), Seq::default()];
4646
let sub = index(&a, seqs).unwrap();
4747
println!("a(seq(1,3,1), span)"); print(&sub);
4848

4949
//Index array using array and sequence
50-
let seq4gen = Seq::new(0.0, 2.0, 1.0);
50+
let seq4gen = Seq::new(0u32, 2, 1);
5151

5252
let mut idxrs = match Indexer::new() {
5353
Ok(v) => v,

src/index.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate libc;
33
use array::Array;
44
use defines::AfError;
55
use seq::Seq;
6-
use self::libc::{c_int, c_uint, c_longlong};
6+
use self::libc::{c_double, c_int, c_uint};
77

88
type MutAfIndex = *mut self::libc::c_longlong;
99
type MutAfArray = *mut self::libc::c_longlong;
@@ -15,12 +15,12 @@ type IndexT = self::libc::c_longlong;
1515
extern {
1616
fn af_create_indexers(indexers: MutAfIndex) -> c_int;
1717
fn af_set_array_indexer(indexer: MutAfIndex, idx: AfArray, dim: DimT) -> c_int;
18-
fn af_set_seq_indexer(indexer: MutAfIndex, idx: *const Seq, dim: DimT, is_batch: c_int) -> c_int;
18+
fn af_set_seq_indexer(indexer: MutAfIndex, idx: *const SeqInternal, dim: DimT, is_batch: c_int) -> c_int;
1919
fn af_release_indexers(indexers: MutAfIndex) -> c_int;
2020

21-
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> c_int;
21+
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const SeqInternal) -> c_int;
2222
fn af_lookup(out: MutAfArray, arr: AfArray, indices: AfArray, dim: c_uint) -> c_int;
23-
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const Seq, rhs: AfArray) -> c_int;
23+
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const SeqInternal, rhs: AfArray) -> c_int;
2424
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int;
2525
fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: *const IndexT, rhs: AfArray) -> c_int;
2626
}
@@ -62,11 +62,12 @@ impl Indexable for Array {
6262
///
6363
/// This is used in functions [index_gen](./fn.index_gen.html) and
6464
/// [assign_gen](./fn.assign_gen.html)
65-
impl Indexable for Seq {
65+
impl<T: Copy> Indexable for Seq<T> where c_double: From<T> {
6666
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> {
6767
unsafe {
68-
let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex, self as *const Seq,
69-
dim as DimT, is_batch.unwrap() as c_int);
68+
let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex,
69+
&SeqInternal::from_seq(self) as *const SeqInternal,
70+
dim as DimT, is_batch.unwrap() as c_int);
7071
match err_val {
7172
0 => Ok(()),
7273
_ => Err(AfError::from(err_val)),
@@ -130,12 +131,16 @@ impl Drop for Indexer {
130131
/// println!("a(seq(1, 3, 1), span)");
131132
/// print(&sub);
132133
/// ```
133-
pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
134+
pub fn index<T: Copy>(input: &Array, seqs: &[Seq<T>]) -> Result<Array, AfError>
135+
where c_double: From<T>
136+
{
134137
unsafe {
135138
let mut temp: i64 = 0;
139+
// TODO: allocating a whole new array on the heap just for this is BAD
140+
let seqs: Vec<SeqInternal> = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect();
136141
let err_val = af_index(&mut temp as MutAfArray
137142
, input.get() as AfArray, seqs.len() as u32
138-
, seqs.as_ptr() as *const Seq);
143+
, seqs.as_ptr() as *const SeqInternal);
139144
match err_val {
140145
0 => Ok(Array::from(temp)),
141146
_ => Err(AfError::from(err_val)),
@@ -155,8 +160,8 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
155160
/// ```
156161
#[allow(dead_code)]
157162
pub fn row(input: &Array, row_num: u64) -> Result<Array, AfError> {
158-
index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0)
159-
, Seq::default()])
163+
index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0),
164+
Seq::default()])
160165
}
161166

162167
#[allow(dead_code)]
@@ -300,11 +305,15 @@ pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfE
300305
/// // 1.0 1.0 1.0
301306
/// // 2.0 2.0 2.0
302307
/// ```
303-
pub fn assign_seq(lhs: &Array, seqs: &[Seq], rhs: &Array) -> Result<Array, AfError> {
308+
pub fn assign_seq<T: Copy>(lhs: &Array, seqs: &[Seq<T>], rhs: &Array) -> Result<Array, AfError>
309+
where c_double: From<T>
310+
{
304311
unsafe{
305312
let mut temp: i64 = 0;
313+
// TODO: allocating a whole new array on the heap just for this is BAD
314+
let seqs: Vec<SeqInternal> = seqs.iter().map(|s| SeqInternal::from_seq(s)).collect();
306315
let err_val = af_assign_seq(&mut temp as MutAfArray, lhs.get() as AfArray,
307-
seqs.len() as c_uint, seqs.as_ptr() as *const Seq,
316+
seqs.len() as c_uint, seqs.as_ptr() as *const SeqInternal,
308317
rhs.get() as AfArray);
309318
match err_val {
310319
0 => Ok(Array::from(temp)),
@@ -402,3 +411,20 @@ pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Result<Array,
402411
}
403412
}
404413
}
414+
415+
#[repr(C)]
416+
struct SeqInternal {
417+
begin: c_double,
418+
end: c_double,
419+
step: c_double,
420+
}
421+
422+
impl SeqInternal {
423+
fn from_seq<T: Copy>(s: &Seq<T>) -> Self where c_double: From<T> {
424+
SeqInternal {
425+
begin: From::from(s.begin()),
426+
end: From::from(s.end()),
427+
step: From::from(s.step()),
428+
}
429+
}
430+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(zero_one)]
2+
13
#![doc(html_logo_url = "http://www.arrayfire.com/logos/arrayfire_logo_symbol.png",
24
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
35
html_root_url = "http://arrayfire.com/docs/rust")]

src/seq.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,49 @@ extern crate libc;
22

33
use std::fmt;
44
use std::default::Default;
5-
use self::libc::{c_double};
5+
use std::num::{One, Zero};
66

77
/// Sequences are used for indexing Arrays
88
#[derive(Copy, Clone)]
99
#[repr(C)]
10-
pub struct Seq {
11-
begin: c_double,
12-
end: c_double,
13-
step: c_double,
10+
pub struct Seq<T> {
11+
begin: T,
12+
end: T,
13+
step: T,
1414
}
1515

1616
/// Default `Seq` spans all the elements along a dimension
17-
impl Default for Seq {
18-
fn default() -> Seq {
19-
Seq { begin: 1.0, end: 1.0, step: 0.0, }
17+
impl<T: One+Zero> Default for Seq<T> {
18+
fn default() -> Self {
19+
Seq { begin: One::one(), end: One::one(), step: Zero::zero() }
2020
}
2121
}
2222

2323
/// Enables use of `Seq` with `{}` format in print statements
24-
impl fmt::Display for Seq {
24+
impl<T: fmt::Display> fmt::Display for Seq<T> {
2525
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2626
write!(f, "[begin: {}, end: {}, step: {}]", self.begin, self.end, self.step)
2727
}
2828
}
2929

30-
impl Seq {
30+
impl<T: Copy> Seq<T> {
3131
/// Create a `Seq` that goes from `begin` to `end` at a step size of `step`
32-
pub fn new(begin: f64, end: f64, step: f64) -> Seq {
32+
pub fn new(begin: T, end: T, step: T) -> Self {
3333
Seq { begin: begin, end: end, step: step, }
3434
}
3535

3636
/// Get begin index of Seq
37-
pub fn begin(&self) -> f64 {
38-
self.begin as f64
37+
pub fn begin(&self) -> T {
38+
self.begin
3939
}
4040

4141
/// Get begin index of Seq
42-
pub fn end(&self) -> f64 {
43-
self.end as f64
42+
pub fn end(&self) -> T {
43+
self.end
4444
}
4545

4646
/// Get step size of Seq
47-
pub fn step(&self) -> f64 {
48-
self.step as f64
47+
pub fn step(&self) -> T {
48+
self.step
4949
}
5050
}

0 commit comments

Comments
 (0)