Documentation
¶
Overview ¶
Package search provides various linear search, bisect search, and bisect insertion functions.
Index ¶
- func BinarySearch[T any](s []T, t T, p func(T, T) bool) (index int)
- func BisectLeftWith[T any](s []T, t T, p func(T, T) bool) (l int)
- func BisectRightWith[T any](s []T, t T, p func(T, T) bool) (l int)
- func BisectWith[T any](s []T, t T, p func(T, T) bool) int
- func LinearSearch[T comparable](slice []T, target T) int
- func LinearSearchWith[T any](slice []T, target T, p func(T, T) bool) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BinarySearch ¶ added in v0.10.0
BinarySearch returns the index of the element `t` in the sorted slice `s`. If `t` is not found, it returns -1. It leverages BisectWith to find the index.
func BisectLeftWith ¶ added in v0.9.0
BisectLeftWith locates the insertion point for target t in slice s to maintain sorted order. The elements of s must be sorted in ascending order, but need not be unique. The predicate function p is used to compare the target with the elements of the slice.
func BisectRightWith ¶ added in v0.9.0
BisectRightWith is similar to BisectLeftWith. Returns an insertion point which comes after (to the right of) any existing entries of t in s.
func BisectWith ¶ added in v0.9.0
BisectWith locates the insertion point for target t in slice s to maintain sorted order. Behaves like BisectRightWith.
func LinearSearch ¶
func LinearSearch[T comparable](slice []T, target T) int
LinearSearch returns the index of the first occurrence of the target in the slice or -1 if not found. The elements of the slice and the target must be comparable.
func LinearSearchWith ¶
LinearSearchWith returns the index of the first occurrence of the target in the slice or -1 if not found. The predicate function p is used to compare the target with the elements of the slice.
Types ¶
This section is empty.