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

129 lines · 2553 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 3 1 7 4 1 1 20 0 9"
10global vals long 16
11global grid uchar 512
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 the abacus has one row per value (at most 16) and one column per unit (at most 32): grid[r*32+c]
90note holds a bead when c < vals[r]; the values here are 0 .. 20
91fn bdsort n ulong to void
92for r ulong n
93for c ulong ulong get vals r
94put grid add mul r 32 c 1
95end
96end
97note gravity: each column's beads fall to the bottom rows
98for c ulong 32
99h = ulong 0
100for r ulong n
101h = add h ulong get grid add mul r 32 c
102put grid add mul r 32 c 0
103end
104for x ulong h
105put grid add mul sub sub n x 1 32 c 1
106end
107end
108note row r now holds the r-th smallest value as its bead count, read top to bottom
109for r ulong n
110v = long 0
111for c ulong 32
112v = add v long get grid add mul r 32 c
113end
114put vals r v
115end
116end
117fn main to int
118n = parse data
119show n
120bdsort n
121show n
122ret 0
123end
124</syntaxhighlight>
125{{out}}
126<pre>
1275 3 1 7 4 1 1 20 0 9
1280 1 1 1 3 4 5 7 9 20
129</pre>