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

152 lines · 2904 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 "170 45 75 -90 -802 24 2 66 0 1999 -1 333"
10global vals long 16
11global key ulong 16
12global tk ulong 16
13global bk ulong 10
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41fn emiti n long to void
42if lt n 0
43emit minus
44emitu add bitcast ulong not n 1
45else
46emitu ulong n
47end
48end
49note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
50note returns how many; one pass over s, so the loop is bounded by len s
51fn parse s view uchar * to ulong
52n = ulong 0
53v = long 0
54sg = long 1
55dig = false
56for i ulong len s
57c = get s i
58if eq c 45
59sg = long -1
60else
61if and ge c 48 le c 57
62v = add mul v 10 long sub c 48
63dig = true
64else
65if dig
66put vals n mul sg v
67n = add n 1
68end
69v = long 0
70sg = long 1
71dig = false
72end
73end
74end
75if dig
76put vals n mul sg v
77n = add n 1
78end
79ret n
80end
81note show writes vals[0..n-1] separated by spaces, then a newline
82fn show n ulong to void
83for i ulong n
84if gt i 0
85emit sp
86end
87emiti get vals i
88end
89emit nl
90end
91note rxsort shifts every value by the minimum so the keys are unsigned, then sorts the keys with a
92note stable least-significant-digit-first counting pass per decimal digit; 20 passes cover any ulong
93fn rxsort n ulong to void
94lo = get vals 0
95for i ulong n
96if lt get vals i lo
97lo = get vals i
98end
99end
100mx = ulong 0
101for i ulong n
102put key i ulong sub get vals i lo
103if gt get key i mx
104mx = get key i
105end
106end
107e = ulong 1
108for p ulong 20
109for b ulong 10
110put bk b 0
111end
112for i ulong n
113dg = rem div get key i e 10
114put bk dg add get bk dg 1
115end
116s = ulong 0
117for b ulong 10
118c = get bk b
119put bk b s
120s = add s c
121end
122for i ulong n
123dg = rem div get key i e 10
124put tk get bk dg get key i
125put bk dg add get bk dg 1
126end
127for i ulong n
128put key i get tk i
129end
130note stop when no key has a digit at 10*e; e > mx/10 means 10*e > mx, and 10*e is never formed past that
131if gt e div mx 10
132break
133end
134e = mul e 10
135end
136for i ulong n
137put vals i add lo long get key i
138end
139end
140fn main to int
141n = parse data
142show n
143rxsort n
144show n
145ret 0
146end
147</syntaxhighlight>
148{{out}}
149<pre>
150170 45 75 -90 -802 24 2 66 0 1999 -1 333
151-802 -90 -1 0 2 24 45 66 75 170 333 1999
152</pre>