Skip to content

Commit 6b45f71

Browse files
author
Jason Ramapuram
committed
initial index commit
1 parent 8cbe77e commit 6b45f71

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

src/index.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
extern crate libc;
2+
3+
use array::Array;
4+
use defines::AfError;
5+
use dim4::Dim4;
6+
use seq::Seq;
7+
use defines::Aftype;
8+
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong};
9+
10+
type MutAfArray = *mut self::libc::c_longlong;
11+
type MutAfSeq = *mut Seq;
12+
type MutMutAfIndex = *mut *mut self::libc::c_longlong;
13+
type MutAfIndex = *mut self::libc::c_longlong;
14+
type MutDouble = *mut self::libc::c_double;
15+
type MutUint = *mut self::libc::c_uint;
16+
type AfArray = self::libc::c_longlong;
17+
type AfIndex = self::libc::c_longlong;
18+
type DimT = self::libc::c_longlong;
19+
type SeqT = self::libc::c_longlong;
20+
type IndexT = self::libc::c_longlong;
21+
22+
#[allow(dead_code)]
23+
extern {
24+
fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int;
25+
fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int;
26+
fn af_release_index(indexer: MutAfIndex) -> c_int;
27+
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const IndexT) -> c_int;
28+
fn af_lookup(out: MutAfArray, arr: AfArray, indices: AfArray, dim: c_uint) -> c_int;
29+
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const IndexT, rhs: AfArray) -> c_int;
30+
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int;
31+
fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: *const IndexT, rhs: AfArray) -> c_int;
32+
}
33+
34+
pub struct Index {
35+
handle: i64,
36+
is_batch: bool,
37+
is_seq: bool,
38+
}
39+
40+
impl Index {
41+
#[allow(unused_mut)]
42+
pub fn new<T>(arr: Option<Array>, seq: Option<Seq>, is_batch: bool) -> Result<Index, AfError> {
43+
unsafe {
44+
let mut err_val: c_int = 0;
45+
let mut temp: i64 = 0;
46+
let mut is_seq = false;
47+
err_val = match arr {
48+
//c_func(&mut (x as *mut libc::c_void)); --> (&mut x) as *mut _ as *mut *mut libc::c_void.
49+
//&mut temp as MutMutAfIndex, x),
50+
Some(mut x) => { is_seq = false; af_create_array_index((&mut temp) as *mut _ as MutMutAfIndex, x.get() as AfArray) },
51+
None => 0,
52+
};
53+
54+
err_val = match seq {
55+
Some(mut x) => { is_seq = true; af_create_seq_index((&mut temp) as *mut _ as MutMutAfIndex, &mut x, is_batch as c_int) },
56+
None => AfError::ERR_UNKNOWN as c_int,
57+
};
58+
59+
match err_val {
60+
0 => Ok(Index {handle: temp, is_batch: is_batch, is_seq: is_seq}),
61+
_ => Err(AfError::from(err_val)),
62+
}
63+
}
64+
}
65+
66+
pub fn index(&self, input: Array, seqs: &[Seq]) -> Result<Array, AfError> {
67+
unsafe {
68+
let mut temp: i64 = 0;
69+
let err_val = af_index(&mut temp as MutAfArray
70+
, input.get() as AfArray, seqs.len() as u32
71+
, seqs.as_ptr() as *const SeqT);
72+
match err_val {
73+
0 => Ok(Array::from(temp)),
74+
_ => Err(AfError::from(err_val)),
75+
}
76+
}
77+
}
78+
79+
pub fn lookup(&self, input: Array, indices: Array, seq_dim: i32) -> Result<Array, AfError> {
80+
unsafe {
81+
let mut temp: i64 = 0;
82+
let err_val = af_lookup(&mut temp as MutAfArray
83+
, input.get() as AfArray
84+
, indices.get() as AfArray
85+
, seq_dim as c_uint);
86+
match err_val {
87+
0 => Ok(Array::from(temp)),
88+
_ => Err(AfError::from(err_val)),
89+
}
90+
}
91+
}
92+
93+
pub fn assign_seq(&self, lhs: Array, ndims: usize, seq_dim: i32, seqs: &[Seq], rhs: Array) -> Result<Array, AfError> {
94+
unsafe{
95+
let mut temp: i64 = 0;
96+
let err_val = af_assign_seq(&mut temp as MutAfArray
97+
, lhs.get() as AfArray
98+
, ndims as c_uint, seqs.as_ptr() as *const IndexT
99+
, rhs.get() as AfArray);
100+
match err_val {
101+
0 => Ok(Array::from(temp)),
102+
_ => Err(AfError::from(err_val)),
103+
}
104+
}
105+
}
106+
107+
pub fn index_gen(&self, input: Array, ndims: Dim4, indices: &[Index]) -> Result<Array, AfError> {
108+
unsafe{
109+
let mut temp: i64 = 0;
110+
let err_val = af_index_gen(&mut temp as MutAfArray
111+
, input.get() as AfArray
112+
, ndims.get().as_ptr() as DimT
113+
, indices.as_ptr() as *const IndexT);
114+
match err_val {
115+
0 => Ok(Array::from(temp)),
116+
_ => Err(AfError::from(err_val)),
117+
}
118+
}
119+
}
120+
121+
pub fn assign_gen(&self, lhs: Array, ndims: Dim4, indices: &[Index], rhs: Array) -> Result<Array, AfError> {
122+
unsafe{
123+
let mut temp: i64 = 0;
124+
let err_val = af_assign_gen(&mut temp as MutAfArray
125+
, lhs.get() as AfArray
126+
, ndims.get().as_ptr() as DimT
127+
, indices.as_ptr() as *const IndexT
128+
, rhs.get() as AfArray);
129+
match err_val {
130+
0 => Ok(Array::from(temp)),
131+
_ => Err(AfError::from(err_val)),
132+
}
133+
}
134+
}
135+
136+
pub fn get(&self) -> i64 {
137+
self.handle
138+
}
139+
140+
pub fn is_seq(&self) -> bool{
141+
self.is_seq
142+
}
143+
144+
pub fn is_batch(&self) -> bool{
145+
self.is_seq
146+
}
147+
}
148+
149+
impl Drop for Index {
150+
fn drop(&mut self) {
151+
unsafe {
152+
let ret_val = af_release_index(self.handle as MutAfIndex);
153+
match ret_val {
154+
0 => (),
155+
_ => panic!("Failed to destruct Index: {}", ret_val),
156+
}
157+
}
158+
}
159+
}

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ mod defines;
4444
pub use dim4::Dim4;
4545
mod dim4;
4646

