123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- package sort
- import "core:mem"
- import _slice "core:slice"
- import "base:intrinsics"
- _ :: intrinsics
- _ :: _slice
- ORD :: intrinsics.type_is_ordered
- Interface :: struct {
- len: proc(it: Interface) -> int,
- less: proc(it: Interface, i, j: int) -> bool,
- swap: proc(it: Interface, i, j: int),
- collection: rawptr,
- }
- // sort sorts an Interface
- // This sort is not guaranteed to be stable
- sort :: proc(it: Interface) {
- max_depth :: proc(n: int) -> int { // 2*ceil(log2(n+1))
- depth: int
- for i := n; i > 0; i >>= 1 {
- depth += 1
- }
- return depth * 2
- }
- n := it->len()
- _quick_sort(it, 0, n, max_depth(n))
- }
- @(deprecated="use slice.sort")
- slice :: proc(array: $T/[]$E) where ORD(E) {
- _slice.sort(array)
- // s := array;
- // sort(slice_interface(&s));
- }
- slice_interface :: proc(s: ^$T/[]$E) -> Interface where ORD(E) {
- return Interface{
- collection = rawptr(s),
- len = proc(it: Interface) -> int {
- s := (^T)(it.collection)
- return len(s^)
- },
- less = proc(it: Interface, i, j: int) -> bool {
- s := (^T)(it.collection)
- return s[i] < s[j]
- },
- swap = proc(it: Interface, i, j: int) {
- s := (^T)(it.collection)
- s[i], s[j] = s[j], s[i]
- },
- }
- }
- reverse_interface :: proc(it: ^Interface) -> Interface {
- return Interface{
- collection = it,
- len = proc(rit: Interface) -> int {
- it := (^Interface)(rit.collection)
- return it.len(it^)
- },
- less = proc(rit: Interface, i, j: int) -> bool {
- it := (^Interface)(rit.collection)
- return it.less(it^, j, i) // reverse parameters
- },
- swap = proc(rit: Interface, i, j: int) {
- it := (^Interface)(rit.collection)
- it.swap(it^, i, j)
- },
- }
- }
- reverse_sort :: proc(it: Interface) {
- it := it
- sort(reverse_interface(&it))
- }
- @(deprecated="use slice.reverse")
- reverse_slice :: proc(array: $T/[]$E) where ORD(E) {
- _slice.reverse(array)
- /*
- s := array;
- sort(Interface{
- collection = rawptr(&s),
- len = proc(it: Interface) -> int {
- s := (^T)(it.collection);
- return len(s^);
- },
- less = proc(it: Interface, i, j: int) -> bool {
- s := (^T)(it.collection);
- return s[j] < s[i]; // manual set up
- },
- swap = proc(it: Interface, i, j: int) {
- s := (^T)(it.collection);
- s[i], s[j] = s[j], s[i];
- },
- });
- */
- }
- is_sorted :: proc(it: Interface) -> bool {
- n := it->len()
- for i := n-1; i > 0; i -= 1 {
- if it->less(i, i-1) {
- return false
- }
- }
- return true
- }
- swap_range :: proc(it: Interface, a, b, n: int) {
- for i in 0..<n {
- it->swap(a+i, b+i)
- }
- }
- rotate :: proc(it: Interface, a, m, b: int) {
- i := m - a
- j := b - m
- for i != j {
- if i > j {
- swap_range(it, m-i, m, j)
- i -= j
- } else {
- swap_range(it, m-i, m+j-1, i)
- j -= 1
- }
- }
- swap_range(it, m-i, m, i)
- }
- @(private)
- _quick_sort :: proc(it: Interface, a, b, max_depth: int) {
- median3 :: proc(it: Interface, m1, m0, m2: int) {
- if it->less(m1, m0) {
- it->swap(m1, m0)
- }
- if it->less(m2, m1) {
- it->swap(m2, m1)
- if it->less(m1, m0) {
- it->swap(m1, m0)
- }
- }
- }
- do_pivot :: proc(it: Interface, lo, hi: int) -> (midlo, midhi: int) {
- m := int(uint(lo+hi)>>1)
- if hi-lo > 40 {
- s := (hi-lo)/8
- median3(it, lo, lo+s, lo+s*2)
- median3(it, m, m-s, m+s)
- median3(it, hi-1, hi-1-s, hi-1-s*2)
- }
- median3(it, lo, m, hi-1)
- pivot := lo
- a, c := lo+1, hi-1
- for ; a < c && it->less(a, pivot); a += 1 {
- }
- b := a
- for {
- for ; b < c && !it->less(pivot, b); b += 1 { // data[b] <= pivot
- }
- for ; b < c && it->less(pivot, c-1); c -=1 { // data[c-1] > pivot
- }
- if b >= c {
- break
- }
- it->swap(b, c-1)
- b += 1
- c -= 1
- }
- protect := hi-c < 5
- if !protect && hi-c < (hi-lo)/4 {
- dups := 0
- if !it->less(pivot, hi-1) {
- it->swap(c, hi-1)
- c += 1
- dups += 1
- }
- if !it->less(b-1, pivot) {
- b -= 1
- dups += 1
- }
- if !it->less(m, pivot) {
- it->swap(m, b-1)
- b -= 1
- dups += 1
- }
- protect = dups > 1
- }
- if protect {
- for {
- for ; a < b && !it->less(b-1, pivot); b -= 1 {
- }
- for ; a < b && it->less(a, pivot); a += 1 {
- }
- if a >= b {
- break
- }
- it->swap(a, b-1)
- a += 1
- b -= 1
- }
- }
- it->swap(pivot, b-1)
- return b-1, c
- }
- heap_sort :: proc(it: Interface, a, b: int) {
- sift_down :: proc(it: Interface, lo, hi, first: int) {
- root := lo
- for {
- child := 2*root + 1
- if child >= hi {
- break
- }
- if child+1 < hi && it->less(first+child, first+child+1) {
- child += 1
- }
- if !it->less(first+root, first+child) {
- return
- }
- it->swap(first+root, first+child)
- root = child
- }
- }
- first, lo, hi := a, 0, b-a
- for i := (hi-1)/2; i >= 0; i -= 1 {
- sift_down(it, i, hi, first)
- }
- for i := hi-1; i >= 0; i -= 1 {
- it->swap(first, first+i)
- sift_down(it, lo, i, first)
- }
- }
- a, b, max_depth := a, b, max_depth
- for b-a > 12 { // only use shell sort for lengths <= 12
- if max_depth == 0 {
- heap_sort(it, a, b)
- return
- }
- max_depth -= 1
- mlo, mhi := do_pivot(it, a, b)
- if mlo-a < b-mhi {
- _quick_sort(it, a, mlo, max_depth)
- a = mhi
- } else {
- _quick_sort(it, mhi, b, max_depth)
- b = mlo
- }
- }
- if b-a > 1 {
- // Shell short with gap 6
- for i in a+6..<b {
- if it->less(i, i-6) {
- it->swap(i, i-6)
- }
- }
- _insertion_sort(it, a, b)
- }
- }
- @(private)
- _insertion_sort :: proc(it: Interface, a, b: int) {
- for i in a+1..<b {
- for j := i; j > a && it->less(j, j-1); j -= 1 {
- it->swap(j, j-1)
- }
- }
- }
- // @(deprecated="use sort.sort or slice.sort_by")
- bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
- assert(f != nil)
- count := len(array)
- init_j, last_j := 0, count-1
- for {
- init_swap, prev_swap := -1, -1
- for j in init_j..<last_j {
- if f(array[j], array[j+1]) > 0 {
- array[j], array[j+1] = array[j+1], array[j]
- prev_swap = j
- if init_swap == -1 {
- init_swap = j
- }
- }
- }
- if prev_swap == -1 {
- return
- }
- init_j = max(init_swap-1, 0)
- last_j = prev_swap
- }
- }
- // @(deprecated="use sort.sort_slice or slice.sort")
- bubble_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
- count := len(array)
- init_j, last_j := 0, count-1
- for {
- init_swap, prev_swap := -1, -1
- for j in init_j..<last_j {
- if array[j] > array[j+1] {
- array[j], array[j+1] = array[j+1], array[j]
- prev_swap = j
- if init_swap == -1 {
- init_swap = j
- }
- }
- }
- if prev_swap == -1 {
- return
- }
- init_j = max(init_swap-1, 0)
- last_j = prev_swap
- }
- }
- // @(deprecated="use sort.sort or slice.sort_by")
- quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
- assert(f != nil)
- a := array
- n := len(a)
- if n < 2 {
- return
- }
- p := a[n/2]
- i, j := 0, n-1
- loop: for {
- for f(a[i], p) < 0 { i += 1 }
- for f(p, a[j]) < 0 { j -= 1 }
- if i >= j {
- break loop
- }
- a[i], a[j] = a[j], a[i]
- i += 1
- j -= 1
- }
- quick_sort_proc(a[0:i], f)
- quick_sort_proc(a[i:n], f)
- }
- // @(deprecated="use sort.sort_slice or slice.sort")
- quick_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
- a := array
- n := len(a)
- if n < 2 {
- return
- }
- p := a[n/2]
- i, j := 0, n-1
- loop: for {
- for a[i] < p { i += 1 }
- for p < a[j] { j -= 1 }
- if i >= j {
- break loop
- }
- a[i], a[j] = a[j], a[i]
- i += 1
- j -= 1
- }
- quick_sort(a[0:i])
- quick_sort(a[i:n])
- }
- _log2 :: proc(x: int) -> int {
- res := 0
- for n := x; n != 0; n >>= 1 {
- res += 1
- }
- return res
- }
- // @(deprecated="use sort.sort or slice.sort_by")
- merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
- merge :: proc(a: A, start, mid, end: int, f: proc(T, T) -> int) {
- s, m := start, mid
- s2 := m + 1
- if f(a[m], a[s2]) <= 0 {
- return
- }
- for s <= m && s2 <= end {
- if f(a[s], a[s2]) <= 0 {
- s += 1
- } else {
- v := a[s2]
- i := s2
- for i != s {
- a[i] = a[i-1]
- i -= 1
- }
- a[s] = v
- s += 1
- m += 1
- s2 += 1
- }
- }
- }
- internal_sort :: proc(a: A, l, r: int, f: proc(T, T) -> int) {
- if l < r {
- m := l + (r - l) / 2
- internal_sort(a, l, m, f)
- internal_sort(a, m+1, r, f)
- merge(a, l, m, r, f)
- }
- }
- internal_sort(array, 0, len(array)-1, f)
- }
- // @(deprecated="use sort.sort_slice or slice.sort")
- merge_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
- merge :: proc(a: A, start, mid, end: int) {
- s, m := start, mid
- s2 := m + 1
- if a[m] <= a[s2] {
- return
- }
- for s <= m && s2 <= end {
- if a[s] <= a[s2] {
- s += 1
- } else {
- v := a[s2]
- i := s2
- for i != s {
- a[i] = a[i-1]
- i -= 1
- }
- a[s] = v
- s += 1
- m += 1
- s2 += 1
- }
- }
- }
- internal_sort :: proc(a: A, l, r: int) {
- if l < r {
- m := l + (r - l) / 2
- internal_sort(a, l, m)
- internal_sort(a, m+1, r)
- merge(a, l, m, r)
- }
- }
- internal_sort(array, 0, len(array)-1)
- }
- // @(deprecated="use sort.sort or slice.sort_by")
- heap_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
- sift_proc :: proc(a: A, pi: int, n: int, f: proc(T, T) -> int) #no_bounds_check {
- p := pi
- v := a[p]
- m := p*2 + 1
- for m <= n {
- if (m < n) && f(a[m+1], a[m]) > 0 {
- m += 1
- }
- if f(v, a[m]) >= 0 {
- break
- }
- a[p] = a[m]
- p = m
- m += m+1
- a[p] = v
- }
- }
- n := len(array)
- if n == 0 {
- return
- }
- for i := n/2; i >= 0; i -= 1 {
- sift_proc(array, i, n-1, f)
- }
- for i := n-1; i >= 1; i -= 1 {
- array[0], array[i] = array[i], array[0]
- sift_proc(array, 0, i-1, f)
- }
- }
- // @(deprecated="use sort.sort_slice or slice.sort")
- heap_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
- sift :: proc(a: A, pi: int, n: int) #no_bounds_check {
- p := pi
- v := a[p]
- m := p*2 + 1
- for m <= n {
- if (m < n) && (a[m+1] > a[m]) {
- m += 1
- }
- if v >= a[m] {
- break
- }
- a[p] = a[m]
- p = m
- m += m+1
- a[p] = v
- }
- }
- n := len(array)
- if n == 0 {
- return
- }
- for i := n/2; i >= 0; i -= 1 {
- sift(array, i, n-1)
- }
- for i := n-1; i >= 1; i -= 1 {
- array[0], array[i] = array[i], array[0]
- sift(array, 0, i-1)
- }
- }
- compare_bools :: proc(a, b: bool) -> int {
- switch {
- case !a && b: return -1
- case a && !b: return +1
- }
- return 0
- }
- compare_ints :: proc(a, b: int) -> int {
- switch delta := a - b; {
- case delta < 0: return -1
- case delta > 0: return +1
- }
- return 0
- }
- compare_uints :: proc(a, b: uint) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_u8s :: proc(a, b: u8) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_u16s :: proc(a, b: u16) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_u32s :: proc(a, b: u32) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_u64s :: proc(a, b: u64) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_i8s :: proc(a, b: i8) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_i16s :: proc(a, b: i16) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_i32s :: proc(a, b: i32) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_i64s :: proc(a, b: i64) -> int {
- switch {
- case a < b: return -1
- case a > b: return +1
- }
- return 0
- }
- compare_f32s :: proc(a, b: f32) -> int {
- switch delta := a - b; {
- case delta < 0: return -1
- case delta > 0: return +1
- }
- return 0
- }
- compare_f64s :: proc(a, b: f64) -> int {
- switch delta := a - b; {
- case delta < 0: return -1
- case delta > 0: return +1
- }
- return 0
- }
- compare_strings :: proc(a, b: string) -> int {
- x := transmute(mem.Raw_String)a
- y := transmute(mem.Raw_String)b
-
- ret := mem.compare_byte_ptrs(x.data, y.data, min(x.len, y.len))
- if ret == 0 && x.len != y.len {
- return -1 if x.len < y.len else +1
- }
- return ret
- }
|