The OpenD Programming Language

mir_slice.opIndex

$(BOLD Indexed slice.)

  1. auto ref opIndex(Indexes indices)
  2. auto ref opIndex(Indexes indices)
  3. auto ref opIndex(size_t[N] _indices)
  4. auto ref opIndex(size_t[N] _indices)
  5. auto ref opIndex(size_t[N] _indices)
  6. auto opIndex(size_t[I] _indices)
  7. auto opIndex(size_t[I] _indices)
  8. auto opIndex(size_t[I] _indices)
  9. auto opIndex(Slices slices)
  10. auto opIndex(Slices slices)
    struct mir_slice(Iterator_, size_t N_ = 1, SliceKind kind_ = Contiguous, Labels_...)
    return scope
    opIndex
    (
    Slices...
    )
    (
    return scope Slices slices
    )
    if (
    isIndexedSlice!Slices
    )
    if (
    0 < N_ &&
    N_ < 255
    &&
    !(
    kind_ == Canonical &&
    N_ == 1
    )
    &&
    Labels_.length <= N_
    &&
    isIterator!Iterator_
    )

Examples

import mir.ndslice.allocation: slice;
auto sli = slice!int(4, 3);
auto idx = slice!(size_t[2])(3);
idx[] = [
    cast(size_t[2])[0, 2],
    cast(size_t[2])[3, 1],
    cast(size_t[2])[2, 0]];

// equivalent to:
// import mir.ndslice.topology: indexed;
// sli.indexed(indx)[] = 1;
sli[idx] = 1;

assert(sli == [
    [0, 0, 1],
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0],
    ]);

foreach (row; sli[[1, 3].sliced])
    row[] += 2;

assert(sli == [
    [0, 0, 1],
    [2, 2, 2], // <--  += 2
    [1, 0, 0],
    [2, 3, 2], // <--  += 2
    ]);
import mir.ndslice.topology: iota;
import mir.ndslice.allocation: slice;
auto sli = slice!int(5, 6);

// equivalent to
// import mir.ndslice.topology: indexed, cartesian;
// auto a = [0, sli.length!0 / 2, sli.length!0 - 1].sliced;
// auto b = [0, sli.length!1 / 2, sli.length!1 - 1].sliced;
// auto c = cartesian(a, b);
// auto minor = sli.indexed(c);
auto minor = sli[[0, $ / 2, $ - 1].sliced, [0, $ / 2, $ - 1].sliced];

minor[] = iota!int([3, 3], 1);

assert(sli == [
//   ↓     ↓        ↓︎
    [1, 0, 0, 2, 0, 3], // <---
    [0, 0, 0, 0, 0, 0],
    [4, 0, 0, 5, 0, 6], // <---
    [0, 0, 0, 0, 0, 0],
    [7, 0, 0, 8, 0, 9], // <---
    ]);

Meta