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