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