DoxigAlpha

prefixScanWithFunc

Same as prefixScan, but with a user-provided, mathematically associative function.

Function parameters

Parameters

#
hop:isize
vec:anytype
ErrorType:type
The error type that `func` might return.
func:fn (@TypeOf(vec), @TypeOf(vec)) if (ErrorType == void) @TypeOf(vec) else ErrorType!@TypeOf(vec)
identity:std.meta.Child(@TypeOf(vec))
When one operand of the operation performed by `func` is this value, the result must equal the other operand.

Functions in this namespace

Functions

#
suggestVectorLength
Suggests a target-dependant vector length for a given type, or null if scalars are recommended.
VectorIndex
Returns the smallest type of unsigned ints capable of indexing any element within the given vector type.
VectorCount
Returns the smallest type of unsigned ints capable of holding the length of the given vector type.
iota
Returns a vector containing the first `len` integers in order from 0 to `len`-1.
repeat
Returns a vector containing the same elements as the input, but repeated until the desired length is reached.
join
Returns a vector containing all elements of the first vector at the lower indices followed by all elements of the second vector
interlace
Returns a vector whose elements alternates between those of each input vector.
deinterlace
The contents of `interlaced` is evenly split between vec_count vectors that are returned as an array.
mergeShift
Joins two vectors, shifts them leftwards (towards lower indices) and extracts the leftmost elements into a vector the length of a and b.
shiftElementsRight
Elements are shifted rightwards (towards higher indices).
shiftElementsLeft
Elements are shifted leftwards (towards lower indices).
rotateElementsLeft
Elements are shifted leftwards (towards lower indices).
rotateElementsRight
Elements are shifted rightwards (towards higher indices).
prefixScanWithFunc
Same as prefixScan, but with a user-provided, mathematically associative function.
prefixScan
Returns a vector whose elements are the result of performing the specified operation on the corresponding

Source

Implementation

#
pub fn prefixScanWithFunc(
    comptime hop: isize,
    vec: anytype,
    /// The error type that `func` might return. Set this to `void` if `func` doesn't return an error union.
    comptime ErrorType: type,
    comptime func: fn (@TypeOf(vec), @TypeOf(vec)) if (ErrorType == void) @TypeOf(vec) else ErrorType!@TypeOf(vec),
    /// When one operand of the operation performed by `func` is this value, the result must equal the other operand.
    /// For example, this should be 0 for addition or 1 for multiplication.
    comptime identity: std.meta.Child(@TypeOf(vec)),
) if (ErrorType == void) @TypeOf(vec) else ErrorType!@TypeOf(vec) {
    // I haven't debugged this, but it might be a cousin of sorts to what's going on with interlace.
    if (builtin.cpu.arch.isMIPS()) @compileError("TODO: Find out why prefixScan doesn't work on MIPS");

    const len = vectorLength(@TypeOf(vec));

    if (hop == 0) @compileError("hop can not be 0; you'd be going nowhere forever!");
    const abs_hop = if (hop < 0) -hop else hop;

    var acc = vec;
    comptime var i = 0;
    inline while ((abs_hop << i) < len) : (i += 1) {
        const shifted = if (hop < 0) shiftElementsLeft(acc, abs_hop << i, identity) else shiftElementsRight(acc, abs_hop << i, identity);

        acc = if (ErrorType == void) func(acc, shifted) else try func(acc, shifted);
    }
    return acc;
}