interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / priority-queue / priority-queue.om

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