omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / counting-sort / counting-sort.om
| 1 | note Rosetta Code task "Sorting algorithms/Counting sort": https://rosettacode.org/wiki/Sorting_algorithms/Counting_sort |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text minus "-" |
| 7 | text data "7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8" |
| 8 | global vals long 16 |
| 9 | global cnt ulong 64 |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow |
| 37 | fn emiti n long to void |
| 38 | if lt n 0 |
| 39 | emit minus |
| 40 | emitu add bitcast ulong not n 1 |
| 41 | else |
| 42 | emitu ulong n |
| 43 | end |
| 44 | end |
| 45 | note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and |
| 46 | note returns how many; one pass over s, so the loop is bounded by len s |
| 47 | fn parse s view uchar * to ulong |
| 48 | n = ulong 0 |
| 49 | v = long 0 |
| 50 | sg = long 1 |
| 51 | dig = false |
| 52 | for i ulong len s |
| 53 | c = get s i |
| 54 | if eq c 45 |
| 55 | sg = long -1 |
| 56 | else |
| 57 | if and ge c 48 le c 57 |
| 58 | v = add mul v 10 long sub c 48 |
| 59 | dig = true |
| 60 | else |
| 61 | if dig |
| 62 | put vals n mul sg v |
| 63 | n = add n 1 |
| 64 | end |
| 65 | v = long 0 |
| 66 | sg = long 1 |
| 67 | dig = false |
| 68 | end |
| 69 | end |
| 70 | end |
| 71 | if dig |
| 72 | put vals n mul sg v |
| 73 | n = add n 1 |
| 74 | end |
| 75 | ret n |
| 76 | end |
| 77 | note show writes vals[0..n-1] separated by spaces, then a newline |
| 78 | fn show n ulong to void |
| 79 | for i ulong n |
| 80 | if gt i 0 |
| 81 | emit sp |
| 82 | end |
| 83 | emiti get vals i |
| 84 | end |
| 85 | emit nl |
| 86 | end |
| 87 | note cntsort counts each value in min..max (the range must fit the 64 counters), then rewrites vals |
| 88 | fn cntsort n ulong to void |
| 89 | lo = get vals 0 |
| 90 | hi = get vals 0 |
| 91 | for i ulong n |
| 92 | if lt get vals i lo |
| 93 | lo = get vals i |
| 94 | end |
| 95 | if gt get vals i hi |
| 96 | hi = get vals i |
| 97 | end |
| 98 | end |
| 99 | for i ulong n |
| 100 | c = ulong sub get vals i lo |
| 101 | put cnt c add get cnt c 1 |
| 102 | end |
| 103 | k = ulong 0 |
| 104 | for x ulong add ulong sub hi lo 1 |
| 105 | for y ulong get cnt x |
| 106 | put vals k add lo long x |
| 107 | k = add k 1 |
| 108 | end |
| 109 | end |
| 110 | end |
| 111 | fn main to int |
| 112 | n = parse data |
| 113 | show n |
| 114 | cntsort n |
| 115 | show n |
| 116 | ret 0 |
| 117 | end |