Documentation ¶
Overview ¶
Package numbers provide miscellaneous functions for working with integer, float, slice of integer, and slice of floats.
Features ¶
List of current features,
- sort slice of floats using in-place mergesort algorithm.
- sort slice of integer/floats by predefined index
- count number of value occurrence in slice of integer/float
- find minimum or maximum value in slice of integer/float
- sum slice of integer/float
Index ¶
- Constants
- func Float64Round(v float64, nprec int) float64
- func Floats64Count(d []float64, class float64) (count int)
- func Floats64Counts(d, classes []float64) (counts []int)
- func Floats64FindMax(d []float64) (maxv float64, maxi int, ok bool)
- func Floats64FindMin(d []float64) (minv float64, mini int, ok bool)
- func Floats64IndirectSort(d []float64, asc bool) (sortedIdx []int)
- func Floats64InplaceMergesort(d []float64, idx []int, l, r int, asc bool)
- func Floats64InsertionSort(d []float64, ids []int, l, r int, asc bool)
- func Floats64IsExist(d []float64, v float64) bool
- func Floats64MaxCountOf(d, classes []float64) (float64, bool)
- func Floats64SortByIndex(d *[]float64, sortedIds []int)
- func Floats64Sum(d []float64) (sum float64)
- func Floats64Swap(d []float64, x, y int)
- func Int64CreateSeq(min, max int64) (seq []int64)
- func IntCreateSeq(min, max int) (seq []int)
- func IntPickRandPositive(maxVal int, dup bool, pickedIds, exsIds []int) (idx int)
- func Ints64Count(d []int64, class int64) (count int)
- func Ints64Counts(d, classes []int64) (counts []int)
- func Ints64FindMax(d []int64) (maxv int64, maxi int, ok bool)
- func Ints64FindMin(d []int64) (minv int64, mini int, ok bool)
- func Ints64IndirectSort(d []int64, asc bool) (sortedIdx []int)
- func Ints64InplaceMergesort(d []int64, idx []int, l, r int, asc bool)
- func Ints64InsertionSort(d []int64, ids []int, l, r int, asc bool)
- func Ints64IsExist(d []int64, i int64) bool
- func Ints64MaxCountOf(d, classes []int64) (int64, bool)
- func Ints64SortByIndex(d *[]int64, sortedIds []int)
- func Ints64Sum(d []int64) (sum int64)
- func Ints64Swap(d []int64, x, y int)
- func IntsCount(d []int, class int) (count int)
- func IntsCounts(d, classes []int) (counts []int)
- func IntsFindMax(d []int) (maxv int, maxi int, ok bool)
- func IntsFindMin(d []int) (minv int, mini int, ok bool)
- func IntsIndirectSort(d []int, asc bool) (sortedIdx []int)
- func IntsInplaceMergesort(d []int, idx []int, l, r int, asc bool)
- func IntsInsertionSort(d, ids []int, l, r int, asc bool)
- func IntsIsExist(d []int, i int) bool
- func IntsMaxCountOf(d, classes []int) (int, bool)
- func IntsSortByIndex(d *[]int, sortedIds []int)
- func IntsSum(d []int) (sum int)
- func IntsSwap(d []int, x, y int)
- func IntsTo64(ints []int) []int64
Examples ¶
Constants ¶
const ( // SortThreshold when the data less than SortThreshold, insertion sort // will be used to replace mergesort. SortThreshold = 7 )
Variables ¶
This section is empty.
Functions ¶
func Float64Round ¶
Float64Round will round `v` to `nprec` digit in fraction.
func Floats64Count ¶
Floats64Count will count number of class in data.
func Floats64Counts ¶
Floats64Counts will count class in data and return each of the counter.
For example, if data is "[1,1,2]" and class is "[1,2]", this function will return "[2,1]".
idx class count 0 : 1 -> 2 1 : 2 -> 1
func Floats64FindMax ¶
Floats64FindMax given slice of float, find the maximum value in slice and and return it with their index.
If data is empty, return -1 in value and index, and false in ok.
Example, given data: [0.0 0.1 0.2 0.2 0.4], it will return 0.4 as max and 4 as index of maximum value.
func Floats64FindMin ¶
Floats64FindMin given slice of float, find the minimum value in slice and and return it with their index.
If data is empty, return -1 in value and index, and false in ok.
Example, given data: [0.0 0.1 0.2 0.2 0.4], it will return 0 as min and 0 as index of minimum value.
func Floats64IndirectSort ¶
Floats64IndirectSort will sort the data and return the sorted index.
func Floats64InplaceMergesort ¶
Floats64InplaceMergesort in-place merge-sort without memory allocation.
func Floats64InsertionSort ¶
Floats64InsertionSort will sort the data using insertion-sort algorithm.
Parameters: - `data` is slice that will be sorted. - `idx` is indices of data. - `l` is starting index of slice to be sorted. - `r` is end index of slice to be sorted.
func Floats64IsExist ¶
Floats64IsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.
func Floats64MaxCountOf ¶
Floats64MaxCountOf will 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 Floats64SortByIndex ¶
Floats64SortByIndex will sort the slice of float using sorted index.
func Floats64Sum ¶
Floats64Sum return sum of slice of float64.
func Floats64Swap ¶
Floats64Swap swap two indices value of 64bit float.
func Int64CreateSeq ¶
Int64CreateSeq will create and return sequence of integer from `min` to `max`.
E.g. if min is 0 and max is 5 then it will return `[0 1 2 3 4 5]`.
func IntCreateSeq ¶
IntCreateSeq will create and return sequence of integer from `min` to `max`.
E.g. if min is 0 and max is 5 then it will return `[0 1 2 3 4 5]`.
func IntPickRandPositive ¶
IntPickRandPositive return random integer value from 0 to maximum value `maxVal`.
The random value is checked with already picked index: `pickedIds`.
If `dup` is true, allow duplicate value in `pickedIds`, otherwise only single unique value allowed in `pickedIds`.
If excluding index `exsIds` is not empty, do not pick the integer value listed in there.
func Ints64Count ¶
Ints64Count will count number of class in data.
func Ints64Counts ¶
Ints64Counts will count class in data and return each of the counter.
For example, if data is "[1,1,2]" and class is "[1,2]", this function will return "[2,1]".
idx class count 0 : 1 -> 2 1 : 2 -> 1
func Ints64FindMax ¶
Ints64FindMax given a slice of integer, return the maximum value in slice and its index.
If data is empty, it will return `-1` in value and index, and false in ok.
Example, given a slice of data: [0 1 2 3 4], it will return 4 as max and 4 as index of maximum value.
func Ints64FindMin ¶
Ints64FindMin given a slice of integer, return the minimum value in slice and its index.
If data is empty, return -1 in value and index; and false in ok.
Example, given a slice of data: [0 1 2 3 4], it will return 0 as min and 0 as minimum index.
func Ints64IndirectSort ¶
Ints64IndirectSort will sort the data and return the sorted index.
func Ints64InplaceMergesort ¶
Ints64InplaceMergesort in-place merge-sort without memory allocation.
func Ints64InsertionSort ¶
Ints64InsertionSort will sort the data using insertion-sort algorithm.
Parameters: - `data` is slice that will be sorted. - `idx` is indices of data. - `l` is starting index of slice to be sorted. - `r` is end index of slice to be sorted.
func Ints64IsExist ¶
Ints64IsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.
func Ints64MaxCountOf ¶
Ints64MaxCountOf will 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 [0, 1, 0, 1, 0] and classes [0, 1], the function will count 0 as 3, 1 as 2; and return 0.
func Ints64SortByIndex ¶
Ints64SortByIndex will sort the slice `d` using sorted index `sortedIds`.
func Ints64Swap ¶
Ints64Swap swap two indices value of integer.
func IntsCounts ¶
IntsCounts will count class in data and return each of the counter.
For example, if data is "[1,1,2]" and class is "[1,2]", this function will return "[2,1]".
idx class count 0 : 1 -> 2 1 : 2 -> 1
func IntsFindMax ¶
IntsFindMax given slice of integer, return the maximum value in slice and its index.
If data is empty, it will return `-1` in value and index, and false in ok.
Example ¶
ints := []int{5, 6, 7, 8, 9, 0, 1, 2, 3, 4} fmt.Println(IntsFindMax(ints))
Output: 9 4 true
func IntsFindMin ¶
IntsFindMin given slice of integer, return the minimum value in slice and its index.
If data is empty, return -1 in value and index, and false in ok.
Example, given a slice of data: [0 1 2 3 4], it will return 0 as min and 0 as minimum index.
func IntsIndirectSort ¶
IntsIndirectSort will sort the data and return the sorted index.
func IntsInplaceMergesort ¶
IntsInplaceMergesort in-place merge-sort without memory allocation.
func IntsInsertionSort ¶
IntsInsertionSort will sort the data using insertion-sort algorithm.
Parameters: - `data` is slice that will be sorted. - `idx` is indices of data. - `l` is starting index of slice to be sorted. - `r` is end index of slice to be sorted.
func IntsIsExist ¶
IntsIsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.
func IntsMaxCountOf ¶
IntsMaxCountOf will 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 IntsSortByIndex ¶
IntsSortByIndex will sort the slice `d` using sorted index `sortedIds`.
Types ¶
This section is empty.