interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / floyd-warshall-algorithm / floyd-warshall-algorithm.om

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