added C and Python bindings
This commit is contained in:
parent
dcc612fc98
commit
f21804ffaf
6 changed files with 211 additions and 2 deletions
|
|
@ -62,7 +62,7 @@ fn main() -> io::Result<()> {
|
|||
("Runs", Onod::runs(&input_data)),
|
||||
("RunUps", Onod::run_ups(&input_data)),
|
||||
("Prediction", Onod::prediction(&input_data)),
|
||||
("UnCorrelation", Onod::un_correlation(&input_data)),
|
||||
("UnCorrelation", Onod::uncorrelation(&input_data)),
|
||||
];
|
||||
|
||||
for (test_name, (observation, z_score, p_value)) in &tests {
|
||||
|
|
|
|||
43
src/ffi.rs
Normal file
43
src/ffi.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
use std::ffi::{c_char, c_double, c_uchar, CStr};
|
||||
|
||||
use crate::Onod;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn onod_run(
|
||||
test: *const c_char,
|
||||
samples: *const c_uchar,
|
||||
len: usize,
|
||||
result: *mut c_double,
|
||||
) -> bool {
|
||||
// Check for null pointers
|
||||
if test.is_null() || samples.is_null() || result.is_null() {
|
||||
eprintln!("Error: Null pointer passed to onod_run.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert C string to Rust string
|
||||
let test = unsafe {
|
||||
match CStr::from_ptr(test).to_str() {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
eprintln!("Error: Invalid UTF-8 string passed to onod_run.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Convert samples to a Rust slice
|
||||
let samples = unsafe { std::slice::from_raw_parts(samples, len as usize) };
|
||||
|
||||
// Call the Rust `run` function
|
||||
let (obs, z, p) = Onod::run(test, samples);
|
||||
|
||||
// Write results to the output buffer
|
||||
unsafe {
|
||||
*result.offset(0) = obs;
|
||||
*result.offset(1) = z;
|
||||
*result.offset(2) = p;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
32
src/lib.rs
32
src/lib.rs
|
|
@ -1,3 +1,33 @@
|
|||
pub struct Onod;
|
||||
|
||||
mod uniformity;
|
||||
mod uniformity;
|
||||
pub mod ffi;
|
||||
#[cfg(feature = "python")]
|
||||
pub mod python;
|
||||
|
||||
impl Onod {
|
||||
pub fn run(test: &str, samples: &[u8]) -> (f64, f64, f64) {
|
||||
|
||||
match test {
|
||||
"avalanche" => Onod::avalanche(samples),
|
||||
"chi_bit" => Onod::chi_bit(samples),
|
||||
"chi_byte" => Onod::chi_byte(samples),
|
||||
"compression" => Onod::compression(samples),
|
||||
"gaps" => Onod::gaps(samples),
|
||||
"ks" => Onod::ks(samples),
|
||||
"mean_byte" => Onod::mean_byte(samples),
|
||||
"monobit" => Onod::monobit(samples),
|
||||
"pi" => Onod::pi(samples),
|
||||
"prediction" => Onod::prediction(samples),
|
||||
"runs" => Onod::runs(samples),
|
||||
"run_ups" => Onod::run_ups(samples),
|
||||
"shannon" => Onod::shannon(samples),
|
||||
"shells" => Onod::shells(samples),
|
||||
"uncorrelation" => Onod::uncorrelation(samples),
|
||||
_ => {
|
||||
eprintln!("Error: Unknown test '{}'", test);
|
||||
(-1.0, 0.0, 0.0) // Default values for unknown test
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
src/python.rs
Normal file
19
src/python.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use pyo3::prelude::*;
|
||||
use super::Onod as O;
|
||||
|
||||
#[pyclass]
|
||||
struct Onod;
|
||||
|
||||
#[pymethods]
|
||||
impl Onod {
|
||||
#[staticmethod]
|
||||
pub fn run(test: &str, samples: Vec<u8>) -> (f64, f64, f64) {
|
||||
O::run(test, &samples)
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn onod(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_class::<Onod>()?;
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue