sort.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. package sort
  2. import "core:mem"
  3. import _slice "core:slice"
  4. import "base:intrinsics"
  5. _ :: intrinsics
  6. _ :: _slice
  7. ORD :: intrinsics.type_is_ordered
  8. Interface :: struct {
  9. len: proc(it: Interface) -> int,
  10. less: proc(it: Interface, i, j: int) -> bool,
  11. swap: proc(it: Interface, i, j: int),
  12. collection: rawptr,
  13. }
  14. // sort sorts an Interface
  15. // This sort is not guaranteed to be stable
  16. sort :: proc(it: Interface) {
  17. max_depth :: proc(n: int) -> int { // 2*ceil(log2(n+1))
  18. depth: int
  19. for i := n; i > 0; i >>= 1 {
  20. depth += 1
  21. }
  22. return depth * 2
  23. }
  24. n := it->len()
  25. _quick_sort(it, 0, n, max_depth(n))
  26. }
  27. @(deprecated="use slice.sort")
  28. slice :: proc(array: $T/[]$E) where ORD(E) {
  29. _slice.sort(array)
  30. // s := array;
  31. // sort(slice_interface(&s));
  32. }
  33. slice_interface :: proc(s: ^$T/[]$E) -> Interface where ORD(E) {
  34. return Interface{
  35. collection = rawptr(s),
  36. len = proc(it: Interface) -> int {
  37. s := (^T)(it.collection)
  38. return len(s^)
  39. },
  40. less = proc(it: Interface, i, j: int) -> bool {
  41. s := (^T)(it.collection)
  42. return s[i] < s[j]
  43. },
  44. swap = proc(it: Interface, i, j: int) {
  45. s := (^T)(it.collection)
  46. s[i], s[j] = s[j], s[i]
  47. },
  48. }
  49. }
  50. reverse_interface :: proc(it: ^Interface) -> Interface {
  51. return Interface{
  52. collection = it,
  53. len = proc(rit: Interface) -> int {
  54. it := (^Interface)(rit.collection)
  55. return it.len(it^)
  56. },
  57. less = proc(rit: Interface, i, j: int) -> bool {
  58. it := (^Interface)(rit.collection)
  59. return it.less(it^, j, i) // reverse parameters
  60. },
  61. swap = proc(rit: Interface, i, j: int) {
  62. it := (^Interface)(rit.collection)
  63. it.swap(it^, i, j)
  64. },
  65. }
  66. }
  67. reverse_sort :: proc(it: Interface) {
  68. it := it
  69. sort(reverse_interface(&it))
  70. }
  71. @(deprecated="use slice.reverse")
  72. reverse_slice :: proc(array: $T/[]$E) where ORD(E) {
  73. _slice.reverse(array)
  74. /*
  75. s := array;
  76. sort(Interface{
  77. collection = rawptr(&s),
  78. len = proc(it: Interface) -> int {
  79. s := (^T)(it.collection);
  80. return len(s^);
  81. },
  82. less = proc(it: Interface, i, j: int) -> bool {
  83. s := (^T)(it.collection);
  84. return s[j] < s[i]; // manual set up
  85. },
  86. swap = proc(it: Interface, i, j: int) {
  87. s := (^T)(it.collection);
  88. s[i], s[j] = s[j], s[i];
  89. },
  90. });
  91. */
  92. }
  93. is_sorted :: proc(it: Interface) -> bool {
  94. n := it->len()
  95. for i := n-1; i > 0; i -= 1 {
  96. if it->less(i, i-1) {
  97. return false
  98. }
  99. }
  100. return true
  101. }
  102. swap_range :: proc(it: Interface, a, b, n: int) {
  103. for i in 0..<n {
  104. it->swap(a+i, b+i)
  105. }
  106. }
  107. rotate :: proc(it: Interface, a, m, b: int) {
  108. i := m - a
  109. j := b - m
  110. for i != j {
  111. if i > j {
  112. swap_range(it, m-i, m, j)
  113. i -= j
  114. } else {
  115. swap_range(it, m-i, m+j-1, i)
  116. j -= 1
  117. }
  118. }
  119. swap_range(it, m-i, m, i)
  120. }
  121. @(private)
  122. _quick_sort :: proc(it: Interface, a, b, max_depth: int) {
  123. median3 :: proc(it: Interface, m1, m0, m2: int) {
  124. if it->less(m1, m0) {
  125. it->swap(m1, m0)
  126. }
  127. if it->less(m2, m1) {
  128. it->swap(m2, m1)
  129. if it->less(m1, m0) {
  130. it->swap(m1, m0)
  131. }
  132. }
  133. }
  134. do_pivot :: proc(it: Interface, lo, hi: int) -> (midlo, midhi: int) {
  135. m := int(uint(lo+hi)>>1)
  136. if hi-lo > 40 {
  137. s := (hi-lo)/8
  138. median3(it, lo, lo+s, lo+s*2)
  139. median3(it, m, m-s, m+s)
  140. median3(it, hi-1, hi-1-s, hi-1-s*2)
  141. }
  142. median3(it, lo, m, hi-1)
  143. pivot := lo
  144. a, c := lo+1, hi-1
  145. for ; a < c && it->less(a, pivot); a += 1 {
  146. }
  147. b := a
  148. for {
  149. for ; b < c && !it->less(pivot, b); b += 1 { // data[b] <= pivot
  150. }
  151. for ; b < c && it->less(pivot, c-1); c -=1 { // data[c-1] > pivot
  152. }
  153. if b >= c {
  154. break
  155. }
  156. it->swap(b, c-1)
  157. b += 1
  158. c -= 1
  159. }
  160. protect := hi-c < 5
  161. if !protect && hi-c < (hi-lo)/4 {
  162. dups := 0
  163. if !it->less(pivot, hi-1) {
  164. it->swap(c, hi-1)
  165. c += 1
  166. dups += 1
  167. }
  168. if !it->less(b-1, pivot) {
  169. b -= 1
  170. dups += 1
  171. }
  172. if !it->less(m, pivot) {
  173. it->swap(m, b-1)
  174. b -= 1
  175. dups += 1
  176. }
  177. protect = dups > 1
  178. }
  179. if protect {
  180. for {
  181. for ; a < b && !it->less(b-1, pivot); b -= 1 {
  182. }
  183. for ; a < b && it->less(a, pivot); a += 1 {
  184. }
  185. if a >= b {
  186. break
  187. }
  188. it->swap(a, b-1)
  189. a += 1
  190. b -= 1
  191. }
  192. }
  193. it->swap(pivot, b-1)
  194. return b-1, c
  195. }
  196. heap_sort :: proc(it: Interface, a, b: int) {
  197. sift_down :: proc(it: Interface, lo, hi, first: int) {
  198. root := lo
  199. for {
  200. child := 2*root + 1
  201. if child >= hi {
  202. break
  203. }
  204. if child+1 < hi && it->less(first+child, first+child+1) {
  205. child += 1
  206. }
  207. if !it->less(first+root, first+child) {
  208. return
  209. }
  210. it->swap(first+root, first+child)
  211. root = child
  212. }
  213. }
  214. first, lo, hi := a, 0, b-a
  215. for i := (hi-1)/2; i >= 0; i -= 1 {
  216. sift_down(it, i, hi, first)
  217. }
  218. for i := hi-1; i >= 0; i -= 1 {
  219. it->swap(first, first+i)
  220. sift_down(it, lo, i, first)
  221. }
  222. }
  223. a, b, max_depth := a, b, max_depth
  224. for b-a > 12 { // only use shell sort for lengths <= 12
  225. if max_depth == 0 {
  226. heap_sort(it, a, b)
  227. return
  228. }
  229. max_depth -= 1
  230. mlo, mhi := do_pivot(it, a, b)
  231. if mlo-a < b-mhi {
  232. _quick_sort(it, a, mlo, max_depth)
  233. a = mhi
  234. } else {
  235. _quick_sort(it, mhi, b, max_depth)
  236. b = mlo
  237. }
  238. }
  239. if b-a > 1 {
  240. // Shell short with gap 6
  241. for i in a+6..<b {
  242. if it->less(i, i-6) {
  243. it->swap(i, i-6)
  244. }
  245. }
  246. _insertion_sort(it, a, b)
  247. }
  248. }
  249. @(private)
  250. _insertion_sort :: proc(it: Interface, a, b: int) {
  251. for i in a+1..<b {
  252. for j := i; j > a && it->less(j, j-1); j -= 1 {
  253. it->swap(j, j-1)
  254. }
  255. }
  256. }
  257. // @(deprecated="use sort.sort or slice.sort_by")
  258. bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
  259. assert(f != nil)
  260. count := len(array)
  261. init_j, last_j := 0, count-1
  262. for {
  263. init_swap, prev_swap := -1, -1
  264. for j in init_j..<last_j {
  265. if f(array[j], array[j+1]) > 0 {
  266. array[j], array[j+1] = array[j+1], array[j]
  267. prev_swap = j
  268. if init_swap == -1 {
  269. init_swap = j
  270. }
  271. }
  272. }
  273. if prev_swap == -1 {
  274. return
  275. }
  276. init_j = max(init_swap-1, 0)
  277. last_j = prev_swap
  278. }
  279. }
  280. // @(deprecated="use sort.sort_slice or slice.sort")
  281. bubble_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
  282. count := len(array)
  283. init_j, last_j := 0, count-1
  284. for {
  285. init_swap, prev_swap := -1, -1
  286. for j in init_j..<last_j {
  287. if array[j] > array[j+1] {
  288. array[j], array[j+1] = array[j+1], array[j]
  289. prev_swap = j
  290. if init_swap == -1 {
  291. init_swap = j
  292. }
  293. }
  294. }
  295. if prev_swap == -1 {
  296. return
  297. }
  298. init_j = max(init_swap-1, 0)
  299. last_j = prev_swap
  300. }
  301. }
  302. // @(deprecated="use sort.sort or slice.sort_by")
  303. quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
  304. assert(f != nil)
  305. a := array
  306. n := len(a)
  307. if n < 2 {
  308. return
  309. }
  310. p := a[n/2]
  311. i, j := 0, n-1
  312. loop: for {
  313. for f(a[i], p) < 0 { i += 1 }
  314. for f(p, a[j]) < 0 { j -= 1 }
  315. if i >= j {
  316. break loop
  317. }
  318. a[i], a[j] = a[j], a[i]
  319. i += 1
  320. j -= 1
  321. }
  322. quick_sort_proc(a[0:i], f)
  323. quick_sort_proc(a[i:n], f)
  324. }
  325. // @(deprecated="use sort.sort_slice or slice.sort")
  326. quick_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
  327. a := array
  328. n := len(a)
  329. if n < 2 {
  330. return
  331. }
  332. p := a[n/2]
  333. i, j := 0, n-1
  334. loop: for {
  335. for a[i] < p { i += 1 }
  336. for p < a[j] { j -= 1 }
  337. if i >= j {
  338. break loop
  339. }
  340. a[i], a[j] = a[j], a[i]
  341. i += 1
  342. j -= 1
  343. }
  344. quick_sort(a[0:i])
  345. quick_sort(a[i:n])
  346. }
  347. _log2 :: proc(x: int) -> int {
  348. res := 0
  349. for n := x; n != 0; n >>= 1 {
  350. res += 1
  351. }
  352. return res
  353. }
  354. // @(deprecated="use sort.sort or slice.sort_by")
  355. merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
  356. merge :: proc(a: A, start, mid, end: int, f: proc(T, T) -> int) {
  357. s, m := start, mid
  358. s2 := m + 1
  359. if f(a[m], a[s2]) <= 0 {
  360. return
  361. }
  362. for s <= m && s2 <= end {
  363. if f(a[s], a[s2]) <= 0 {
  364. s += 1
  365. } else {
  366. v := a[s2]
  367. i := s2
  368. for i != s {
  369. a[i] = a[i-1]
  370. i -= 1
  371. }
  372. a[s] = v
  373. s += 1
  374. m += 1
  375. s2 += 1
  376. }
  377. }
  378. }
  379. internal_sort :: proc(a: A, l, r: int, f: proc(T, T) -> int) {
  380. if l < r {
  381. m := l + (r - l) / 2
  382. internal_sort(a, l, m, f)
  383. internal_sort(a, m+1, r, f)
  384. merge(a, l, m, r, f)
  385. }
  386. }
  387. internal_sort(array, 0, len(array)-1, f)
  388. }
  389. // @(deprecated="use sort.sort_slice or slice.sort")
  390. merge_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
  391. merge :: proc(a: A, start, mid, end: int) {
  392. s, m := start, mid
  393. s2 := m + 1
  394. if a[m] <= a[s2] {
  395. return
  396. }
  397. for s <= m && s2 <= end {
  398. if a[s] <= a[s2] {
  399. s += 1
  400. } else {
  401. v := a[s2]
  402. i := s2
  403. for i != s {
  404. a[i] = a[i-1]
  405. i -= 1
  406. }
  407. a[s] = v
  408. s += 1
  409. m += 1
  410. s2 += 1
  411. }
  412. }
  413. }
  414. internal_sort :: proc(a: A, l, r: int) {
  415. if l < r {
  416. m := l + (r - l) / 2
  417. internal_sort(a, l, m)
  418. internal_sort(a, m+1, r)
  419. merge(a, l, m, r)
  420. }
  421. }
  422. internal_sort(array, 0, len(array)-1)
  423. }
  424. // @(deprecated="use sort.sort or slice.sort_by")
  425. heap_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
  426. sift_proc :: proc(a: A, pi: int, n: int, f: proc(T, T) -> int) #no_bounds_check {
  427. p := pi
  428. v := a[p]
  429. m := p*2 + 1
  430. for m <= n {
  431. if (m < n) && f(a[m+1], a[m]) > 0 {
  432. m += 1
  433. }
  434. if f(v, a[m]) >= 0 {
  435. break
  436. }
  437. a[p] = a[m]
  438. p = m
  439. m += m+1
  440. a[p] = v
  441. }
  442. }
  443. n := len(array)
  444. if n == 0 {
  445. return
  446. }
  447. for i := n/2; i >= 0; i -= 1 {
  448. sift_proc(array, i, n-1, f)
  449. }
  450. for i := n-1; i >= 1; i -= 1 {
  451. array[0], array[i] = array[i], array[0]
  452. sift_proc(array, 0, i-1, f)
  453. }
  454. }
  455. // @(deprecated="use sort.sort_slice or slice.sort")
  456. heap_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
  457. sift :: proc(a: A, pi: int, n: int) #no_bounds_check {
  458. p := pi
  459. v := a[p]
  460. m := p*2 + 1
  461. for m <= n {
  462. if (m < n) && (a[m+1] > a[m]) {
  463. m += 1
  464. }
  465. if v >= a[m] {
  466. break
  467. }
  468. a[p] = a[m]
  469. p = m
  470. m += m+1
  471. a[p] = v
  472. }
  473. }
  474. n := len(array)
  475. if n == 0 {
  476. return
  477. }
  478. for i := n/2; i >= 0; i -= 1 {
  479. sift(array, i, n-1)
  480. }
  481. for i := n-1; i >= 1; i -= 1 {
  482. array[0], array[i] = array[i], array[0]
  483. sift(array, 0, i-1)
  484. }
  485. }
  486. compare_bools :: proc(a, b: bool) -> int {
  487. switch {
  488. case !a && b: return -1
  489. case a && !b: return +1
  490. }
  491. return 0
  492. }
  493. compare_ints :: proc(a, b: int) -> int {
  494. switch delta := a - b; {
  495. case delta < 0: return -1
  496. case delta > 0: return +1
  497. }
  498. return 0
  499. }
  500. compare_uints :: proc(a, b: uint) -> int {
  501. switch {
  502. case a < b: return -1
  503. case a > b: return +1
  504. }
  505. return 0
  506. }
  507. compare_u8s :: proc(a, b: u8) -> int {
  508. switch {
  509. case a < b: return -1
  510. case a > b: return +1
  511. }
  512. return 0
  513. }
  514. compare_u16s :: proc(a, b: u16) -> int {
  515. switch {
  516. case a < b: return -1
  517. case a > b: return +1
  518. }
  519. return 0
  520. }
  521. compare_u32s :: proc(a, b: u32) -> int {
  522. switch {
  523. case a < b: return -1
  524. case a > b: return +1
  525. }
  526. return 0
  527. }
  528. compare_u64s :: proc(a, b: u64) -> int {
  529. switch {
  530. case a < b: return -1
  531. case a > b: return +1
  532. }
  533. return 0
  534. }
  535. compare_i8s :: proc(a, b: i8) -> int {
  536. switch {
  537. case a < b: return -1
  538. case a > b: return +1
  539. }
  540. return 0
  541. }
  542. compare_i16s :: proc(a, b: i16) -> int {
  543. switch {
  544. case a < b: return -1
  545. case a > b: return +1
  546. }
  547. return 0
  548. }
  549. compare_i32s :: proc(a, b: i32) -> int {
  550. switch {
  551. case a < b: return -1
  552. case a > b: return +1
  553. }
  554. return 0
  555. }
  556. compare_i64s :: proc(a, b: i64) -> int {
  557. switch {
  558. case a < b: return -1
  559. case a > b: return +1
  560. }
  561. return 0
  562. }
  563. compare_f32s :: proc(a, b: f32) -> int {
  564. switch delta := a - b; {
  565. case delta < 0: return -1
  566. case delta > 0: return +1
  567. }
  568. return 0
  569. }
  570. compare_f64s :: proc(a, b: f64) -> int {
  571. switch delta := a - b; {
  572. case delta < 0: return -1
  573. case delta > 0: return +1
  574. }
  575. return 0
  576. }
  577. compare_strings :: proc(a, b: string) -> int {
  578. x := transmute(mem.Raw_String)a
  579. y := transmute(mem.Raw_String)b
  580. ret := mem.compare_byte_ptrs(x.data, y.data, min(x.len, y.len))
  581. if ret == 0 && x.len != y.len {
  582. return -1 if x.len < y.len else +1
  583. }
  584. return ret
  585. }