Skip to content

Commit a3ba23b

Browse files
committed
Add an example of PyArray::is_contiguous
1 parent 3ad4272 commit a3ba23b

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/array.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,37 @@ impl<T, D> PyArray<T, D> {
150150
unsafe { (*self.as_array_ptr()).flags & flag == flag }
151151
}
152152

153+
/// Returns `true` if the internal data of the array is C-style contiguous
154+
/// (default of numpy and ndarray) or Fortran-style contiguous.
155+
///
156+
/// # Example
157+
/// ```
158+
/// # extern crate pyo3; extern crate numpy; fn main() {
159+
/// use pyo3::types::IntoPyDict;
160+
/// let gil = pyo3::Python::acquire_gil();
161+
/// let py = gil.python();
162+
/// let array = numpy::PyArray::arange(py, 0, 1, 10);
163+
/// assert!(array.is_contiguous());
164+
/// let locals = [("np", numpy::get_array_module(py).unwrap())].into_py_dict(py);
165+
/// let not_contiguous: &numpy::PyArray1<f32> = py
166+
/// .eval("np.zeros((3, 5))[::2, 4]", Some(locals), None)
167+
/// .unwrap()
168+
/// .downcast_ref()
169+
/// .unwrap();
170+
/// assert!(!not_contiguous.is_contiguous());
171+
/// # }
172+
/// ```
153173
pub fn is_contiguous(&self) -> bool {
154174
self.check_flag(npyffi::NPY_ARRAY_C_CONTIGUOUS)
155175
| self.check_flag(npyffi::NPY_ARRAY_F_CONTIGUOUS)
156176
}
157177

178+
/// Returns `true` if the internal data of the array is Fortran-style contiguous.
158179
pub fn is_fotran_contiguous(&self) -> bool {
159180
self.check_flag(npyffi::NPY_ARRAY_F_CONTIGUOUS)
160181
}
161182

183+
/// Returns `true` if the internal data of the array is C-style contiguous.
162184
pub fn is_c_contiguous(&self) -> bool {
163185
self.check_flag(npyffi::NPY_ARRAY_C_CONTIGUOUS)
164186
}
@@ -426,7 +448,7 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
426448
/// assert_eq!(py_array.as_slice().unwrap(), &[0, 1, 2, 3]);
427449
/// let locals = [("np", numpy::get_array_module(py).unwrap())].into_py_dict(py);
428450
/// let not_contiguous: &PyArray1<f32> = py
429-
/// .eval("np.zeros(10)[::2]", Some(locals), None)
451+
/// .eval("np.zeros((3, 5))[[0, 2], [3, 4]]", Some(locals), None)
430452
/// .unwrap()
431453
/// .downcast_ref()
432454
/// .unwrap();

0 commit comments

Comments
 (0)