binary

package
v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SearchL

func SearchL[T any](n int, f func(int) (bool, T, error)) (int, T, error)

SearchL is a binary search variant, which uses an indicator func f(i), and finds the index of the last element, and the element itself, which returns true.

Example search space: index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 values: 1, 1, 1, 1, 1, 0, 0, 0, 0, 0

SearchL would return: index 4 and f(4) Returns -1, if f(i) returns false for all i in [0,n) Returns n-1, if f(i) returns true for all i in [0,n)

Based on: https://pesho-ivanov.github.io/#Binary%20search

func SearchR

func SearchR[T any](n int, f func(int) (bool, T, error)) (int, T, error)

func SearchWithError added in v1.13.6

func SearchWithError(n int, f func(int) (bool, error)) (int, error)

SearchWithError is like sort.Search but allows f to return an error, and exits earlier on error. The int value returned is the same as what sort.Search returns if no error.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL