Documentation
¶
Overview ¶
Package slices complement the standard slices package for working with slices comparable and cmp.Ordered types.
List of current features,
- sort slice of cmp.Ordered types using in-place mergesort algorithm.
- sort slice of cmp.Ordered types by predefined index.
- count number of value occurrence in slice of [comparable] types.
- find minimum or maximum value in slice of cmp.Ordered types.
- sum slice of constraints.Integer or constraints.Float types.
Index ¶
- func Count[S []E, E comparable](slice S, val E) (count int)
- func Counts[S []E, E comparable](slice S, classes S) (counts []int)
- func IndirectSort[S []E, E cmp.Ordered](slice S, isAsc bool) (sortedIdx []int)
- func InplaceInsertionSort[S []E, E cmp.Ordered](slice S, ids []int, l, r int, isAsc bool)
- func InplaceMergesort[S []E, E cmp.Ordered](slice S, ids []int, l, r int, isAsc bool)
- func Max2[S []E, E cmp.Ordered](slice S) (max E, idx int)
- func MaxCountOf[S []E, E comparable](slice S, classes S) (maxClass E, state int)
- func MaxRange[S []E, E cmp.Ordered](slice S, l, r int) (v E, i int)
- func MergeByDistance[S []E, E ~int | ~int8 | ~int16 | ~int32 | ~int64](a, b S, distance E) (out S)
- func Min2[S []E, E cmp.Ordered](slice S) (min E, idx int)
- func MinRange[S []E, E cmp.Ordered](slice S, l, r int) (v E, i int)
- func Remove[S []E, E comparable](slice S, v E) (S, bool)
- func SortByIndex[S []E, E cmp.Ordered](slice *S, sortedIdx []int)
- func Sum[S []E, E constraints.Integer | constraints.Float](slice S) (sum E)
- func Swap[S []E, E any](slice S, x, y int)
- func ToInt64[S []E, E constraints.Integer](ints S) []int64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Count ¶
func Count[S []E, E comparable](slice S, val E) (count int)
Count the number of occurence of val in slice.
func Counts ¶
func Counts[S []E, E comparable](slice S, classes S) (counts []int)
Counts number of each class in slice.
For example, if slice is "[1,1,2]" and classes is "[1,2]", this function will return "[2,1]".
func IndirectSort ¶
IndirectSort sort the data and return the sorted index.
func InplaceInsertionSort ¶
InplaceInsertionSort sort the data and their index using insertion-sort algorithm.
The parameter `slice` is the slice that will be sorted, `ids` is indices of slice, `l` is starting index of slice to be sorted, and `r` is the end index of slice to be sorted.
func InplaceMergesort ¶
InplaceMergesort sort the slice "slice" in-place, without memory allocation, using mergesort algorithm. The ids parameter is empty slice with length equal to the length of slice, which will used for storing sorted index.
func Max2 ¶
Max2 find the maximum value in slice and return its value and index. If slice is empty, it will return (0, -1).
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/slices" ) func main() { ints := []int{5, 6, 7, 8, 9, 0, 1, 2, 3, 4} fmt.Println(slices.Max2(ints)) }
Output: 9 4
func MaxCountOf ¶
func MaxCountOf[S []E, E comparable](slice S, classes S) (maxClass E, state int)
MaxCountOf count number of occurrence of each element of classes in data and return the class with maximum count.
If classes has the same count value, then the first max in the class will be returned.
For example, given a data [5, 6, 5, 6, 5] and classes [5, 6, 7], the function will count 5 as 3, 6 as 2, and 7 as 0. Since frequency of 5 is greater than 6 and 7, then it will return `5` and true.
func MaxRange ¶
MaxRange find the (last) maximum value in slice between index "l" and "r".
WARNING: This function does not check index out of range.
func MergeByDistance ¶
func MergeByDistance[S []E, E ~int | ~int8 | ~int16 | ~int32 | ~int64]( a, b S, distance E, ) (out S)
MergeByDistance merge two slice of integers by their distance between each others.
For example, if slice a contains "{1, 5, 9}" and b contains "{4, 11, 15}" and the distance is 3, the output of merged is "{1, 5, 9, 15}". The 4 and 11 are not included because 4 is in range between 1 and (1+3), and 11 is in range between 9 and 9+3.
Example ¶
package main import ( "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/slices" ) func main() { a := []int{1, 5, 9} b := []int{4, 11, 15} ab := slices.MergeByDistance(a, b, 3) ba := slices.MergeByDistance(b, a, 3) fmt.Println(ab) fmt.Println(ba) }
Output: [1 5 9 15] [1 5 9 15]
func Min2 ¶
Min2 find the minimum value in slice and return its value and index.
If slice is empty, it will return (0, -1).
func MinRange ¶
MinRange find the (last) minimum value in slice between index "l" and "r".
WARNING: This function does not check index out of range.
func Remove ¶
func Remove[S []E, E comparable](slice S, v E) (S, bool)
Remove val from slice if its exist and return new slice and true. Otherwise, if val not found, return unmodified slice and false.
func SortByIndex ¶
SortByIndex sort the slice using sorted index sortedIdx.
func Sum ¶
func Sum[S []E, E constraints.Integer | constraints.Float](slice S) (sum E)
Sum all value in slice.
func ToInt64 ¶
func ToInt64[S []E, E constraints.Integer](ints S) []int64
ToInt64 convert slice of integer to its 64 bit values.
Types ¶
This section is empty.