The OpenD Programming Language

find

Finds a backward index such that pred(slices[0].backward(index), ..., slices[$-1].backward(index)) is true.

template find(alias pred)
static if(__traits(isSame, naryFun!pred, pred))
Select!(DimensionCount!(Slices[0]) > 1, size_t[DimensionCount!(Slices[0])], size_t)
find
(
Slices...
)
(
Slices slices
)
if (
Slices.length &&
)

Members

Functions

find
Select!(DimensionCount!(Slices[0]) > 1, size_t[DimensionCount!(Slices[0])], size_t) find(Slices slices)

Parameters

pred

A predicate.

Optimization: To check if any element was found use the last dimension (row index). This will slightly optimize the code.

if (backwardIndex)
{
    auto elem1 = slice1.backward(backwardIndex);
    //...
    auto elemK = sliceK.backward(backwardIndex);
}
else
{
    // not found
}

Examples

Ranges and arrays

import std.range : iota;

auto sl = iota(10);
size_t index = sl.find!"a == 3";

assert(sl[$ - index] == 3);
import mir.ndslice.topology : iota;
// 0 1 2
// 3 4 5
auto sl = iota(2, 3);
size_t[2] bi = sl.find!"a == 3";
assert(sl.backward(bi) == 3);
assert(sl[$ - bi[0], $ - bi[1]] == 3);

bi = sl.find!"a == 6";
assert(bi[0] == 0);
assert(bi[1] == 0);

Multiple slices

import mir.ndslice.topology : iota;

// 0 1 2
// 3 4 5
auto a = iota(2, 3);
// 10 11 12
// 13 14 15
auto b = iota([2, 3], 10);

size_t[2] bi = find!((a, b) => a * b == 39)(a, b);
assert(a.backward(bi) == 3);
assert(b.backward(bi) == 13);

Zipped slices

import mir.ndslice.topology : iota, zip;

// 0 1 2
// 3 4 5
auto a = iota(2, 3);
// 10 11 12
// 13 14 15
auto b = iota([2, 3], 10);

size_t[2] bi = zip!true(a, b).find!"a.a * a.b == 39";

assert(a.backward(bi) == 3);
assert(b.backward(bi) == 13);

Mutation on-the-fly

import mir.ndslice.allocation : slice;
import mir.ndslice.topology : as, iota;

// 0 1 2
// 3 4 5
auto sl = iota(2, 3).as!double.slice;

static bool pred(T)(ref T a)
{
    if (a == 5)
        return true;
    a = 8;
    return false;
}

size_t[2] bi = sl.find!pred;

assert(bi == [1, 1]);
assert(sl.backward(bi) == 5);

import mir.test;
// sl was changed
sl.should == [[8, 8, 8],
              [8, 8, 5]];

See Also

findIndex, any, mir.ndslice.slice.Slice.backward.

Optimization: find!"a" has accelerated specialization for slices created with mir.ndslice.topology.bitwise, mir.ndslice.allocation.bitSlice.

Meta