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

125 lines · 2358 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 "7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8"
10global vals long 16
11global cnt ulong 64
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
39fn emiti n long to void
40if lt n 0
41emit minus
42emitu add bitcast ulong not n 1
43else
44emitu ulong n
45end
46end
47note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
48note returns how many; one pass over s, so the loop is bounded by len s
49fn parse s view uchar * to ulong
50n = ulong 0
51v = long 0
52sg = long 1
53dig = false
54for i ulong len s
55c = get s i
56if eq c 45
57sg = long -1
58else
59if and ge c 48 le c 57
60v = add mul v 10 long sub c 48
61dig = true
62else
63if dig
64put vals n mul sg v
65n = add n 1
66end
67v = long 0
68sg = long 1
69dig = false
70end
71end
72end
73if dig
74put vals n mul sg v
75n = add n 1
76end
77ret n
78end
79note show writes vals[0..n-1] separated by spaces, then a newline
80fn show n ulong to void
81for i ulong n
82if gt i 0
83emit sp
84end
85emiti get vals i
86end
87emit nl
88end
89note cntsort counts each value in min..max (the range must fit the 64 counters), then rewrites vals
90fn cntsort n ulong to void
91lo = get vals 0
92hi = get vals 0
93for i ulong n
94if lt get vals i lo
95lo = get vals i
96end
97if gt get vals i hi
98hi = get vals i
99end
100end
101for i ulong n
102c = ulong sub get vals i lo
103put cnt c add get cnt c 1
104end
105k = ulong 0
106for x ulong add ulong sub hi lo 1
107for y ulong get cnt x
108put vals k add lo long x
109k = add k 1
110end
111end
112end
113fn main to int
114n = parse data
115show n
116cntsort n
117show n
118ret 0
119end
120</syntaxhighlight>
121{{out}}
122<pre>
1237 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8
124-5 -3 -3 0 0 1 4 4 7 7 8 9 12 12 15
125</pre>