omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / longest-common-subsequence.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text a1 "thisisatest" |
| 9 | text b1 "testing123testing" |
| 10 | text a2 "1234" |
| 11 | text b2 "1224533324" |
| 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 fill writes the (len a + 1) x (len b + 1) length table into t, row-major with width len b + 1; |
| 24 | note both lengths up to 63 |
| 25 | fn fill a view uchar * b view uchar * t edit ulong 4096 to void |
| 26 | w = add len b 1 |
| 27 | for i ulong len a |
| 28 | for j ulong len b |
| 29 | up = add mul i w add j 1 |
| 30 | x = add get t sub up 1 1 |
| 31 | if ne get a i get b j |
| 32 | x = get t up |
| 33 | l = get t add up sub w 1 |
| 34 | if lt x l |
| 35 | x = l |
| 36 | end |
| 37 | end |
| 38 | put t add up w x |
| 39 | end |
| 40 | end |
| 41 | end |
| 42 | note goup: the cell above is at least the cell to the left |
| 43 | fn goup t view ulong 4096 i ulong j ulong w ulong to bool |
| 44 | u = get t add mul sub i 1 w j |
| 45 | ret ge u get t add mul i w sub j 1 |
| 46 | end |
| 47 | note walk goes back from the corner of the table: a match is taken, otherwise the larger neighbour, |
| 48 | note the upper one on a tie. The subsequence is written into o back to front. |
| 49 | fn walk a view uchar * b view uchar * t view ulong 4096 o edit uchar 64 to void |
| 50 | w = add len b 1 |
| 51 | i = len a |
| 52 | j = len b |
| 53 | k = get t add mul i w j |
| 54 | while and gt i 0 gt j 0 bound 200 |
| 55 | if eq get a sub i 1 get b sub j 1 |
| 56 | k = sub k 1 |
| 57 | put o k get a sub i 1 |
| 58 | i = sub i 1 |
| 59 | j = sub j 1 |
| 60 | else |
| 61 | if goup t i j w |
| 62 | i = sub i 1 |
| 63 | else |
| 64 | j = sub j 1 |
| 65 | end |
| 66 | end |
| 67 | end |
| 68 | end |
| 69 | fn lcs a view uchar * b view uchar * to void |
| 70 | t = new ulong 4096 0 |
| 71 | fill a b t |
| 72 | o = new uchar 64 0 |
| 73 | n = get t add mul len a add len b 1 len b |
| 74 | walk a b t o |
| 75 | emit part o 0 n |
| 76 | emit nl |
| 77 | end |
| 78 | fn main to int |
| 79 | lcs a1 b1 |
| 80 | lcs a2 b2 |
| 81 | ret 0 |
| 82 | end |
| 83 | </syntaxhighlight> |
| 84 | {{out}} |
| 85 | <pre> |
| 86 | tsitest |
| 87 | 1234 |
| 88 | </pre> |