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 / strand-sort.wiki

175 lines · 3074 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 "5 1 4 2 0 9 6 3 8 7 -2 11"
10global vals long 16
11global rest long 16
12global strand long 16
13global res long 16
14global mrg long 16
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
42fn emiti n long to void
43if lt n 0
44emit minus
45emitu add bitcast ulong not n 1
46else
47emitu ulong n
48end
49end
50note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
51note returns how many; one pass over s, so the loop is bounded by len s
52fn parse s view uchar * to ulong
53n = ulong 0
54v = long 0
55sg = long 1
56dig = false
57for i ulong len s
58c = get s i
59if eq c 45
60sg = long -1
61else
62if and ge c 48 le c 57
63v = add mul v 10 long sub c 48
64dig = true
65else
66if dig
67put vals n mul sg v
68n = add n 1
69end
70v = long 0
71sg = long 1
72dig = false
73end
74end
75end
76if dig
77put vals n mul sg v
78n = add n 1
79end
80ret n
81end
82note show writes vals[0..n-1] separated by spaces, then a newline
83fn show n ulong to void
84for i ulong n
85if gt i 0
86emit sp
87end
88emiti get vals i
89end
90emit nl
91end
92note pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the
93note front of rest, and returns the strand length
94fn pull m ulong to ulong
95s = ulong 0
96k = ulong 0
97for i ulong m
98v = get rest i
99take = false
100if eq s 0
101take = true
102else
103if ge v get strand sub s 1
104take = true
105end
106end
107if take
108put strand s v
109s = add s 1
110else
111put rest k v
112k = add k 1
113end
114end
115ret s
116end
117note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length
118fn mergein r ulong s ulong to ulong
119i = ulong 0
120j = ulong 0
121k = ulong 0
122while lt k add r s bound 32
123left = false
124if ge j s
125left = true
126else
127if lt i r
128if le get res i get strand j
129left = true
130end
131end
132end
133if left
134put mrg k get res i
135i = add i 1
136else
137put mrg k get strand j
138j = add j 1
139end
140k = add k 1
141end
142for x ulong k
143put res x get mrg x
144end
145ret k
146end
147note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice
148fn stsort n ulong to void
149for i ulong n
150put rest i get vals i
151end
152m = n
153r = ulong 0
154while gt m 0 bound 16
155s = pull m
156m = sub m s
157r = mergein r s
158end
159for i ulong n
160put vals i get res i
161end
162end
163fn main to int
164n = parse data
165show n
166stsort n
167show n
168ret 0
169end
170</syntaxhighlight>
171{{out}}
172<pre>
1735 1 4 2 0 9 6 3 8 7 -2 11
174-2 0 1 2 3 4 5 6 7 8 9 11
175</pre>