sklears_utils/array_utils/
memory.rs

1//! Memory layout and optimization utilities
2
3use scirs2_core::ndarray::Array2;
4
5/// Check if array is contiguous in memory
6pub fn is_contiguous<T>(array: &Array2<T>) -> bool {
7    array.is_standard_layout()
8}
9
10/// Make array contiguous by copying if necessary
11pub fn make_contiguous<T: Clone>(array: &Array2<T>) -> Array2<T> {
12    if array.is_standard_layout() {
13        array.clone()
14    } else {
15        array.to_owned()
16    }
17}
18
19/// Efficient copy operation for arrays
20pub fn efficient_copy<T: Clone>(array: &Array2<T>) -> Array2<T> {
21    if array.is_standard_layout() {
22        // Use faster clone for standard layout
23        array.clone()
24    } else {
25        // Convert to standard layout first
26        array.to_owned()
27    }
28}
29
30/// Get memory strides for array
31pub fn get_strides<T>(array: &Array2<T>) -> Vec<isize> {
32    array.strides().to_vec()
33}
34
35/// Check if two arrays have compatible memory layout for operations
36pub fn compatible_layout<T>(a: &Array2<T>, b: &Array2<T>) -> bool {
37    a.raw_dim() == b.raw_dim() && a.strides() == b.strides()
38}