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/strand-sort.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 / strand-sort.wiki`
175 lines ยท 3074 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 "5 1 4 2 0 9 6 3 8 7 -2 11"
10 global vals long 16
11 global rest long 16
12 global strand long 16
13 global res long 16
14 global mrg long 16
15 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16 fn emit a view uchar * to void
17 o = ulong 0
18 while lt o len a bound 4096
19 k = write 1 a o sub len a o
20 if le k 0
21 exit 1
22 end
23 o = add o ulong k
24 end
25 end
26 note emitu writes n in decimal; 20 digits hold every ulong
27 fn emitu n ulong to void
28 d = new uchar 20 0
29 i = ulong 20
30 v = n
31 while true bound 20
32 i = sub i 1
33 put d i uchar add rem v 10 48
34 v = div v 10
35 if eq v 0
36 break
37 end
38 end
39 emit part d i sub 20 i
40 end
41 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
42 fn emiti n long to void
43 if lt n 0
44 emit minus
45 emitu add bitcast ulong not n 1
46 else
47 emitu ulong n
48 end
49 end
50 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
51 note returns how many; one pass over s, so the loop is bounded by len s
52 fn parse s view uchar * to ulong
53 n = ulong 0
54 v = long 0
55 sg = long 1
56 dig = false
57 for i ulong len s
58 c = get s i
59 if eq c 45
60 sg = long -1
61 else
62 if and ge c 48 le c 57
63 v = add mul v 10 long sub c 48
64 dig = true
65 else
66 if dig
67 put vals n mul sg v
68 n = add n 1
69 end
70 v = long 0
71 sg = long 1
72 dig = false
73 end
74 end
75 end
76 if dig
77 put vals n mul sg v
78 n = add n 1
79 end
80 ret n
81 end
82 note show writes vals[0..n-1] separated by spaces, then a newline
83 fn show n ulong to void
84 for i ulong n
85 if gt i 0
86 emit sp
87 end
88 emiti get vals i
89 end
90 emit nl
91 end
92 note pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the
93 note front of rest, and returns the strand length
94 fn pull m ulong to ulong
95 s = ulong 0
96 k = ulong 0
97 for i ulong m
98 v = get rest i
99 take = false
100 if eq s 0
101 take = true
102 else
103 if ge v get strand sub s 1
104 take = true
105 end
106 end
107 if take
108 put strand s v
109 s = add s 1
110 else
111 put rest k v
112 k = add k 1
113 end
114 end
115 ret s
116 end
117 note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length
118 fn mergein r ulong s ulong to ulong
119 i = ulong 0
120 j = ulong 0
121 k = ulong 0
122 while lt k add r s bound 32
123 left = false
124 if ge j s
125 left = true
126 else
127 if lt i r
128 if le get res i get strand j
129 left = true
130 end
131 end
132 end
133 if left
134 put mrg k get res i
135 i = add i 1
136 else
137 put mrg k get strand j
138 j = add j 1
139 end
140 k = add k 1
141 end
142 for x ulong k
143 put res x get mrg x
144 end
145 ret k
146 end
147 note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice
148 fn stsort n ulong to void
149 for i ulong n
150 put rest i get vals i
151 end
152 m = n
153 r = ulong 0
154 while gt m 0 bound 16
155 s = pull m
156 m = sub m s
157 r = mergein r s
158 end
159 for i ulong n
160 put vals i get res i
161 end
162 end
163 fn main to int
164 n = parse data
165 show n
166 stsort n
167 show n
168 ret 0
169 end
170
171 {{out}}
172
173 5 1 4 2 0 9 6 3 8 7 -2 11
174 -2 0 1 2 3 4 5 6 7 8 9 11
175