binary_search

package
v0.0.0-...-881dfb5 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinarySearcher

type BinarySearcher[V any] struct {
	// contains filtered or unexported fields
}

BinarySearcher searches element from a slice of non decreasing elements.

func NewBinarySearcher

func NewBinarySearcher[V any](compare func(v1, v2 V) int) *BinarySearcher[V]

NewBinarySearcher creates a BinarySearcher using the compare function.

func (*BinarySearcher[V]) LowerBound

func (s *BinarySearcher[V]) LowerBound(elements []V, first int, last int, val V) int

LowerBound finds the leftmost position such that the element at the position is larger than or equal to val. Imagine we'd insert val to the sorted slice and keep the slice sorted, the returned position is the lower bound where we can insert. The range is half close [first, last). Return last if all elements are less than val.

func (*BinarySearcher[V]) UpperBound

func (s *BinarySearcher[V]) UpperBound(elements []V, first int, last int, val V) int

UpperBound finds the leftmost position such that the element at the position is larger than val. Imagine we'd insert val to the sorted slice and keep the slice sorted, the returned position is the upper bound where we can insert. The range is half close [first, last). Return last if all elements are less than or equal to val.

Jump to

Keyboard shortcuts

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