boxarray_

Function boxarray_ 

Source
pub fn boxarray_<E, L: CUList + IndexCoord<L>, A: Arrays<E, L>, F: Fn(<L as CUList>::CoordType) -> E>(
    f: F,
) -> Box<A>
Expand description

Same as boxarray but use a fonction that takes nested tuples of usize as coordinates and return a value of type E to initialize every cells.

§Examples

Zero-size array (i.e. a simple value)

fn signle_array() {
    let a: Box<u32> = boxarray::boxarray_(|()| 1);
    assert_eq!(*a, 1u32);
}

Single array.

fn signle_array() {
    let a: Box<[u32; 4]> = boxarray::boxarray_(|((),i)| i as  u32);
    assert_eq!(*a, [0,1,2,3]);
}

Nested array.

fn nested_array() {
    let a: Box<[[[i32; 3]; 2]; 4]> = boxarray::boxarray_(|((((),i),j),k)| (i+j*k) as i32);
    let mut sol = [[[0i32; 3]; 2]; 4];
    for k in 0..4 {
        for j in 0..2 {
            for i in 0..3 {
                sol[k][j][i] = (i+j*k) as i32;
            }
        }
    }
    assert_eq!(*a, sol);
}

Fails to compile when the number of coordinates are not the same as the dimension of the nested arrays.

fn nested_array() {
    let a: Box<[[[i32; 3]; 2]; 4]> = boxarray::boxarray_(|(((),i),j)| i as i32);
}
fn nested_array() {
    let a: Box<[[[i32; 3]; 2]; 4]> = boxarray::boxarray_(|(((((),i),j),k),l)| i as i32);
}