kfind

Performs a linear scan of haystack from [0 .. haystack.length$(RP), returning the index of the first element matching needle, or haystack.length if no match was found. Comparisons will be performed using the supplied predicate or '==' if none is supplied.

This function uses the KMP algorithm and offers O(M+N) performance but must allocate a temporary buffer of size needle.sizeof to do so. If it is available on the target system, alloca will be used for the allocation, otherwise a standard dynamic memory allocation will occur.

Parameters

haystack Buffer!(T)

The array to search.

needle T

The pattern to search for.

pred Pred

The evaluation predicate, which should return true if e1 is equal to e2 and false if not. This predicate may be any callable type.

Return Value

Type: size_t

The index of the first match or haystack.length if no match was found.

Examples

test!("==")(kfind( "abc", 'b' ), 1);
test!("==")( kfind( "abc", "a" ), 0 );
auto buffer = createBuffer([ 1, 2, 3 ]);
test!("==")(kfind(buffer, 1), 0);
auto buffer = createBuffer([ 1, 2, 3 ]);
test!("==")(kfind(buffer, [ 2, 3 ]), 1);

Meta