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/dijkstras-algorithm/dijkstras-algorithm.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 / dijkstras-algorithm / dijkstras-algorithm.om` 143 lines ยท 2557 bytes Raw Blame History 1 note Rosetta Code task "Dijkstra's algorithm": https://rosettacode.org/wiki/Dijkstra's_algorithm 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 tarrow " -> " 8 text tcost " cost " 9 text tpath " path" 10 text tnone " unreachable" 11 global wt long 36 12 global dist long 6 13 global prev ulong 6 14 global done uchar 6 15 global stk ulong 6 16 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 17 fn emit a view uchar * to void 18 o = ulong 0 19 while lt o len a bound 4096 20 k = write 1 a o sub len a o 21 if le k 0 22 exit 1 23 end 24 o = add o ulong k 25 end 26 end 27 note emitu writes n in decimal; 20 digits hold every ulong 28 fn emitu n ulong to void 29 d = new uchar 20 0 30 i = ulong 20 31 v = n 32 while true bound 20 33 i = sub i 1 34 put d i uchar add rem v 10 48 35 v = div v 10 36 if eq v 0 37 break 38 end 39 end 40 emit part d i sub 20 i 41 end 42 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 43 fn emiti n long to void 44 if lt n 0 45 emit minus 46 emitu add bitcast ulong not n 1 47 else 48 emitu ulong n 49 end 50 end 51 note emitb writes the single byte c 52 fn emitb c ulong to void 53 d = new uchar 1 0 54 put d 0 uchar c 55 emit d 56 end 57 note edge records a directed edge u -> v of weight w; vertices a..f are 0..5, weight 0 means no edge 58 fn edge u ulong v ulong w long to void 59 put wt add mul u 6 v w 60 end 61 note dijkstra from s: repeatedly settle the unsettled vertex of least known distance (-1 is infinity) 62 note and relax its out-edges; 6 rounds settle every reachable vertex 63 fn dijkstra s ulong to void 64 for i ulong 6 65 put dist i -1 66 put prev i 6 67 put done i 0 68 end 69 put dist s 0 70 for r ulong 6 71 u = ulong 6 72 for i ulong 6 73 if and eq get done i 0 ge get dist i 0 74 if eq u 6 75 u = i 76 else 77 if lt get dist i get dist u 78 u = i 79 end 80 end 81 end 82 end 83 if eq u 6 84 ret 85 end 86 put done u 1 87 for v ulong 6 88 relax u v 89 end 90 end 91 end 92 fn relax u ulong v ulong to void 93 w = get wt add mul u 6 v 94 if gt w 0 95 d = add get dist u w 96 if or lt get dist v 0 lt d get dist v 97 put dist v d 98 put prev v u 99 end 100 end 101 end 102 note route prints the path to t by following prev back to the source through a small stack 103 fn route t ulong to void 104 k = ulong 0 105 v = t 106 while lt v 6 bound 6 107 put stk k v 108 k = add k 1 109 v = get prev v 110 end 111 emit tpath 112 for x ulong k 113 emit sp 114 emitb add 97 get stk sub sub k x 1 115 end 116 end 117 fn main to int 118 edge 0 1 7 119 edge 0 2 9 120 edge 0 5 14 121 edge 1 2 10 122 edge 1 3 15 123 edge 2 3 11 124 edge 2 5 2 125 edge 3 4 6 126 edge 4 5 9 127 dijkstra 0 128 for t ulong 5 129 v = add t 1 130 emitb 97 131 emit tarrow 132 emitb add 97 v 133 if lt get dist v 0 134 emit tnone 135 else 136 emit tcost 137 emiti get dist v 138 route v 139 end 140 emit nl 141 end 142 ret 0 143 end