extern crate libc;
use array::Array;
use defines::{AfError, SparseFormat};
use error::HANDLE_ERROR;
use self::libc::{uint8_t, c_void, c_int};
use util::{AfArray, DimT, HasAfEnum, MutAfArray, MutDimT};
#[allow(dead_code)]
extern {
fn af_create_sparse_array(out: MutAfArray, nRows: DimT, nCols: DimT, vals: AfArray,
rowIdx: AfArray, colIdx: AfArray, stype: uint8_t) -> c_int;
fn af_create_sparse_array_from_ptr(out: MutAfArray, nRows: DimT, nCols: DimT, nNZ: DimT,
values: *const c_void, rowIdx: *const c_int, colIdx: *const c_int,
aftype: uint8_t, stype: uint8_t, src: uint8_t) -> c_int;
fn af_create_sparse_array_from_dense(out: MutAfArray, dense: AfArray, stype: uint8_t) -> c_int;
fn af_sparse_convert_to(out: MutAfArray, input: AfArray, dstStrge: uint8_t) -> c_int;
fn af_sparse_to_dense(out: MutAfArray, sparse: AfArray) -> c_int;
fn af_sparse_get_info(vals: MutAfArray, rIdx: MutAfArray, cIdx: MutAfArray, stype: *mut uint8_t,
input: AfArray) -> c_int;
fn af_sparse_get_values(out: MutAfArray, input: AfArray) -> c_int;
fn af_sparse_get_row_idx(out: MutAfArray, input: AfArray) -> c_int;
fn af_sparse_get_col_idx(out: MutAfArray, input: AfArray) -> c_int;
fn af_sparse_get_nnz(out: MutDimT, input: AfArray) -> c_int;
fn af_sparse_get_storage(out: *mut uint8_t, input: AfArray) -> c_int;
}
pub fn sparse(rows: u64, cols: u64, values: &Array, row_indices: &Array, col_indices: &Array,
format: SparseFormat) -> Array {
unsafe {
let mut temp: i64 = 0;
let err_val = af_create_sparse_array(&mut temp as MutAfArray, rows as DimT, cols as DimT,
values.get() as AfArray, row_indices.get() as AfArray,
col_indices.get() as AfArray, format as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
pub fn sparse_from_host<T: HasAfEnum>(rows: u64, cols: u64, nzz: u64,
values: &[T], row_indices: &[i32], col_indices: &[i32],
format: SparseFormat) -> Array {
unsafe {
let aftype = T::get_af_dtype();
let mut temp: i64 = 0;
let err_val = af_create_sparse_array_from_ptr(&mut temp as MutAfArray,
rows as DimT, cols as DimT, nzz as DimT,
values.as_ptr() as *const c_void,
row_indices.as_ptr() as *const c_int,
col_indices.as_ptr() as *const c_int,
aftype as uint8_t, format as uint8_t, 1);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
pub fn sparse_from_dense(dense: &Array, format: SparseFormat) -> Array {
unsafe {
let mut temp : i64 = 0;
let err_val = af_create_sparse_array_from_dense(&mut temp as MutAfArray, dense.get() as AfArray,
format as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
pub fn sparse_convert_to(input: &Array, format: SparseFormat) -> Array {
unsafe {
let mut temp : i64 = 0;
let err_val = af_sparse_convert_to(&mut temp as MutAfArray, input.get() as AfArray,
format as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
pub fn sparse_to_dense(input: &Array) -> Array {
unsafe {
let mut temp : i64 = 0;
let err_val = af_sparse_to_dense(&mut temp as MutAfArray, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
pub fn sparse_get_info(input: &Array) -> (Array, Array, Array, SparseFormat) {
unsafe {
let mut val : i64 = 0;
let mut row : i64 = 0;
let mut col : i64 = 0;
let mut stype : u8 = 0;
let err_val = af_sparse_get_info(&mut val as MutAfArray, &mut row as MutAfArray,
&mut col as MutAfArray, &mut stype as *mut uint8_t,
input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
(Array::from(val), Array::from(row), Array::from(col), SparseFormat::from(stype as i32))
}
}
pub fn sparse_get_values(input: &Array) -> Array {
unsafe {
let mut val : i64 = 0;
let err_val = af_sparse_get_values(&mut val as MutAfArray, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Array::from(val)
}
}
pub fn sparse_get_row_indices(input: &Array) -> Array {
unsafe {
let mut val : i64 = 0;
let err_val = af_sparse_get_row_idx(&mut val as MutAfArray, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Array::from(val)
}
}
pub fn sparse_get_col_indices(input: &Array) -> Array {
unsafe {
let mut val : i64 = 0;
let err_val = af_sparse_get_col_idx(&mut val as MutAfArray, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Array::from(val)
}
}
pub fn sparse_get_nnz(input: &Array) -> i64 {
unsafe {
let mut count : i64 = 0;
let err_val = af_sparse_get_nnz(&mut count as *mut DimT, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
count
}
}
pub fn sparse_get_format(input: &Array) -> SparseFormat {
unsafe {
let mut stype : u8 = 0;
let err_val = af_sparse_get_storage(&mut stype as *mut uint8_t, input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
SparseFormat::from(stype as i32)
}
}