interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / patience-sort / patience-sort.om

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