47+
pub use index::Index;
48+
mod index;
49+
50+
pub use seq::Seq;
51+
mod seq;
52+
4753
pub use graphics::Window;
4854
mod graphics;
4955

src/seq.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
extern crate libc;
2+
3+
use std::fmt;
4+
use std::ops::Index;
5+
use std::default::Default;
6+
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong, c_double};
7+
8+
#[derive(Copy, Clone)]
9+
#[repr(C)]
10+
pub struct Seq {
11+
begin: c_double,
12+
end: c_double,
13+
step: c_double,
14+
}
15+
16+
impl Default for Seq {
17+
fn default() -> Seq {
18+
Seq { begin: 0.0, end: 0.0, step: 0.0, }
19+
}
20+
}
21+
22+
impl fmt::Display for Seq {
23+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24+
write!(f, "[begin: {}, end: {}, step: {}", self.begin, self.end, self.step)
25+
}
26+
}
27+
28+
impl Seq {
29+
pub fn new(begin: f64, end: f64, step: f64) -> Seq {
30+
Seq { begin: begin, end: end, step: step, }
31+
}
32+
33+
pub fn begin(&self) -> f64 {
34+
self.begin as f64
35+
}
36+
37+
pub fn end(&self) -> f64 {
38+
self.end as f64
39+
}
40+
41+
pub fn step(&self) -> f64 {
42+
self.step as f64
43+
}
44+
}

0 commit comments

Comments
 (0)