numbers

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2019 License: BSD-3-Clause, BSD-3-Clause Imports: 3 Imported by: 0

README

GoDoc Go Report Card

Package numbers provide functions for working with integer, float, slice of integers, and slice of floats.

Currently it have function to,

  • sort slice of floats using in-place mergesort algorithm
  • sort slice of integer/floats by predefined index
  • count number of value occurence in slice of integer/float
  • find minimum or maximum value in slice of integer/float
  • sum slice of integer/float

See documentation for more information.

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

Examples

Constants

View Source
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

func Float64Round(v float64, nprec int) float64

Float64Round will round `v` to `nprec` digit in fraction.

func Floats64Count

func Floats64Count(d []float64, class float64) (count int)

Floats64Count will count number of class in data.

func Floats64Counts

func Floats64Counts(d, classes []float64) (counts []int)

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

func Floats64FindMax(d []float64) (maxv float64, maxi int, ok bool)

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

func Floats64FindMin(d []float64) (minv float64, mini int, ok bool)

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

func Floats64IndirectSort(d []float64, asc bool) (sortedIdx []int)

Floats64IndirectSort will sort the data and return the sorted index.

func Floats64InplaceMergesort

func Floats64InplaceMergesort(d []float64, idx []int, l, r int, asc bool)

Floats64InplaceMergesort in-place merge-sort without memory allocation.

func Floats64InsertionSort

func Floats64InsertionSort(d []float64, ids []int, l, r int, asc bool)

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

func Floats64IsExist(d []float64, v float64) bool

Floats64IsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.

func Floats64MaxCountOf

func Floats64MaxCountOf(d, classes []float64) (float64, bool)

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

func Floats64SortByIndex(d *[]float64, sortedIds []int)

Floats64SortByIndex will sort the slice of float using sorted index.

func Floats64Sum

func Floats64Sum(d []float64) (sum float64)

Floats64Sum return sum of slice of float64.

func Floats64Swap

func Floats64Swap(d []float64, x, y int)

Floats64Swap swap two indices value of 64bit float.

func Int64CreateSeq

func Int64CreateSeq(min, max int64) (seq []int64)

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

func IntCreateSeq(min, max int) (seq []int)

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

func IntPickRandPositive(maxVal int, dup bool, pickedIds, exsIds []int) (
	idx int,
)

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

func Ints64Count(d []int64, class int64) (count int)

Ints64Count will count number of class in data.

func Ints64Counts

func Ints64Counts(d, classes []int64) (counts []int)

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

func Ints64FindMax(d []int64) (maxv int64, maxi int, ok bool)

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

func Ints64FindMin(d []int64) (minv int64, mini int, ok bool)

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

func Ints64IndirectSort(d []int64, asc bool) (sortedIdx []int)

Ints64IndirectSort will sort the data and return the sorted index.

func Ints64InplaceMergesort

func Ints64InplaceMergesort(d []int64, idx []int, l, r int, asc bool)

Ints64InplaceMergesort in-place merge-sort without memory allocation.

func Ints64InsertionSort

func Ints64InsertionSort(d []int64, ids []int, l, r int, asc bool)

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

func Ints64IsExist(d []int64, i int64) bool

Ints64IsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.

func Ints64MaxCountOf

func Ints64MaxCountOf(d, classes []int64) (int64, bool)

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

func Ints64SortByIndex(d *[]int64, sortedIds []int)

Ints64SortByIndex will sort the slice `d` using sorted index `sortedIds`.

func Ints64Sum

func Ints64Sum(d []int64) (sum int64)

Ints64Sum return sum of all value in slice.

func Ints64Swap

func Ints64Swap(d []int64, x, y int)

Ints64Swap swap two indices value of integer.

func IntsCount

func IntsCount(d []int, class int) (count int)

IntsCount will count number of class in data.

func IntsCounts

func IntsCounts(d, classes []int) (counts []int)

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

func IntsFindMax(d []int) (maxv int, maxi int, ok bool)

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

func IntsFindMin(d []int) (minv int, mini int, ok bool)

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

func IntsIndirectSort(d []int, asc bool) (sortedIdx []int)

IntsIndirectSort will sort the data and return the sorted index.

func IntsInplaceMergesort

func IntsInplaceMergesort(d []int, idx []int, l, r int, asc bool)

IntsInplaceMergesort in-place merge-sort without memory allocation.

func IntsInsertionSort

func IntsInsertionSort(d, ids []int, l, r int, asc bool)

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

func IntsIsExist(d []int, i int) bool

IntsIsExist will return true if value `v` exist in slice of `d`, otherwise it will return false.

func IntsMaxCountOf

func IntsMaxCountOf(d, classes []int) (int, bool)

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

func IntsSortByIndex(d *[]int, sortedIds []int)

IntsSortByIndex will sort the slice `d` using sorted index `sortedIds`.

func IntsSum

func IntsSum(d []int) (sum int)

IntsSum return sum of all value in slice.

func IntsSwap

func IntsSwap(d []int, x, y int)

IntsSwap swap two indices value of integer.

func IntsTo64

func IntsTo64(ints []int) []int64

IntsTo64 convert slice of integer to 64bit values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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