The OpenD Programming Language

count

Counts elements in slices according to the fun.

template count(alias fun)
static if(__traits(isSame, naryFun!fun, fun))
size_t
count
(
Slices...
)
(
Slices slices
)
if (
Slices.length
)

Members

Functions

count
size_t count(Slices slices)

Parameters

fun

A predicate.

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

Examples

Ranges and arrays

import std.range : iota;
// 0 1 2 3 4 5
auto r = iota(6);

assert(r.count!"true" == 6);
assert(r.count!"a" == 5);
assert(r.count!"a % 2" == 3);

Single slice

import mir.ndslice.topology : iota;

//| 0 1 2 |
//| 3 4 5 |
auto sl = iota(2, 3);

assert(sl.count!"true" == 6);
assert(sl.count!"a" == 5);
assert(sl.count!"a % 2" == 3);

Accelerated set bit count

import mir.ndslice.topology: retro, iota, bitwise;
import mir.ndslice.allocation: slice;

//| 0 1 2 |
//| 3 4 5 |
auto sl = iota!size_t(2, 3).bitwise;

assert(sl.count!"true" == 6 * size_t.sizeof * 8);

assert(sl.slice.count!"a" == 7);

// accelerated
assert(sl.count!"a" == 7);
assert(sl.retro.count!"a" == 7);

auto sl2 = iota!ubyte([6], 128).bitwise;
// accelerated
assert(sl2.count!"a" == 13);
assert(sl2[4 .. $].count!"a" == 13);
assert(sl2[4 .. $ - 1].count!"a" == 12);
assert(sl2[4 .. $ - 1].count!"a" == 12);
assert(sl2[41 .. $ - 1].count!"a" == 1);

Meta