interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / longest-common-subsequence / longest-common-subsequence.om

80 lines · 1844 bytes Raw Blame History
1note Rosetta Code task "Longest common subsequence": https://rosettacode.org/wiki/Longest_common_subsequence
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text a1 "thisisatest"
7text b1 "testing123testing"
8text a2 "1234"
9text b2 "1224533324"
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note fill writes the (len a + 1) x (len b + 1) length table into t, row-major with width len b + 1;
22note both lengths up to 63
23fn fill a view uchar * b view uchar * t edit ulong 4096 to void
24w = add len b 1
25for i ulong len a
26for j ulong len b
27up = add mul i w add j 1
28x = add get t sub up 1 1
29if ne get a i get b j
30x = get t up
31l = get t add up sub w 1
32if lt x l
33x = l
34end
35end
36put t add up w x
37end
38end
39end
40note goup: the cell above is at least the cell to the left
41fn goup t view ulong 4096 i ulong j ulong w ulong to bool
42u = get t add mul sub i 1 w j
43ret ge u get t add mul i w sub j 1
44end
45note walk goes back from the corner of the table: a match is taken, otherwise the larger neighbour,
46note the upper one on a tie. The subsequence is written into o back to front.
47fn walk a view uchar * b view uchar * t view ulong 4096 o edit uchar 64 to void
48w = add len b 1
49i = len a
50j = len b
51k = get t add mul i w j
52while and gt i 0 gt j 0 bound 200
53if eq get a sub i 1 get b sub j 1
54k = sub k 1
55put o k get a sub i 1
56i = sub i 1
57j = sub j 1
58else
59if goup t i j w
60i = sub i 1
61else
62j = sub j 1
63end
64end
65end
66end
67fn lcs a view uchar * b view uchar * to void
68t = new ulong 4096 0
69fill a b t
70o = new uchar 64 0
71n = get t add mul len a add len b 1 len b
72walk a b t o
73emit part o 0 n
74emit nl
75end
76fn main to int
77lcs a1 b1
78lcs a2 b2
79ret 0
80end