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