Documentation ¶
Overview ¶
Package sorty is a type-specific, fast, efficient, concurrent/parallel QuickSort implementation. It is in-place and does not require extra memory. You can call corresponding Sort*() to rapidly sort your slices (in ascending order) or collections of objects. For example:
sorty.SortS(string_slice) // native slice sorty.Sort(n, lesswap) // lesswap() function based
Index ¶
- Constants
- Variables
- func IsSorted(n int, lsw Lesswap) int
- func IsSortedB(ar [][]byte) int
- func IsSortedF4(ar []float32) int
- func IsSortedF8(ar []float64) int
- func IsSortedI(ar []int) int
- func IsSortedI4(ar []int32) int
- func IsSortedI8(ar []int64) int
- func IsSortedLen(ar interface{}) int
- func IsSortedP(ar []uintptr) int
- func IsSortedS(ar []string) int
- func IsSortedU(ar []uint) int
- func IsSortedU4(ar []uint32) int
- func IsSortedU8(ar []uint64) int
- func Search(n int, fn func(int) bool) int
- func Sort(n int, lsw Lesswap)
- func SortB(ar [][]byte)
- func SortF4(ar []float32)
- func SortF8(ar []float64)
- func SortI(ar []int)
- func SortI4(ar []int32)
- func SortI8(ar []int64)
- func SortLen(ar interface{})
- func SortP(ar []uintptr)
- func SortS(ar []string)
- func SortU(ar []uint)
- func SortU4(ar []uint32)
- func SortU8(ar []uint64)
- type Lesswap
Constants ¶
const ( // Mli is the maximum slice length for insertion sort in // Sort*() except SortS(), SortB() and Sort(). Mli = 100 // Hmli is the maximum slice length for insertion sort in // SortS(), SortB() and Sort(). Hmli = 40 // Mlr is the maximum slice length for recursion when there are available // goroutines. So Mlr+1 is the minimum slice length for new sorting goroutines. Mlr = 496 )
Variables ¶
var Mxg uint32 = 3
Mxg is the maximum concurrent goroutines (including caller) used for sorting per Sort*() call. Mxg can be changed live, even during an ongoing Sort*() call. Mxg=1 (or a short input) yields single-goroutine sorting: No goroutines or channel will be created by sorty.
Functions ¶
func IsSorted ¶
IsSorted returns 0 if underlying collection of length n is sorted, otherwise it returns i > 0 with less(i,i-1) = true.
func IsSortedB ¶ added in v1.1.0
IsSortedB returns 0 if ar is sorted in ascending lexicographical order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedF4 ¶
IsSortedF4 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedF8 ¶
IsSortedF8 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedI ¶
IsSortedI returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedI4 ¶
IsSortedI4 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedI8 ¶
IsSortedI8 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedLen ¶ added in v1.2.0
func IsSortedLen(ar interface{}) int
IsSortedLen returns 0 if ar is sorted 'by length' in ascending order, otherwise it returns i > 0 with len(ar[i]) < len(ar[i-1]). ar's (underlying) type can be []string or [][]T (for any type T), otherwise it panics.
func IsSortedP ¶
IsSortedP returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedS ¶
IsSortedS returns 0 if ar is sorted in ascending lexicographical order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedU ¶
IsSortedU returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedU4 ¶
IsSortedU4 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
func IsSortedU8 ¶
IsSortedU8 returns 0 if ar is sorted in ascending order, otherwise it returns i > 0 with ar[i] < ar[i-1]
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 collection.
func Sort ¶
Sort concurrently sorts underlying collection of length n via lsw(). 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 { // strict comparator like < or > if r != s { c[r], c[s] = c[s], c[r] } return true } return false } sorty.Sort(len(c), lsw) }
Lesswap is a 'contract' between users and sorty library. Strict comparator, r!=s check, swap and returns are all strictly necessary.
func SortB ¶ added in v1.1.0
func SortB(ar [][]byte)
SortB concurrently sorts ar in ascending lexicographical order.
func SortLen ¶ added in v1.2.0
func SortLen(ar interface{})
SortLen concurrently sorts ar 'by length' in ascending order. ar's (underlying) type can be []string or [][]T (for any type T), otherwise it panics.