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

143 lines · 2587 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 6 5 4 3 2 1 0"
10text tidx "indices: "
11global vals long 8
12global idx ulong 3
13global sub3 long 3
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 sorts the three indices, gathers their values, sorts those, and writes them back in index order;
92note both sorts are three compare-exchanges, enough for three elements
93fn cx3u to void
94for p ulong 3
95i = rem p 2
96if gt get idx i get idx add i 1
97t = get idx i
98put idx i get idx add i 1
99put idx add i 1 t
100end
101end
102end
103fn cx3l to void
104for p ulong 3
105i = rem p 2
106if gt get sub3 i get sub3 add i 1
107t = get sub3 i
108put sub3 i get sub3 add i 1
109put sub3 add i 1 t
110end
111end
112end
113fn main to int
114n = parse data
115put idx 0 6
116put idx 1 1
117put idx 2 7
118show n
119emit tidx
120emitu get idx 0
121emit sp
122emitu get idx 1
123emit sp
124emitu get idx 2
125emit nl
126cx3u
127for i ulong 3
128put sub3 i get vals get idx i
129end
130cx3l
131for i ulong 3
132put vals get idx i get sub3 i
133end
134show n
135ret 0
136end
137</syntaxhighlight>
138{{out}}
139<pre>
1407 6 5 4 3 2 1 0
141indices: 6 1 7
1427 0 5 4 3 2 1 6
143</pre>