vendor tsgo

This commit is contained in:
2026-07-09 16:50:43 -04:00
parent c06ea2e5a4
commit 98978e4930
5804 changed files with 1556156 additions and 101 deletions

View File

@@ -0,0 +1,26 @@
package core
// BinarySearchUniqueFunc works like [slices.BinarySearchFunc], but avoids extra
// invocations of the comparison function by assuming that only one element
// in the slice could match the target. Also, unlike [slices.BinarySearchFunc],
// the comparison function is passed the current index of the element being
// compared, instead of the target element.
func BinarySearchUniqueFunc[S ~[]E, E any](x S, cmp func(int, E) int) (int, bool) {
n := len(x)
if n == 0 {
return 0, false
}
low, high := 0, n-1
for low <= high {
middle := low + ((high - low) >> 1)
value := cmp(middle, x[middle])
if value < 0 {
low = middle + 1
} else if value > 0 {
high = middle - 1
} else {
return middle, true
}
}
return low, false
}