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-increasing-subsequence / longest-increasing-subsequence.om

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