omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / longest-common-prefix.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 tarr " -> \"" |
| 8 | text tq "\"\n" |
| 9 | text lb "[" |
| 10 | text rb "]" |
| 11 | text l0 "interspecies|interstellar|interstate" |
| 12 | text l1 "throne|throne" |
| 13 | text l2 "throne|dungeon" |
| 14 | text l3 "throne||throne" |
| 15 | text l4 "cheese" |
| 16 | text l6 "prefix|suffix" |
| 17 | text l7 "foo|foobar" |
| 18 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 19 | fn emit a view uchar * to void |
| 20 | o = ulong 0 |
| 21 | while lt o len a bound 4096 |
| 22 | k = write 1 a o sub len a o |
| 23 | if le k 0 |
| 24 | exit 1 |
| 25 | end |
| 26 | o = add o ulong k |
| 27 | end |
| 28 | end |
| 29 | note a list is its elements separated by '|'; an empty text is the empty list. |
| 30 | note split records each element as (start, length) and returns how many |
| 31 | fn split a view uchar * st edit ulong 16 ln edit ulong 16 to ulong |
| 32 | k = ulong 0 |
| 33 | s = ulong 0 |
| 34 | for i ulong add len a 1 |
| 35 | e = eq i len a |
| 36 | if not e |
| 37 | e = eq get a i 124 |
| 38 | end |
| 39 | if e |
| 40 | put st k s |
| 41 | put ln k sub i s |
| 42 | k = add k 1 |
| 43 | s = add i 1 |
| 44 | end |
| 45 | end |
| 46 | ret k |
| 47 | end |
| 48 | note common walks the first element and stops where any other element ends or differs |
| 49 | fn common a view uchar * st view ulong 16 ln view ulong 16 k ulong to ulong |
| 50 | p = ulong 0 |
| 51 | while lt p get ln 0 bound 4096 |
| 52 | for w ulong k |
| 53 | if eq p get ln w |
| 54 | ret p |
| 55 | end |
| 56 | c = get a add get st 0 p |
| 57 | if ne get a add get st w p c |
| 58 | ret p |
| 59 | end |
| 60 | end |
| 61 | p = add p 1 |
| 62 | end |
| 63 | ret p |
| 64 | end |
| 65 | fn lcp a view uchar * to void |
| 66 | st = new ulong 16 0 |
| 67 | ln = new ulong 16 0 |
| 68 | emit lb |
| 69 | emit a |
| 70 | emit rb |
| 71 | emit tarr |
| 72 | if gt len a 0 |
| 73 | k = split a st ln |
| 74 | emit part a get st 0 common a st ln k |
| 75 | end |
| 76 | emit tq |
| 77 | end |
| 78 | fn main to int |
| 79 | lcp l0 |
| 80 | lcp l1 |
| 81 | lcp l2 |
| 82 | lcp l3 |
| 83 | lcp l4 |
| 84 | lcp part l0 0 0 |
| 85 | lcp l6 |
| 86 | lcp l7 |
| 87 | ret 0 |
| 88 | end |
| 89 | </syntaxhighlight> |
| 90 | {{out}} |
| 91 | <pre> |
| 92 | [interspecies|interstellar|interstate] -> "inters" |
| 93 | [throne|throne] -> "throne" |
| 94 | [throne|dungeon] -> "" |
| 95 | [throne||throne] -> "" |
| 96 | [cheese] -> "cheese" |
| 97 | [] -> "" |
| 98 | [prefix|suffix] -> "" |
| 99 | [foo|foobar] -> "foo" |
| 100 | </pre> |