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/floyd-warshall-algorithm/floyd-warshall-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 / floyd-warshall-algorithm / floyd-warshall-algorithm.om` 134 lines ยท 2459 bytes Raw Blame History 1 note Rosetta Code task "Floyd-Warshall algorithm": https://rosettacode.org/wiki/Floyd-Warshall_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 thead "pair dist path\n" 8 text tarrow " -> " 9 global dist long 16 10 global nxt ulong 16 11 global has uchar 16 12 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 13 fn emit a view uchar * to void 14 o = ulong 0 15 while lt o len a bound 4096 16 k = write 1 a o sub len a o 17 if le k 0 18 exit 1 19 end 20 o = add o ulong k 21 end 22 end 23 note emitu writes n in decimal; 20 digits hold every ulong 24 fn emitu n ulong to void 25 d = new uchar 20 0 26 i = ulong 20 27 v = n 28 while true bound 20 29 i = sub i 1 30 put d i uchar add rem v 10 48 31 v = div v 10 32 if eq v 0 33 break 34 end 35 end 36 emit part d i sub 20 i 37 end 38 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 39 fn emiti n long to void 40 if lt n 0 41 emit minus 42 emitu add bitcast ulong not n 1 43 else 44 emitu ulong n 45 end 46 end 47 note emitwi writes a signed n right-aligned in w columns (w at most 20) 48 fn emitwi n long w ulong to void 49 d = new uchar 21 32 50 i = ulong 21 51 v = n 52 if lt n 0 53 v = neg n 54 end 55 while true bound 20 56 i = sub i 1 57 put d i uchar add long 48 rem v 10 58 v = div v 10 59 if eq v 0 60 break 61 end 62 end 63 if lt n 0 64 i = sub i 1 65 put d i 45 66 end 67 if gt w sub 21 i 68 i = sub 21 w 69 end 70 emit part d i sub 21 i 71 end 72 note vertices 1..4 are stored as 0..3; has[i*4+j] marks a known distance, nxt the first hop 73 fn edge u ulong v ulong w long to void 74 i = add mul sub u 1 4 sub v 1 75 put dist i w 76 put has i 1 77 put nxt i sub v 1 78 end 79 note try k as an intermediate between i and j 80 fn via i ulong k ulong j ulong to void 81 ik = add mul i 4 k 82 kj = add mul k 4 j 83 ij = add mul i 4 j 84 if and eq get has ik 1 eq get has kj 1 85 d = add get dist ik get dist kj 86 if or eq get has ij 0 lt d get dist ij 87 put dist ij d 88 put has ij 1 89 put nxt ij get nxt ik 90 end 91 end 92 end 93 note path follows the next-hop table from i to j; a shortest path visits at most the 4 vertices 94 fn path i ulong j ulong to void 95 emitu add i 1 96 v = i 97 while ne v j bound 4 98 v = get nxt add mul v 4 j 99 emit tarrow 100 emitu add v 1 101 end 102 end 103 fn main to int 104 edge 1 3 -2 105 edge 2 1 4 106 edge 2 3 3 107 edge 3 4 2 108 edge 4 2 -1 109 for k ulong 4 110 for i ulong 4 111 for j ulong 4 112 via i k j 113 end 114 end 115 end 116 emit thead 117 for i ulong 4 118 for j ulong 4 119 if ne i j 120 emitu add i 1 121 emit tarrow 122 emitu add j 1 123 emitwi get dist add mul i 4 j 6 124 emit sp 125 emit sp 126 emit sp 127 emit sp 128 path i j 129 emit nl 130 end 131 end 132 end 133 ret 0 134 end