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 / topological-sort.wiki

169 lines · 3613 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 data "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee;dw01 ieee dw01 dware gtech;dw02 ieee dw02 dware;dw03 std synopsys dware dw03 dw02 dw01 ieee gtech;dw04 dw04 ieee dw01 dware gtech;dw05 dw05 ieee dware;dw06 dw06 ieee dware;dw07 ieee dware;dware ieee dware;gtech ieee gtech;ramlib std ieee;std_cell_lib ieee std_cell_lib;synopsys;"
10text tcyc "un-orderable:"
11global nst ulong 32
12global nln ulong 32
13global dep uchar 1024
14global indeg ulong 32
15global sent uchar 32
16global nn ulong 1
17note emit writes every byte of a to stdout; a short write is retried, an error exits 1
18fn emit a view uchar * to void
19o = ulong 0
20while lt o len a bound 4096
21k = write 1 a o sub len a o
22if le k 0
23exit 1
24end
25o = add o ulong k
26end
27end
28note emitu writes n in decimal; 20 digits hold every ulong
29fn emitu n ulong to void
30d = new uchar 20 0
31i = ulong 20
32v = n
33while true bound 20
34i = sub i 1
35put d i uchar add rem v 10 48
36v = div v 10
37if eq v 0
38break
39end
40end
41emit part d i sub 20 i
42end
43note data lists one library per ;-terminated group: the library, then what it depends on; a library
44note that names itself is ignored, as the task asks. Names are kept as (start, length) in data.
45note same says whether the names at (a, la) and (b, lb) in data are the same bytes
46fn same a ulong la ulong b ulong lb ulong to bool
47if ne la lb
48ret false
49end
50for i ulong la
51if ne get data add a i get data add b i
52ret false
53end
54end
55ret true
56end
57note before says whether name i sorts before name j, comparing bytes as C's strcmp does
58fn before i ulong j ulong to bool
59a = get nst i
60b = get nst j
61la = get nln i
62lb = get nln j
63for k ulong la
64if ge k lb
65ret false
66end
67if ne get data add a k get data add b k
68ret lt get data add a k get data add b k
69end
70end
71ret lt la lb
72end
73note intern returns the number of the name at (s, l), adding it to the table if it is new
74fn intern s ulong l ulong to ulong
75n = get nn 0
76for i ulong n
77if same get nst i get nln i s l
78ret i
79end
80end
81put nst n s
82put nln n l
83put nn 0 add n 1
84ret n
85end
86note load reads every group: the first word is the library u, each later word v adds the edge v -> u
87fn load to void
88first = true
89u = ulong 0
90st = ulong 0
91for i ulong len data
92c = get data i
93if or eq c 32 eq c 59
94if gt i st
95v = intern st sub i st
96if first
97u = v
98first = false
99else
100addedge v u
101end
102end
103st = add i 1
104if eq c 59
105first = true
106end
107end
108end
109end
110fn addedge v ulong u ulong to void
111if ne v u
112if eq get dep add mul v 32 u 0
113put dep add mul v 32 u 1
114put indeg u add get indeg u 1
115end
116end
117end
118fn name i ulong to void
119emit part data get nst i get nln i
120end
121note Kahn's algorithm: output the smallest ready name (in-degree 0), then release what depends on it;
122note each round outputs one name, so nn rounds suffice; names never output are part of a cycle
123fn main to int
124load
125n = get nn 0
126for r ulong n
127b = n
128for i ulong n
129if and eq get sent i 0 eq get indeg i 0
130if eq b n
131b = i
132else
133if before i b
134b = i
135end
136end
137end
138end
139if eq b n
140break
141end
142put sent b 1
143if gt r 0
144emit sp
145end
146name b
147for j ulong n
148if eq get dep add mul b 32 j 1
149put indeg j sub get indeg j 1
150end
151end
152end
153emit nl
154emit tcyc
155for i ulong n
156if eq get sent i 0
157emit sp
158name i
159end
160end
161emit nl
162ret 0
163end
164</syntaxhighlight>
165{{out}}
166<pre>
167ieee dware dw02 dw05 dw06 dw07 gtech dw01 dw04 std ramlib std_cell_lib synopsys des_system_lib dw03
168un-orderable:
169</pre>