Skip to content

Commit 8330324

Browse files
committed
FEATURE: generalized scan functions
* scan * scan_by_key
1 parent bfd5ada commit 8330324

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

src/algorithm/mod.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
extern crate libc;
22

33
use array::Array;
4-
use defines::AfError;
4+
use defines::{AfError, BinaryOp};
55
use error::HANDLE_ERROR;
6-
use self::libc::{c_int, c_uint, c_double};
6+
use self::libc::{c_int, uint8_t, c_uint, c_double};
77

88
type MutAfArray = *mut self::libc::c_longlong;
99
type MutDouble = *mut self::libc::c_double;
@@ -46,6 +46,10 @@ extern {
4646

4747
fn af_sort_by_key(out_keys: MutAfArray, out_vals: MutAfArray,
4848
in_keys: AfArray, in_vals: AfArray, dim: c_uint, ascend: c_int) -> c_int;
49+
50+
fn af_scan(out: MutAfArray, inp: AfArray, dim: c_int, op: uint8_t, inclusive: c_int) -> c_int;
51+
fn af_scan_by_key(out: MutAfArray, key: AfArray, inp: AfArray,
52+
dim: c_int, op: uint8_t, inclusive: c_int) -> c_int;
4953
}
5054

5155
macro_rules! dim_reduce_func_def {
@@ -855,3 +859,51 @@ pub fn set_intersect(first: &Array, second: &Array, is_unique: bool) -> Array {
855859
Array::from(temp)
856860
}
857861
}
862+
863+
/// Generalized scan
864+
///
865+
/// # Parameters
866+
///
867+
/// - `input` is the data on which scan is to be performed
868+
/// - `dim` is the dimension along which scan operation is to be performed
869+
/// - `op` takes value of [BinaryOp](./enum.BinaryOp.html) enum indicating
870+
/// the type of scan operation
871+
/// - `inclusive` says if inclusive/exclusive scan is to be performed
872+
///
873+
/// # Return Values
874+
///
875+
/// Output Array of scanned input
876+
pub fn scan(input: &Array, dim: i32, op: BinaryOp, inclusive: bool) -> Array {
877+
unsafe {
878+
let mut temp : i64 = 0;
879+
let err_val = af_scan(&mut temp as MutAfArray, input.get() as AfArray, dim as c_int,
880+
op as uint8_t, inclusive as c_int);
881+
HANDLE_ERROR(AfError::from(err_val));
882+
Array::from(temp)
883+
}
884+
}
885+
886+
/// Generalized scan by key
887+
///
888+
/// # Parameters
889+
///
890+
/// - `key` is the key Array
891+
/// - `input` is the data on which scan is to be performed
892+
/// - `dim` is the dimension along which scan operation is to be performed
893+
/// - `op` takes value of [BinaryOp](./enum.BinaryOp.html) enum indicating
894+
/// the type of scan operation
895+
/// - `inclusive` says if inclusive/exclusive scan is to be performed
896+
///
897+
/// # Return Values
898+
///
899+
/// Output Array of scanned input
900+
pub fn scan_by_key(key: &Array, input: &Array, dim: i32, op: BinaryOp, inclusive: bool) -> Array {
901+
unsafe {
902+
let mut temp : i64 = 0;
903+
let err_val = af_scan_by_key(&mut temp as MutAfArray,
904+
key.get() as AfArray, input.get() as AfArray, dim as c_int,
905+
op as uint8_t, inclusive as c_int);
906+
HANDLE_ERROR(AfError::from(err_val));
907+
Array::from(temp)
908+
}
909+
}

src/defines.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,13 @@ pub enum SparseFormat {
370370
/// Coordinate list (row, coloumn, value) tuples.
371371
COO = 3
372372
}
373+
374+
/// Binary operation types for generalized scan functions
375+
#[repr(C)]
376+
#[derive(Clone, Copy, Debug, PartialEq)]
377+
pub enum BinaryOp {
378+
ADD = 0,
379+
MUL = 1,
380+
MIN = 2,
381+
MAX = 3
382+
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use algorithm::{sum, product, min, max, all_true, any_true, count, sum_nan,
1414
pub use algorithm::{sum_all, product_all, min_all, max_all, sum_nan_all, product_nan_all};
1515
pub use algorithm::{all_true_all, any_true_all, count_all, imin, imax, imin_all, imax_all};
1616
pub use algorithm::{accum, locate, diff1, diff2, sort, sort_index, sort_by_key};
17-
pub use algorithm::{set_unique, set_union, set_intersect};
17+
pub use algorithm::{set_unique, set_union, set_intersect, scan, scan_by_key};
1818
mod algorithm;
1919

2020
pub use arith::{add, sub, div, mul, lt, gt, le, ge, eq, neq, and, or, minof, maxof, rem};
@@ -47,7 +47,7 @@ mod device;
4747
pub use defines::{DType, AfError, Backend, ColorMap, YCCStd, HomographyType};
4848
pub use defines::{InterpType, BorderType, MatchType, NormType};
4949
pub use defines::{Connectivity, ConvMode, ConvDomain, ColorSpace, MatProp};
50-
pub use defines::{MarkerType, MomentType, SparseFormat};
50+
pub use defines::{MarkerType, MomentType, SparseFormat, BinaryOp};
5151
mod defines;
5252

5353
pub use dim4::Dim4;

src/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate libc;
22
extern crate num;
33

44
use defines::{AfError, ColorMap, ConvDomain, ConvMode, DType, InterpType, MatProp, MatchType};
5-
use defines::{SparseFormat};
5+
use defines::{SparseFormat, BinaryOp};
66
use error::HANDLE_ERROR;
77
use std::mem;
88
use self::num::Complex;
@@ -143,3 +143,10 @@ impl From<i32> for SparseFormat {
143143
unsafe { mem::transmute(t) }
144144
}
145145
}
146+
147+
impl From<i32> for BinaryOp {
148+
fn from(t: i32) -> BinaryOp {
149+
assert!(BinaryOp::ADD as i32 <= t && t <= BinaryOp::MAX as i32);
150+
unsafe { mem::transmute(t) }
151+
}
152+
}

0 commit comments

Comments
 (0)