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