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-mv/blob/main/si/rosetta/tasks/longest-increasing-subsequence/longest-increasing-subsequence.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / longest-increasing-subsequence / longest-increasing-subsequence.om` 150 lines ยท 2876 bytes Raw Blame History 1 note Rosetta Code task "Longest increasing subsequence": https://rosettacode.org/wiki/Longest_increasing_subsequence 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text d1 "3 2 6 4 5 1" 8 text d2 "0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15" 9 global vals long 16 10 global ln ulong 16 11 global nx ulong 16 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 emitu writes n in decimal; 20 digits hold every ulong 24 fn emitu n ulong to void 25 d = new uchar 20 0 26 i = ulong 20 27 v = n 28 while true bound 20 29 i = sub i 1 30 put d i uchar add rem v 10 48 31 v = div v 10 32 if eq v 0 33 break 34 end 35 end 36 emit part d i sub 20 i 37 end 38 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 39 fn emiti n long to void 40 if lt n 0 41 emit minus 42 emitu add bitcast ulong not n 1 43 else 44 emitu ulong n 45 end 46 end 47 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 48 note returns how many; one pass over s, so the loop is bounded by len s 49 fn parse s view uchar * to ulong 50 n = ulong 0 51 v = long 0 52 sg = long 1 53 dig = false 54 for i ulong len s 55 c = get s i 56 if eq c 45 57 sg = long -1 58 else 59 if and ge c 48 le c 57 60 v = add mul v 10 long sub c 48 61 dig = true 62 else 63 if dig 64 put vals n mul sg v 65 n = add n 1 66 end 67 v = long 0 68 sg = long 1 69 dig = false 70 end 71 end 72 end 73 if dig 74 put vals n mul sg v 75 n = add n 1 76 end 77 ret n 78 end 79 note show writes vals[0..n-1] separated by spaces, then a newline 80 fn show n ulong to void 81 for i ulong n 82 if gt i 0 83 emit sp 84 end 85 emiti get vals i 86 end 87 emit nl 88 end 89 note lis prints the lexicographically smallest of the longest strictly increasing subsequences: 90 note ln[i] is the longest increasing run starting at i (computed right to left), nx[i] the smallest 91 note next value that keeps it longest; the walk then starts at the smallest value with the maximum ln 92 note grow considers continuing the run at i with element k > i 93 fn grow i ulong k ulong to void 94 if gt get vals k get vals i 95 c = add get ln k 1 96 if gt c get ln i 97 put ln i c 98 put nx i k 99 else 100 if eq c get ln i 101 if lt get vals k get vals get nx i 102 put nx i k 103 end 104 end 105 end 106 end 107 end 108 note better says whether a run starting at i beats the best so far (length m starting at s) 109 fn better i ulong s ulong m ulong to bool 110 if gt get ln i m 111 ret true 112 end 113 ret and eq get ln i m lt get vals i get vals s 114 end 115 fn lis n ulong to void 116 for x ulong n 117 i = sub sub n x 1 118 put ln i 1 119 put nx i n 120 for j ulong sub sub n i 1 121 grow i add add i j 1 122 end 123 end 124 m = ulong 0 125 s = ulong 0 126 for i ulong n 127 if better i s m 128 m = get ln i 129 s = i 130 end 131 end 132 i = s 133 for x ulong m 134 if gt x 0 135 emit sp 136 end 137 emiti get vals i 138 i = get nx i 139 end 140 emit nl 141 end 142 fn main to int 143 n = parse d1 144 show n 145 lis n 146 n = parse d2 147 show n 148 lis n 149 ret 0 150 end