indexed

Takes a field source and a slice indices, and creates a view of source as if its elements were reordered according to indices. indices may include only a subset of the elements of source and may also repeat elements.

Parameters

source Field

a filed, source of data. source must be an array or a pointer, or have opIndex primitive. Full random access range API is not required.

indices S[]

a slice, source of indices.

Return Value

Type: auto

n-dimensional slice with the same kind, shape and strides.

Examples

auto source = [1, 2, 3, 4, 5];
auto indices = [4, 3, 1, 2, 0, 4];
auto ind = source.indexed(indices);
assert(ind == [5, 4, 2, 3, 1, 5]);

assert(ind.retro == source.indexed(indices.retro));

ind[3] += 10; // for index 2
//                0  1   2  3  4
assert(source == [1, 2, 13, 4, 5]);

See Also

indexed is similar to vmap, but a field ([]) is used instead of a function (()), and order of arguments is reversed.

Meta