Skip to content

Commit 05a9770

Browse files
committed
preliminary python function and test
1 parent b37ebfb commit 05a9770

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

Cargo.lock

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
pyo3 = "0.25.0"
13+
numpy = "0.25.0"

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use pyo3::prelude::*;
2+
use numpy::PyReadonlyArray1;
23

3-
/// Formats the sum of two numbers as string.
44
#[pyfunction]
5-
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
6-
Ok((a + b).to_string())
5+
fn first_true_1d(array: PyReadonlyArray1<bool>) -> isize {
6+
match array.as_slice() {
7+
Ok(slice) => slice.iter().position(|&v| v).map(|i| i as isize).unwrap_or(-1),
8+
Err(_) => -1, // Should not happen for 1D arrays, but fallback to -1
9+
}
710
}
811

9-
/// A Python module implemented in Rust.
12+
1013
#[pymodule]
1114
fn arrayredox(m: &Bound<'_, PyModule>) -> PyResult<()> {
12-
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
15+
m.add_function(wrap_pyfunction!(first_true_1d, m)?)?;
1316
Ok(())
1417
}

tests/test_basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from arrayredox import first_true_1d
2+
import numpy as np
3+
4+
def test_first_true_1d():
5+
6+
a = np.array([False, False, True, False])
7+
assert first_true_1d(a) == 2
8+
9+
b = np.array([False, False, False])
10+
assert first_true_1d(b) == -1
11+
12+
13+

0 commit comments

Comments
 (0)