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.

  1. Slice!(IndexIterator!(Iterator, Field), N, kind) indexed(Field source, Slice!(Iterator, N, kind) indices)
    @optmath
    Slice!(IndexIterator!(Iterator, Field), N, kind)
    indexed
    (
    Field
    Iterator
    size_t N
    SliceKind kind
    )
    (
    Field source
    ,
    Slice!(Iterator, N, kind) indices
    )
  2. auto indexed(Field source, S[] indices)
  3. auto indexed(Field source, S indices)

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 Slice!(Iterator, N, kind)

a slice, source of indices.

Return Value

Type: Slice!(IndexIterator!(Iterator, Field), N, kind)

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