interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / patience-sort.wiki

146 lines · 2839 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text data "4 65 2 -31 0 99 83 782 1 4 -31 18"
10global vals long 16
11global pile long 256
12global plen ulong 16
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
40fn emiti n long to void
41if lt n 0
42emit minus
43emitu add bitcast ulong not n 1
44else
45emitu ulong n
46end
47end
48note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
49note returns how many; one pass over s, so the loop is bounded by len s
50fn parse s view uchar * to ulong
51n = ulong 0
52v = long 0
53sg = long 1
54dig = false
55for i ulong len s
56c = get s i
57if eq c 45
58sg = long -1
59else
60if and ge c 48 le c 57
61v = add mul v 10 long sub c 48
62dig = true
63else
64if dig
65put vals n mul sg v
66n = add n 1
67end
68v = long 0
69sg = long 1
70dig = false
71end
72end
73end
74if dig
75put vals n mul sg v
76n = add n 1
77end
78ret n
79end
80note show writes vals[0..n-1] separated by spaces, then a newline
81fn show n ulong to void
82for i ulong n
83if gt i 0
84emit sp
85end
86emiti get vals i
87end
88emit nl
89end
90note piles: pile p holds its cards in pile[p*16 .. p*16+plen[p]-1], the top card last; tops decrease
91note bottom to top, so a new card goes on the leftmost pile whose top is >= it, else on a new pile
92fn deal n ulong to ulong
93np = ulong 0
94for i ulong n
95v = get vals i
96p = ulong 0
97while lt p np bound 16
98if ge top p v
99break
100end
101p = add p 1
102end
103if eq p np
104np = add np 1
105end
106put pile add mul p 16 get plen p v
107put plen p add get plen p 1
108end
109ret np
110end
111note top is the top card of pile p, which must not be empty
112fn top p ulong to long
113ret get pile add mul p 16 sub get plen p 1
114end
115note merge takes the smallest top card of all piles n times; at most 16 piles to scan each time
116fn merge n ulong np ulong to void
117for k ulong n
118b = np
119for p ulong np
120if gt get plen p 0
121if eq b np
122b = p
123else
124if lt top p top b
125b = p
126end
127end
128end
129end
130put plen b sub get plen b 1
131put vals k get pile add mul b 16 get plen b
132end
133end
134fn main to int
135n = parse data
136show n
137merge n deal n
138show n
139ret 0
140end
141</syntaxhighlight>
142{{out}}
143<pre>
1444 65 2 -31 0 99 83 782 1 4 -31 18
145-31 -31 0 1 2 4 4 18 65 83 99 782
146</pre>