interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

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

167 lines · 2922 bytes Raw Blame History
1note Rosetta Code task "Sorting algorithms/Strand sort": https://rosettacode.org/wiki/Sorting_algorithms/Strand_sort
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text data "5 1 4 2 0 9 6 3 8 7 -2 11"
8global vals long 16
9global rest long 16
10global strand long 16
11global res long 16
12global mrg long 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 pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the
91note front of rest, and returns the strand length
92fn pull m ulong to ulong
93s = ulong 0
94k = ulong 0
95for i ulong m
96v = get rest i
97take = false
98if eq s 0
99take = true
100else
101if ge v get strand sub s 1
102take = true
103end
104end
105if take
106put strand s v
107s = add s 1
108else
109put rest k v
110k = add k 1
111end
112end
113ret s
114end
115note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length
116fn mergein r ulong s ulong to ulong
117i = ulong 0
118j = ulong 0
119k = ulong 0
120while lt k add r s bound 32
121left = false
122if ge j s
123left = true
124else
125if lt i r
126if le get res i get strand j
127left = true
128end
129end
130end
131if left
132put mrg k get res i
133i = add i 1
134else
135put mrg k get strand j
136j = add j 1
137end
138k = add k 1
139end
140for x ulong k
141put res x get mrg x
142end
143ret k
144end
145note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice
146fn stsort n ulong to void
147for i ulong n
148put rest i get vals i
149end
150m = n
151r = ulong 0
152while gt m 0 bound 16
153s = pull m
154m = sub m s
155r = mergein r s
156end
157for i ulong n
158put vals i get res i
159end
160end
161fn main to int
162n = parse data
163show n
164stsort n
165show n
166ret 0
167end