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/priority-queue/priority-queue.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 / priority-queue / priority-queue.om` 123 lines ยท 2270 bytes Raw Blame History 1 note Rosetta Code task "Priority queue": https://rosettacode.org/wiki/Priority_queue 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 tins "insert " 8 text tpop "pop " 9 text tdata "3 Clear drains|4 Feed cat|5 Make tea|1 Solve RC tasks|2 Tax return" 10 global heap ulong 16 11 global pri ulong 16 12 global hn ulong 1 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 a binary min-heap of task numbers keyed by priority: heap[0] has the smallest pri; the five tasks 40 note are the |-separated entries of tdata, each a priority digit and a name 41 fn entry k ulong to void 42 f = ulong 0 43 st = ulong 0 44 for i ulong len tdata 45 if eq get tdata i 124 46 if eq f k 47 emit part tdata st sub i st 48 ret 49 end 50 f = add f 1 51 st = add i 1 52 end 53 end 54 emit part tdata st sub len tdata st 55 end 56 fn swap i ulong j ulong to void 57 t = get heap i 58 put heap i get heap j 59 put heap j t 60 end 61 fn less i ulong j ulong to bool 62 ret lt get pri get heap i get pri get heap j 63 end 64 note insert sifts the new task up; the index halves each step, so 16 steps bound it 65 fn insert k ulong p ulong to void 66 put pri k p 67 n = get hn 0 68 put heap n k 69 put hn 0 add n 1 70 i = n 71 while gt i 0 bound 16 72 u = shr sub i 1 1 73 if not less i u 74 break 75 end 76 swap i u 77 i = u 78 end 79 emit tins 80 entry k 81 emit nl 82 end 83 note popmin moves the last task to the root and sifts it down; the index doubles each step 84 fn popmin to void 85 n = sub get hn 0 1 86 k = get heap 0 87 put heap 0 get heap n 88 put hn 0 n 89 i = ulong 0 90 while lt add mul i 2 1 n bound 16 91 c = add mul i 2 1 92 if lt add c 1 n 93 if less add c 1 c 94 c = add c 1 95 end 96 end 97 if not less c i 98 break 99 end 100 swap i c 101 i = c 102 end 103 emit tpop 104 entry k 105 emit nl 106 end 107 fn main to int 108 st = ulong 0 109 k = ulong 0 110 for i ulong len tdata 111 if eq i st 112 insert k ulong sub get tdata i 48 113 k = add k 1 114 end 115 if eq get tdata i 124 116 st = add i 1 117 end 118 end 119 for x ulong k 120 popmin 121 end 122 ret 0 123 end