mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
19 lines
688 B
Go
19 lines
688 B
Go
package util
|
|
|
|
import "sort"
|
|
|
|
// SearchSlice uses binary search to find and return the smallest index i
|
|
// in [0, n) at which f(i) is true, assuming that on the range [0, n),
|
|
// f(i) == true implies f(i+1) == true. That is, SearchSlice requires that
|
|
// f is false for some (possibly empty) prefix of the input range [0, n)
|
|
// and then true for the (possibly empty) remainder; SearchSlice returns
|
|
// the first true index.
|
|
// SearchSlice calls f(i) only for i in the range [0, n).
|
|
func SearchSlice(sliceLength int, searchFunc func(int) bool) (foundIndex int, ok bool) {
|
|
result := sort.Search(sliceLength, searchFunc)
|
|
if result == sliceLength {
|
|
return -1, false
|
|
}
|
|
return result, true
|
|
}
|