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/tasks/topological-sort/topological-sort.om # 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 / tasks / topological-sort / topological-sort.om` 161 lines ยท 3371 bytes Raw Blame History 1 note Rosetta Code task "Topological sort": https://rosettacode.org/wiki/Topological_sort 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 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;" 8 text tcyc "un-orderable:" 9 global nst ulong 32 10 global nln ulong 32 11 global dep uchar 1024 12 global indeg ulong 32 13 global sent uchar 32 14 global nn 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 data lists one library per ;-terminated group: the library, then what it depends on; a library 42 note that names itself is ignored, as the task asks. Names are kept as (start, length) in data. 43 note same says whether the names at (a, la) and (b, lb) in data are the same bytes 44 fn same a ulong la ulong b ulong lb ulong to bool 45 if ne la lb 46 ret false 47 end 48 for i ulong la 49 if ne get data add a i get data add b i 50 ret false 51 end 52 end 53 ret true 54 end 55 note before says whether name i sorts before name j, comparing bytes as C's strcmp does 56 fn before i ulong j ulong to bool 57 a = get nst i 58 b = get nst j 59 la = get nln i 60 lb = get nln j 61 for k ulong la 62 if ge k lb 63 ret false 64 end 65 if ne get data add a k get data add b k 66 ret lt get data add a k get data add b k 67 end 68 end 69 ret lt la lb 70 end 71 note intern returns the number of the name at (s, l), adding it to the table if it is new 72 fn intern s ulong l ulong to ulong 73 n = get nn 0 74 for i ulong n 75 if same get nst i get nln i s l 76 ret i 77 end 78 end 79 put nst n s 80 put nln n l 81 put nn 0 add n 1 82 ret n 83 end 84 note load reads every group: the first word is the library u, each later word v adds the edge v -> u 85 fn load to void 86 first = true 87 u = ulong 0 88 st = ulong 0 89 for i ulong len data 90 c = get data i 91 if or eq c 32 eq c 59 92 if gt i st 93 v = intern st sub i st 94 if first 95 u = v 96 first = false 97 else 98 addedge v u 99 end 100 end 101 st = add i 1 102 if eq c 59 103 first = true 104 end 105 end 106 end 107 end 108 fn addedge v ulong u ulong to void 109 if ne v u 110 if eq get dep add mul v 32 u 0 111 put dep add mul v 32 u 1 112 put indeg u add get indeg u 1 113 end 114 end 115 end 116 fn name i ulong to void 117 emit part data get nst i get nln i 118 end 119 note Kahn's algorithm: output the smallest ready name (in-degree 0), then release what depends on it; 120 note each round outputs one name, so nn rounds suffice; names never output are part of a cycle 121 fn main to int 122 load 123 n = get nn 0 124 for r ulong n 125 b = n 126 for i ulong n 127 if and eq get sent i 0 eq get indeg i 0 128 if eq b n 129 b = i 130 else 131 if before i b 132 b = i 133 end 134 end 135 end 136 end 137 if eq b n 138 break 139 end 140 put sent b 1 141 if gt r 0 142 emit sp 143 end 144 name b 145 for j ulong n 146 if eq get dep add mul b 32 j 1 147 put indeg j sub get indeg j 1 148 end 149 end 150 end 151 emit nl 152 emit tcyc 153 for i ulong n 154 if eq get sent i 0 155 emit sp 156 name i 157 end 158 end 159 emit nl 160 ret 0 161 end