pub fn countScalar(comptime T: type, list: []const T, element: T) usize T: typelist: []const Telement: T1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699pub fn countScalar(comptime T: type, list: []const T, element: T) usize {
const n = list.len;
var i: usize = 0;
var found: usize = 0;
if (use_vectors_for_comparison and
(@typeInfo(T) == .int or @typeInfo(T) == .float) and std.math.isPowerOfTwo(@bitSizeOf(T)))
{
if (std.simd.suggestVectorLength(T)) |block_size| {
const Block = @Vector(block_size, T);
const letter_mask: Block = @splat(element);
while (n - i >= block_size) : (i += block_size) {
const haystack_block: Block = list[i..][0..block_size].*;
found += std.simd.countTrues(letter_mask == haystack_block);
}
}
}
for (list[i..n]) |item| {
found += @intFromBool(item == element);
}
return found;
}
Returns the number of times
elementappears in a slice of memory.