Documentation ¶
Overview ¶
Example (MaxHeap) ¶
This example inserts several ints into an MaxHeap, checks the maximum, and removes them in order of priority.
package main import ( "container/heap" "fmt" "github.com/searKing/golang/go/exp/slices" ) func main() { h := &slices.MaxHeap[int]{2, 1, 5} heap.Init(h) heap.Push(h, 3) fmt.Printf("maximum: %d\n", (*h)[0]) for h.Len() > 0 { fmt.Printf("%d ", heap.Pop(h)) } }
Output: maximum: 5 5 3 2 1
Example (MinHeap) ¶
This example inserts several ints into an MinHeap, checks the minimum, and removes them in order of priority.
package main import ( "container/heap" "fmt" "github.com/searKing/golang/go/exp/slices" ) func main() { h := &slices.MinHeap[int]{2, 1, 5} heap.Init(h) heap.Push(h, 3) fmt.Printf("minimum: %d\n", (*h)[0]) for h.Len() > 0 { fmt.Printf("%d ", heap.Pop(h)) } }
Output: minimum: 1 1 2 3 5
Index ¶
- func And[E comparable](s ...E) bool
- func AndFunc[S ~[]E, E any](s S, f func(E) bool) bool
- func Contains[E comparable](s []E, v E) bool
- func ContainsFunc[E any](s []E, f func(E) bool) bool
- func Filter[S ~[]E, E comparable](s S) S
- func FilterFunc[S ~[]E, E any](s S, f func(E) bool) S
- func FirstFunc[S ~[]E, E any](s S, f func(E) bool) (e E, ok bool)
- func FirstOrZero[E comparable](s ...E) E
- func FirstOrZeroFunc[S ~[]E, E any](s S, f func(E) bool) E
- func Group[S ~[]E, M map[E]N, E comparable, N int](s S) M
- func GroupFunc[S ~[]E, M map[K]S, E any, K comparable](s S, f func(E) K) M
- func Intersect[S ~[]E, E comparable](s1, s2 S) S
- func IntersectFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S
- func IsPartialSorted[S ~[]E, E constraints.Ordered](s S, k int) bool
- func LinearSearch[S ~[]E, E constraints.Ordered](x S, target E) (int, bool)
- func LinearSearchFunc[S ~[]E, E any](x S, target E, cmp func(E, E) int) (int, bool)
- func Map[S ~[]E, E any, R []M, M string](s S) R
- func MapFunc[S ~[]E, E any, R []M, M any](s S, f func(E) M) R
- func MapIndexFunc[S ~[]E, E any, R []M, M any](s S, f func(i int, e E) M) R
- func Or[E comparable](s ...E) bool
- func OrFunc[S ~[]E, E any](s S, f func(E) bool) bool
- func PartialSort[S ~[]E, E constraints.Ordered](s S, k int)
- func PartialSortFunc[S ~[]E, E any](s S, k int, cmp func(E, E) int)
- func Reverse[S ~[]E, E any](x S)
- func SearchMax[S ~[]E, E constraints.Ordered](s S) int
- func SearchMin[S ~[]E, E constraints.Ordered](s S) int
- func SearchMinFunc[S ~[]E, E any](s S, cmp func(E, E) int) int
- func Split[S ~[]E, E any](s S, sep int) []S
- func SplitN[S ~[]E, E any](s S, sep int, n int) []S
- func TypeAssertFilter[S ~[]E, M ~[]R, E any, R any](s S) M
- func TypeAssertFilterFunc[S ~[]E, M ~[]R, E any, R any](s S, f func(E) (R, bool)) M
- func Union[S ~[]E, E comparable](s1, s2 S) S
- func UnionFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S
- func Uniq[S ~[]E, E comparable](s S) S
- func UniqFunc[S ~[]E, E any](s S, f func(v1, v2 E) bool) S
- type Heap
- type MaxHeap
- type MinHeap
- type SortSlice
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶ added in v1.2.13
func And[E comparable](s ...E) bool
And tests whether the slice satisfying c != zero within all c in the slice. return true if len(s) == 0
func AndFunc ¶ added in v1.2.13
AndFunc tests the slice satisfying f(c), or true if none do. return true if len(s) == 0
func Contains ¶ added in v1.2.38
func Contains[E comparable](s []E, v E) bool
Contains reports whether v is present in s.
func ContainsFunc ¶ added in v1.2.38
ContainsFunc reports whether v satisfying f(s[i]) is present in s.
func Filter ¶ added in v1.2.16
func Filter[S ~[]E, E comparable](s S) S
Filter returns a slice satisfying c != zero within all c in the slice. Filter modifies the contents of the slice s; it does not create a new slice.
func FilterFunc ¶ added in v1.2.16
FilterFunc returns a slice satisfying f(c) within all c in the slice. FilterFunc modifies the contents of the slice s; it does not create a new slice.
func FirstFunc ¶ added in v1.2.11
FirstFunc returns the first item from the slice satisfying f(c), or zero if none do.
func FirstOrZero ¶ added in v1.2.11
func FirstOrZero[E comparable](s ...E) E
FirstOrZero returns the first non-zero item from the slice, or zero if none do.
func FirstOrZeroFunc ¶ added in v1.2.11
FirstOrZeroFunc returns the first item from the slice satisfying f(c), or zero if none do.
func Group ¶ added in v1.2.65
func Group[S ~[]E, M map[E]N, E comparable, N int](s S) M
Group returns a map group by all elements in s that have the same values.
If s is nil, Group returns nil (zero map).
If s is empty, Group returns an empty map.
Else, Group returns a map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[E]N, E comparable, N constraints.Number] if go support template type deduction
func GroupFunc ¶ added in v1.2.65
func GroupFunc[S ~[]E, M map[K]S, E any, K comparable](s S, f func(E) K) M
GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values.
If s is nil, GroupFunc returns nil (zero map).
If s and f are both empty or nil, GroupFunc returns an empty map.
Else, GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[K]S, E any, K comparable] if go support template type deduction
func Intersect ¶ added in v1.2.38
func Intersect[S ~[]E, E comparable](s1, s2 S) S
Intersect returns a slice satisfying c != zero within all c in the slice. Intersect does not modify the contents of the slice s1 and s2; it creates a new slice.
func IntersectFunc ¶ added in v1.2.38
IntersectFunc returns a slice satisfying f(c) within all c in the slice. IntersectFunc does not modify the contents of the slice s1 and s2; it creates a new slice.
func IsPartialSorted ¶ added in v1.2.74
func IsPartialSorted[S ~[]E, E constraints.Ordered](s S, k int) bool
IsPartialSorted reports whether data[:k] is partial sorted, as top k of data[:].
func LinearSearch ¶
func LinearSearch[S ~[]E, E constraints.Ordered](x S, target E) (int, bool)
LinearSearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order. Note: Binary-search was compared using the benchmarks. The following code is equivalent to the linear search above:
pos := sort.Search(len(x), func(i int) bool { return target < x[i] })
The binary search wins for very large boundary sets, but the linear search performs better up through arrays between 256 and 512 elements, so we continue to prefer linear search.
func LinearSearchFunc ¶
LinearSearchFunc works like LinearSearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.
func Map ¶ added in v1.2.51
Map returns a slice mapped by format "%v" within all c in the slice. Map does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M ~string] if go support template type deduction
func MapFunc ¶ added in v1.2.51
MapFunc returns a slice mapped by f(c) within all c in the slice. MapFunc does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M any] if go support template type deduction
func MapIndexFunc ¶ added in v1.2.76
MapIndexFunc works like MapFunc, returns a slice mapped by f(i, c) within all c in the slice. MapIndexFunc does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M any] if go support template type deduction
func Or ¶ added in v1.2.13
func Or[E comparable](s ...E) bool
Or tests whether the slice satisfying c != zero within any c in the slice. return true if len(s) == 0
func OrFunc ¶ added in v1.2.13
OrFunc tests the slice satisfying f(c), or false if none do. return true if len(s) == 0
func PartialSort ¶ added in v1.2.74
func PartialSort[S ~[]E, E constraints.Ordered](s S, k int)
PartialSort rearranges elements such that the range [0, m) contains the sorted m smallest elements in the range [first, data.Len). The order of equal elements is not guaranteed to be preserved. The order of the remaining elements in the range [m, data.Len) is unspecified.
The sort is not guaranteed to be stable: equal elements may be reversed from their original order.
PartialSort modifies the contents of the slice s; it does not create a new slice.
func PartialSortFunc ¶ added in v1.2.74
PartialSortFunc works like PartialSort, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.
func Reverse ¶ added in v1.2.7
func Reverse[S ~[]E, E any](x S)
Reverse reorder a slice of any ordered type in reverse order. Reverse modifies the contents of the slice s; it does not create a new slice.
func SearchMax ¶ added in v1.2.76
func SearchMax[S ~[]E, E constraints.Ordered](s S) int
SearchMax uses liner search to find and return the biggest index i in [0, n) at which f(i) is min(f...), assuming that on the range [0, n), Search returns the first true index. If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) Search calls f(i) only for i in the range [0, n).
func SearchMin ¶ added in v1.2.76
func SearchMin[S ~[]E, E constraints.Ordered](s S) int
SearchMin uses liner search to find and return the smallest index i in [0, n) at which f(i) is min(f...), assuming that on the range [0, n), Search returns the first true index. If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) SearchMin calls f(i) only for i in the range [0, n).
func SearchMinFunc ¶ added in v1.2.76
SearchMinFunc works like SearchMin, but uses a custom comparison function. The slice is sorted in any order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.
func Split ¶ added in v1.2.31
Split slices s into all subslices separated by sep and returns a slice of the subslices between those separators.
If s is less than sep and sep is more than zero, Split returns a slice of length 1 whose only element is s.
If s is nil, Split returns nil (zero subslices).
If both s and sep are empty or zero, Split returns an empty slice.
If sep is <= zero, Split splits after each element, as chunk size is 1.
It is equivalent to SplitN with a count of -1.
func SplitN ¶
SplitN slices s into subslices and returns a slice of the subslices.
The count determines the number of subslices to return:
n > 0: at most n subslices; the last subslices will be the unsplit remainder. The count determines the number of subslices to return: sep > 0: Split splits every sep as chunk size; the last subslices will be the unsplit remainder. sep <= 0: take len(S)/n as chunk size n == 0: the result is nil (zero subslices) n < 0: all subslices as n == len(s)
Edge cases for s and sep (for example, zero) are handled as described in the documentation for Split.
func TypeAssertFilter ¶ added in v1.2.58
TypeAssertFilter returns a slice satisfying r, ok := any(c).(R); ok == true within all r in the slice. TypeAssertFilter does not modify the contents of the slice s; it creates a new slice.
func TypeAssertFilterFunc ¶ added in v1.2.58
TypeAssertFilterFunc returns a slice satisfying f(c) within all c in the slice. TypeAssertFilterFunc does not modify the contents of the slice s; it creates a new slice.
func Union ¶ added in v1.2.38
func Union[S ~[]E, E comparable](s1, s2 S) S
Union returns a slice satisfying c != zero within all c in the slice. Union replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Union does not modify the contents of the slice s1 and s2; it creates a new slice.
func UnionFunc ¶ added in v1.2.38
UnionFunc returns a slice satisfying f(c) within all c in the slice. UnionFunc does not modify the contents of the slice s1 and s2; it creates a new slice.
func Uniq ¶ added in v1.2.38
func Uniq[S ~[]E, E comparable](s S) S
Uniq returns a slice satisfying c != zero within all c in the slice. Uniq modifies the contents of the slice s; it does not create a new slice.
Types ¶
type Heap ¶ added in v1.2.74
func NewHeapFunc ¶ added in v1.2.74
func NewHeapMax ¶ added in v1.2.75
func NewHeapMax[S ~[]E, E constraints.Ordered](s S) *Heap[S, E]
func NewHeapMin ¶ added in v1.2.75
func NewHeapMin[S ~[]E, E constraints.Ordered](s S) *Heap[S, E]
type MaxHeap ¶ added in v1.2.74
type MaxHeap[E constraints.Ordered] []E
An MaxHeap is a max-heap of slices.
type MinHeap ¶ added in v1.2.74
type MinHeap[E constraints.Ordered] []E
An MinHeap is a min-heap of slices.
type SortSlice ¶ added in v1.2.74
type SortSlice[E constraints.Ordered] []E
SortSlice attaches the methods of Interface to []E, sorting in increasing order.
func (SortSlice[E]) Less ¶ added in v1.2.74
Less reports whether x[i] should be ordered before x[j], as required by the sort Interface. Note that floating-point comparison by itself is not a transitive relation: it does not report a consistent ordering for not-a-number (NaN) values. This implementation of Less places NaN values before any others, by using:
x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))