Documentation ¶
Overview ¶
Package sorty provides type-specific concurrent / parallel sorting functionality
sorty is an in-place QuickSort implementation and does not require extra memory. Call corresponding Sort*() to concurrently sort your slice (in ascending order) or collection. For example:
sorty.SortS(string_slice) // native slice sorty.Sort(col) // satisfies sort.Interface sorty.Sort2(col2) // satisfies sorty.Collection2 sorty.Sort3(n, lesswap) // lesswap() function based
Index ¶
- Variables
- func IsSorted(ar Lesser) bool
- func IsSorted3(n int, less func(i, k int) bool) bool
- func IsSortedF4(ar []float32) bool
- func IsSortedF8(ar []float64) bool
- func IsSortedI(ar []int) bool
- func IsSortedI4(ar []int32) bool
- func IsSortedI8(ar []int64) bool
- func IsSortedP(ar []uintptr) bool
- func IsSortedS(ar []string) bool
- func IsSortedU(ar []uint) bool
- func IsSortedU4(ar []uint32) bool
- func IsSortedU8(ar []uint64) bool
- func Search(n int, fn func(int) bool) int
- func Sort(ar Collection)
- func Sort2(ar Collection2)
- func Sort3(n int, lesswap func(i, k, r, s int) bool)
- func SortF4(ar []float32)
- func SortF8(ar []float64)
- func SortI(ar []int)
- func SortI4(ar []int32)
- func SortI8(ar []int64)
- func SortP(ar []uintptr)
- func SortS(ar []string)
- func SortU(ar []uint)
- func SortU4(ar []uint32)
- func SortU8(ar []uint64)
- type Collection
- type Collection2
- type Lesser
Constants ¶
This section is empty.
Variables ¶
var Mli = 96
Mli is the maximum array length for insertion sort. SortS() and Sort3() use 1/2 of this as their limits. Sort() and Sort2() use 1/4 of this as their limits.
var Mlr = 401
Mlr is the maximum array length for recursion when there is available goroutines. So Mlr+1 is the minimum array length for new sorting goroutines.
var Mxg uint32 = 3
Mxg is the maximum number of goroutines used for sorting per Sort*() call.
Functions ¶
func IsSorted3 ¶ added in v0.4.0
IsSorted3 checks if underlying collection of length n is sorted as per less(). An existing lesswap() can be used like:
IsSorted3(n, func(i, k int) bool { return lesswap(i, k, 0, 0) })
func IsSortedF4 ¶
IsSortedF4 checks if ar is sorted in ascending order.
func IsSortedF8 ¶
IsSortedF8 checks if ar is sorted in ascending order.
func IsSortedI4 ¶
IsSortedI4 checks if ar is sorted in ascending order.
func IsSortedI8 ¶
IsSortedI8 checks if ar is sorted in ascending order.
func IsSortedU4 ¶
IsSortedU4 checks if ar is sorted in ascending order.
func IsSortedU8 ¶
IsSortedU8 checks if ar is sorted in ascending order.
func Search ¶
Search returns lowest integer k in [0,n) where fn(k) is true, assuming:
fn(k) => fn(k+1)
If there is no such k, it returns n. It can be used to locate an element in a sorted array or collection.
func Sort3 ¶ added in v0.4.0
Sort3 concurrently sorts underlying collection of length n via lesswap() which must be equivalent to:
if less(i, k) { // must be a strict ordering like < or > if r != s { swap(r, s) } return true } return false
Once for each non-trivial type you want to sort in a certain way, you can implement a custom sorting routine (for a slice for example) as:
func SortObjAsc(c []Obj) { lsw := func(i, k, r, s int) bool { if c[i].Key < c[k].Key { // or your custom comparator if r != s { c[r], c[s] = c[s], c[r] } return true } return false } sorty.Sort3(len(c), lsw) }
Types ¶
type Collection ¶
Collection is same with standard library's sort.Interface. It represents a collection of sortable (as per Less) elements.
type Collection2 ¶
type Collection2 interface { Lesser // LessSwap must be equivalent to: // if less(i, k) { // swap(r, s) // return true // } // return false LessSwap(i, k, r, s int) bool }
Collection2 is an alternative for faster interface-based sorting
type Lesser ¶
type Lesser interface { // Len is the number of elements in the collection. First element has index 0. Len() int // Less (a strict ordering like < or >) reports if element-i should come // before element-k: // Less(i,k) && Less(k,r) => Less(i,r) // Less(i,k) => ! Less(k,i) Less(i, k int) bool }
Lesser represents a collection of comparable elements.