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