|
| 1 | +extern crate libc; |
| 2 | + |
| 3 | +use array::Array; |
| 4 | +use defines::AfError; |
| 5 | +use seq::Seq; |
| 6 | +use self::libc::{c_int, c_uint, c_longlong}; |
| 7 | + |
| 8 | +type MutAfIndex = *mut self::libc::c_longlong; |
| 9 | +type MutAfArray = *mut self::libc::c_longlong; |
| 10 | +type AfArray = self::libc::c_longlong; |
| 11 | +type DimT = self::libc::c_longlong; |
| 12 | +type IndexT = self::libc::c_longlong; |
| 13 | + |
| 14 | +#[allow(dead_code)] |
| 15 | +extern { |
| 16 | + fn af_create_indexers(indexers: MutAfIndex) -> c_int; |
| 17 | + 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; |
| 19 | + fn af_release_indexers(indexers: MutAfIndex) -> c_int; |
| 20 | + |
| 21 | + fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> c_int; |
| 22 | + 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; |
| 24 | + fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int; |
| 25 | + fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: *const IndexT, rhs: AfArray) -> c_int; |
| 26 | +} |
| 27 | + |
| 28 | +pub struct Indexer { |
| 29 | + handle: i64, |
| 30 | + count: u32, |
| 31 | +} |
| 32 | + |
| 33 | +pub trait Indexable { |
| 34 | + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError>; |
| 35 | +} |
| 36 | + |
| 37 | +impl Indexable for Array { |
| 38 | + #[allow(unused_variables)] |
| 39 | + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> { |
| 40 | + unsafe { |
| 41 | + let err_val = af_set_array_indexer(idxr.clone().get() as MutAfIndex, |
| 42 | + self.get() as AfArray, |
| 43 | + dim as DimT); |
| 44 | + match err_val { |
| 45 | + 0 => Ok(()), |
| 46 | + _ => Err(AfError::from(err_val)), |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Indexable for Seq { |
| 53 | + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> { |
| 54 | + unsafe { |
| 55 | + let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex, self as *const Seq, |
| 56 | + dim as DimT, is_batch.unwrap() as c_int); |
| 57 | + match err_val { |
| 58 | + 0 => Ok(()), |
| 59 | + _ => Err(AfError::from(err_val)), |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl Indexer { |
| 66 | + #[allow(unused_mut)] |
| 67 | + pub fn new() -> Result<Indexer, AfError> { |
| 68 | + unsafe { |
| 69 | + let mut temp: i64 = 0; |
| 70 | + let err_val = af_create_indexers(&mut temp as MutAfIndex); |
| 71 | + match err_val { |
| 72 | + 0 => Ok(Indexer{handle: temp, count: 0}), |
| 73 | + _ => Err(AfError::from(err_val)), |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + pub fn set_index<T: Indexable>(&mut self, idx: &T, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> { |
| 79 | + self.count = self.count + 1; |
| 80 | + idx.set(self, dim, is_batch) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn get(&self) -> i64 { |
| 84 | + self.handle |
| 85 | + } |
| 86 | + |
| 87 | + pub fn len(&self) -> u32 { |
| 88 | + self.count |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl Drop for Indexer { |
| 93 | + fn drop(&mut self) { |
| 94 | + unsafe { |
| 95 | + let ret_val = af_release_indexers(self.handle as MutAfIndex); |
| 96 | + match ret_val { |
| 97 | + 0 => (), |
| 98 | + _ => panic!("Failed to release indexers resource: {}", ret_val), |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> { |
| 105 | + unsafe { |
| 106 | + let mut temp: i64 = 0; |
| 107 | + let err_val = af_index(&mut temp as MutAfArray |
| 108 | + , input.get() as AfArray, seqs.len() as u32 |
| 109 | + , seqs.as_ptr() as *const Seq); |
| 110 | + match err_val { |
| 111 | + 0 => Ok(Array::from(temp)), |
| 112 | + _ => Err(AfError::from(err_val)), |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +pub fn row(input: &Array, row_num: u64) -> Result<Array, AfError> { |
| 118 | + let dims_err = input.dims(); |
| 119 | + let dims = match dims_err { |
| 120 | + Ok(dim) => dim.clone(), |
| 121 | + Err(e) => panic!("Error unwrapping dims in row(): {}", e), |
| 122 | + }; |
| 123 | + |
| 124 | + index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0) |
| 125 | + ,Seq::new(0.0, dims[1] as f64 - 1.0, 1.0)]) |
| 126 | +} |
| 127 | + |
| 128 | +pub fn col(input: &Array, col_num: u64) -> Result<Array, AfError> { |
| 129 | + let dims_err = input.dims(); |
| 130 | + let dims = match dims_err { |
| 131 | + Ok(dim) => dim.clone(), |
| 132 | + Err(e) => panic!("Error unwrapping dims in row(): {}", e), |
| 133 | + }; |
| 134 | + |
| 135 | + index(input, &[Seq::new(0.0, dims[0] as f64 - 1.0, 1.0) |
| 136 | + ,Seq::new(col_num as f64, col_num as f64, 1.0)]) |
| 137 | +} |
| 138 | + |
| 139 | +pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfError> { |
| 140 | + unsafe { |
| 141 | + let mut temp: i64 = 0; |
| 142 | + let err_val = af_lookup(&mut temp as MutAfArray, input.get() as AfArray, |
| 143 | + indices.get() as AfArray, seq_dim as c_uint); |
| 144 | + match err_val { |
| 145 | + 0 => Ok(Array::from(temp)), |
| 146 | + _ => Err(AfError::from(err_val)), |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Result<Array, AfError> { |
| 152 | + unsafe{ |
| 153 | + let mut temp: i64 = 0; |
| 154 | + let err_val = af_assign_seq(&mut temp as MutAfArray, lhs.get() as AfArray, |
| 155 | + ndims as c_uint, seqs.as_ptr() as *const Seq, |
| 156 | + rhs.get() as AfArray); |
| 157 | + match err_val { |
| 158 | + 0 => Ok(Array::from(temp)), |
| 159 | + _ => Err(AfError::from(err_val)), |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +pub fn index_gen(input: &Array, indices: Indexer) -> Result<Array, AfError> { |
| 165 | + unsafe{ |
| 166 | + let mut temp: i64 = 0; |
| 167 | + let err_val = af_index_gen(&mut temp as MutAfArray, input.get() as AfArray, |
| 168 | + indices.len() as DimT, indices.get() as *const IndexT); |
| 169 | + match err_val { |
| 170 | + 0 => Ok(Array::from(temp)), |
| 171 | + _ => Err(AfError::from(err_val)), |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Result<Array, AfError> { |
| 177 | + unsafe{ |
| 178 | + let mut temp: i64 = 0; |
| 179 | + let err_val = af_assign_gen(&mut temp as MutAfArray, lhs.get() as AfArray, |
| 180 | + indices.len() as DimT, indices.get() as *const IndexT, |
| 181 | + rhs.get() as AfArray); |
| 182 | + match err_val { |
| 183 | + 0 => Ok(Array::from(temp)), |
| 184 | + _ => Err(AfError::from(err_val)), |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments