interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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