Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/sort-disjoint-sublist.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
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}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text data "7 6 5 4 3 2 1 0"
10 text tidx "indices: "
11 global vals long 8
12 global idx ulong 3
13 global sub3 long 3
14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15 fn emit a view uchar * to void
16 o = ulong 0
17 while lt o len a bound 4096
18 k = write 1 a o sub len a o
19 if le k 0
20 exit 1
21 end
22 o = add o ulong k
23 end
24 end
25 note emitu writes n in decimal; 20 digits hold every ulong
26 fn emitu n ulong to void
27 d = new uchar 20 0
28 i = ulong 20
29 v = n
30 while true bound 20
31 i = sub i 1
32 put d i uchar add rem v 10 48
33 v = div v 10
34 if eq v 0
35 break
36 end
37 end
38 emit part d i sub 20 i
39 end
40 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41 fn emiti n long to void
42 if lt n 0
43 emit minus
44 emitu add bitcast ulong not n 1
45 else
46 emitu ulong n
47 end
48 end
49 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
50 note returns how many; one pass over s, so the loop is bounded by len s
51 fn parse s view uchar * to ulong
52 n = ulong 0
53 v = long 0
54 sg = long 1
55 dig = false
56 for i ulong len s
57 c = get s i
58 if eq c 45
59 sg = long -1
60 else
61 if and ge c 48 le c 57
62 v = add mul v 10 long sub c 48
63 dig = true
64 else
65 if dig
66 put vals n mul sg v
67 n = add n 1
68 end
69 v = long 0
70 sg = long 1
71 dig = false
72 end
73 end
74 end
75 if dig
76 put vals n mul sg v
77 n = add n 1
78 end
79 ret n
80 end
81 note show writes vals[0..n-1] separated by spaces, then a newline
82 fn show n ulong to void
83 for i ulong n
84 if gt i 0
85 emit sp
86 end
87 emiti get vals i
88 end
89 emit nl
90 end
91 note sorts the three indices, gathers their values, sorts those, and writes them back in index order;
92 note both sorts are three compare-exchanges, enough for three elements
93 fn cx3u to void
94 for p ulong 3
95 i = rem p 2
96 if gt get idx i get idx add i 1
97 t = get idx i
98 put idx i get idx add i 1
99 put idx add i 1 t
100 end
101 end
102 end
103 fn cx3l to void
104 for p ulong 3
105 i = rem p 2
106 if gt get sub3 i get sub3 add i 1
107 t = get sub3 i
108 put sub3 i get sub3 add i 1
109 put sub3 add i 1 t
110 end
111 end
112 end
113 fn main to int
114 n = parse data
115 put idx 0 6
116 put idx 1 1
117 put idx 2 7
118 show n
119 emit tidx
120 emitu get idx 0
121 emit sp
122 emitu get idx 1
123 emit sp
124 emitu get idx 2
125 emit nl
126 cx3u
127 for i ulong 3
128 put sub3 i get vals get idx i
129 end
130 cx3l
131 for i ulong 3
132 put vals get idx i get sub3 i
133 end
134 show n
135 ret 0
136 end
137
138 {{out}}
139
140 7 6 5 4 3 2 1 0
141 indices: 6 1 7
142 7 0 5 4 3 2 1 6
143