Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/bead-sort/bead-sort.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / bead-sort / bead-sort.om` 121 lines ยท 2407 bytes Raw Blame History 1 note Rosetta Code task "Sorting algorithms/Bead sort": https://rosettacode.org/wiki/Sorting_algorithms/Bead_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 "5 3 1 7 4 1 1 20 0 9" 8 global vals long 16 9 global grid uchar 512 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 the abacus has one row per value (at most 16) and one column per unit (at most 32): grid[r*32+c] 88 note holds a bead when c < vals[r]; the values here are 0 .. 20 89 fn bdsort n ulong to void 90 for r ulong n 91 for c ulong ulong get vals r 92 put grid add mul r 32 c 1 93 end 94 end 95 note gravity: each column's beads fall to the bottom rows 96 for c ulong 32 97 h = ulong 0 98 for r ulong n 99 h = add h ulong get grid add mul r 32 c 100 put grid add mul r 32 c 0 101 end 102 for x ulong h 103 put grid add mul sub sub n x 1 32 c 1 104 end 105 end 106 note row r now holds the r-th smallest value as its bead count, read top to bottom 107 for r ulong n 108 v = long 0 109 for c ulong 32 110 v = add v long get grid add mul r 32 c 111 end 112 put vals r v 113 end 114 end 115 fn main to int 116 n = parse data 117 show n 118 bdsort n 119 show n 120 ret 0 121 end