search

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package search provides various linear search, bisect search, and bisect insertion functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch added in v0.10.0

func BinarySearch[T any](s []T, t T, p func(T, T) bool) (index int)

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

func BisectLeftWith[T any](s []T, t T, p func(T, T) bool) (l int)

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

func BisectRightWith[T any](s []T, t T, p func(T, T) bool) (l int)

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

func BisectWith[T any](s []T, t T, p func(T, T) bool) int

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

func LinearSearchWith[T any](slice []T, target T, p func(T, T) bool) int

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.

Jump to

Keyboard shortcuts

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