ints

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2020 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package ints provide a library for working with slice of integer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

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

Count number of class in data.

func Counts

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

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

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

IndirectSort sort the data and return the sorted index.

func InplaceInsertionSort

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

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

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

InplaceMergesort sort the slice "d" in-place, without memory allocation, using mergesort algorithm.

func IsExist

func IsExist(d []int, v int) bool

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

func Max

func Max(d []int) (v int, i int, ok bool)

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

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

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

func MaxRange(d []int, l, r int) (v, i int)

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

func MergeByDistance(a, b []int, distance int) (out []int)

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

func Min(d []int) (v int, i int, ok bool)

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

func MinRange(d []int, l, r int) (v, i int)

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(d []int, v int) ([]int, bool)

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

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

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

func Sum

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

Sum all value in slice.

func Swap

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

Swap two indices value of slice.

func To64

func To64(ints []int) []int64

To64 convert slice of integer to 64 bit values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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