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/drafts/rosettacode/priority-queue.wiki
# 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 / drafts / rosettacode / priority-queue.wiki`
139 lines ยท 2591 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tins "insert "
10 text tpop "pop "
11 text tdata "3 Clear drains|4 Feed cat|5 Make tea|1 Solve RC tasks|2 Tax return"
12 global heap ulong 16
13 global pri ulong 16
14 global hn ulong 1
15 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16 fn emit a view uchar * to void
17 o = ulong 0
18 while lt o len a bound 4096
19 k = write 1 a o sub len a o
20 if le k 0
21 exit 1
22 end
23 o = add o ulong k
24 end
25 end
26 note emitu writes n in decimal; 20 digits hold every ulong
27 fn emitu n ulong to void
28 d = new uchar 20 0
29 i = ulong 20
30 v = n
31 while true bound 20
32 i = sub i 1
33 put d i uchar add rem v 10 48
34 v = div v 10
35 if eq v 0
36 break
37 end
38 end
39 emit part d i sub 20 i
40 end
41 note a binary min-heap of task numbers keyed by priority: heap[0] has the smallest pri; the five tasks
42 note are the |-separated entries of tdata, each a priority digit and a name
43 fn entry k ulong to void
44 f = ulong 0
45 st = ulong 0
46 for i ulong len tdata
47 if eq get tdata i 124
48 if eq f k
49 emit part tdata st sub i st
50 ret
51 end
52 f = add f 1
53 st = add i 1
54 end
55 end
56 emit part tdata st sub len tdata st
57 end
58 fn swap i ulong j ulong to void
59 t = get heap i
60 put heap i get heap j
61 put heap j t
62 end
63 fn less i ulong j ulong to bool
64 ret lt get pri get heap i get pri get heap j
65 end
66 note insert sifts the new task up; the index halves each step, so 16 steps bound it
67 fn insert k ulong p ulong to void
68 put pri k p
69 n = get hn 0
70 put heap n k
71 put hn 0 add n 1
72 i = n
73 while gt i 0 bound 16
74 u = shr sub i 1 1
75 if not less i u
76 break
77 end
78 swap i u
79 i = u
80 end
81 emit tins
82 entry k
83 emit nl
84 end
85 note popmin moves the last task to the root and sifts it down; the index doubles each step
86 fn popmin to void
87 n = sub get hn 0 1
88 k = get heap 0
89 put heap 0 get heap n
90 put hn 0 n
91 i = ulong 0
92 while lt add mul i 2 1 n bound 16
93 c = add mul i 2 1
94 if lt add c 1 n
95 if less add c 1 c
96 c = add c 1
97 end
98 end
99 if not less c i
100 break
101 end
102 swap i c
103 i = c
104 end
105 emit tpop
106 entry k
107 emit nl
108 end
109 fn main to int
110 st = ulong 0
111 k = ulong 0
112 for i ulong len tdata
113 if eq i st
114 insert k ulong sub get tdata i 48
115 k = add k 1
116 end
117 if eq get tdata i 124
118 st = add i 1
119 end
120 end
121 for x ulong k
122 popmin
123 end
124 ret 0
125 end
126
127 {{out}}
128
129 insert 3 Clear drains
130 insert 4 Feed cat
131 insert 5 Make tea
132 insert 1 Solve RC tasks
133 insert 2 Tax return
134 pop 1 Solve RC tasks
135 pop 2 Tax return
136 pop 3 Clear drains
137 pop 4 Feed cat
138 pop 5 Make tea
139