Documentation ¶
Overview ¶
Package ints provide a library for working with slice of integer.
Index ¶
- func Count(d []int, class int) (count int)
- func Counts(d, classes []int) (counts []int)
- func IndirectSort(d []int, asc bool) (sortedIdx []int)
- func InplaceInsertionSort(d, ids []int, l, r int, asc bool)
- func InplaceMergesort(d []int, idx []int, l, r int, asc bool)
- func IsExist(d []int, v int) bool
- func Max(d []int) (v int, i int, ok bool)
- func MaxCountOf(d, classes []int) (int, bool)
- func MaxRange(d []int, l, r int) (v, i int)
- func MergeByDistance(a, b []int, distance int) (out []int)
- func Min(d []int) (v int, i int, ok bool)
- func MinRange(d []int, l, r int) (v, i int)
- func Remove(d []int, v int) ([]int, bool)
- func SortByIndex(d *[]int, sortedIds []int)
- func Sum(d []int) (sum int)
- func Swap(d []int, x, y int)
- func To64(ints []int) []int64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Counts ¶
Counts number of each class in slice.
For example, if data 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 will sort the data and their index using insertion-sort algorithm.
Parameters: `d` is slice that will be sorted, `ids` is indices of data, `l` is starting index of slice to be sorted, and `r` is end index of slice to be sorted.
func InplaceMergesort ¶
InplaceMergesort sort the slice "d" in-place, without memory allocation, using mergesort algorithm.
func IsExist ¶
IsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.
func Max ¶
Max find the maximum value in slice and return its value and index.
If slice is empty, it will return false in ok.
Example ¶
ints := []int{5, 6, 7, 8, 9, 0, 1, 2, 3, 4} fmt.Println(Max(ints))
Output: 9 4 true
func MaxCountOf ¶
MaxCountOf count number of occurrence of each element of classes in data and return the class with maximum count.
If `classes` is empty, it will return -1 and false. If `data` is empty, it will return -2 and false. 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 ¶ added in v0.10.1
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 ¶
a := []int{1, 5, 9} b := []int{4, 11, 15} ab := MergeByDistance(a, b, 3) ba := MergeByDistance(b, a, 3) fmt.Println(ab) fmt.Println(ba)
Output: [1 5 9 15] [1 5 9 15]
func Min ¶
Min find the minimum value in slice and return its value and index.
If slice is empty, it will return false in ok.
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 ¶
Remove value "v" from slice if its exist and return new slice and true; otherwise, if not found, return unmodified slice and false.
func SortByIndex ¶
SortByIndex will sort the slice `d` using sorted index `sortedIds`.
Types ¶
This section is empty.