slices

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 1 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LinearSearch

func LinearSearch[E constraints.Ordered](x []E, target E) (int, bool)

LinearSearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order. Note: Binary-search was compared using the benchmarks. The following code is equivalent to the linear search above:

pos := sort.Search(len(x), func(i int) bool {
    return target < x[i]
})

The binary search wins for very large boundary sets, but the linear search performs better up through arrays between 256 and 512 elements, so we continue to prefer linear search.

func LinearSearchFunc

func LinearSearchFunc[E any](x []E, target E, cmp func(E, E) int) (int, bool)

LinearSearchFunc works like LinearSearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func SplitN

func SplitN[S ~[]E, E any](s S, n int) []S

SplitN slices s into subslices and returns a slice of the subslices .

The count determines the number of subslices to return:

n > 0: at most n subslices; the last subslices will be the unsplit remainder.
n == 0: the result is nil (zero subslices)
n < 0: all subslices as n == len(s)

Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.

Types

This section is empty.

Jump to

Keyboard shortcuts

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