Searches for item; returns whether it was found and the index of its first occurrence, or -1.
Elements are compared with ==, or with compare(element, item) when a comparator is given. Searching is
linear and the empty array finds nothing. Long arrays of integers, enums and pointers are searched with
vector instructions, which does not change the result. A given compare always takes the plain loop.
values := int.[5, 8, 5, 13];
found, index := array_find(values, 5);
assert(found && index == 0);
found, index = array_find(values, 6);
assert(!found && index == -1);
same_half :: (a: int, b: int) -> bool { return a / 2 == b / 2; }
found, index = array_find(values, 9, same_half); // 8 and 9 share a half
assert(found && index == 1);- compare: decides whether two elements are equal.
==when not given.
array_find :: (
array: [] $T,
item: T,
$compare: (T, T) -> bool = null
) -> (bool, s64)
Searches for item; returns whether it was found and the index of its first occurrence, or -1.
Elements are compared with ==, or with compare(element, item) when a comparator is given. Searching is
linear and the empty array finds nothing. Long arrays of integers, enums and pointers are searched with
vector instructions, which does not change the result. A given compare always takes the plain loop.
Parameters
- array: [] $T
- item: T
- $compare: (T, T) -> bool
- decides whether two elements are equal.
==when not given.
Returns
- bool
- s64
values := int.[5, 8, 5, 13];
found, index := array_find(values, 5);
assert(found && index == 0);
found, index = array_find(values, 6);
assert(!found && index == -1);
same_half :: (a: int, b: int) -> bool { return a / 2 == b / 2; }
found, index = array_find(values, 9, same_half); // 8 and 9 share a half
assert(found && index == 1);