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/strand-sort/strand-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 / strand-sort / strand-sort.om` 167 lines ยท 2922 bytes Raw Blame History 1 note Rosetta Code task "Sorting algorithms/Strand sort": https://rosettacode.org/wiki/Sorting_algorithms/Strand_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 1 4 2 0 9 6 3 8 7 -2 11" 8 global vals long 16 9 global rest long 16 10 global strand long 16 11 global res long 16 12 global mrg long 16 13 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 14 fn emit a view uchar * to void 15 o = ulong 0 16 while lt o len a bound 4096 17 k = write 1 a o sub len a o 18 if le k 0 19 exit 1 20 end 21 o = add o ulong k 22 end 23 end 24 note emitu writes n in decimal; 20 digits hold every ulong 25 fn emitu n ulong to void 26 d = new uchar 20 0 27 i = ulong 20 28 v = n 29 while true bound 20 30 i = sub i 1 31 put d i uchar add rem v 10 48 32 v = div v 10 33 if eq v 0 34 break 35 end 36 end 37 emit part d i sub 20 i 38 end 39 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 40 fn emiti n long to void 41 if lt n 0 42 emit minus 43 emitu add bitcast ulong not n 1 44 else 45 emitu ulong n 46 end 47 end 48 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 49 note returns how many; one pass over s, so the loop is bounded by len s 50 fn parse s view uchar * to ulong 51 n = ulong 0 52 v = long 0 53 sg = long 1 54 dig = false 55 for i ulong len s 56 c = get s i 57 if eq c 45 58 sg = long -1 59 else 60 if and ge c 48 le c 57 61 v = add mul v 10 long sub c 48 62 dig = true 63 else 64 if dig 65 put vals n mul sg v 66 n = add n 1 67 end 68 v = long 0 69 sg = long 1 70 dig = false 71 end 72 end 73 end 74 if dig 75 put vals n mul sg v 76 n = add n 1 77 end 78 ret n 79 end 80 note show writes vals[0..n-1] separated by spaces, then a newline 81 fn show n ulong to void 82 for i ulong n 83 if gt i 0 84 emit sp 85 end 86 emiti get vals i 87 end 88 emit nl 89 end 90 note pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the 91 note front of rest, and returns the strand length 92 fn pull m ulong to ulong 93 s = ulong 0 94 k = ulong 0 95 for i ulong m 96 v = get rest i 97 take = false 98 if eq s 0 99 take = true 100 else 101 if ge v get strand sub s 1 102 take = true 103 end 104 end 105 if take 106 put strand s v 107 s = add s 1 108 else 109 put rest k v 110 k = add k 1 111 end 112 end 113 ret s 114 end 115 note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length 116 fn mergein r ulong s ulong to ulong 117 i = ulong 0 118 j = ulong 0 119 k = ulong 0 120 while lt k add r s bound 32 121 left = false 122 if ge j s 123 left = true 124 else 125 if lt i r 126 if le get res i get strand j 127 left = true 128 end 129 end 130 end 131 if left 132 put mrg k get res i 133 i = add i 1 134 else 135 put mrg k get strand j 136 j = add j 1 137 end 138 k = add k 1 139 end 140 for x ulong k 141 put res x get mrg x 142 end 143 ret k 144 end 145 note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice 146 fn stsort n ulong to void 147 for i ulong n 148 put rest i get vals i 149 end 150 m = n 151 r = ulong 0 152 while gt m 0 bound 16 153 s = pull m 154 m = sub m s 155 r = mergein r s 156 end 157 for i ulong n 158 put vals i get res i 159 end 160 end 161 fn main to int 162 n = parse data 163 show n 164 stsort n 165 show n 166 ret 0 167 end