interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / permutation-sort / permutation-sort.om

156 lines · 2854 bytes Raw Blame History
1note Rosetta Code task "Sorting algorithms/Permutation sort": https://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text data "8 -2 15 3 0 6"
8text ttried "permutations tried: "
9global vals long 8
10global perm ulong 8
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
38fn emiti n long to void
39if lt n 0
40emit minus
41emitu add bitcast ulong not n 1
42else
43emitu ulong n
44end
45end
46note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
47note returns how many; one pass over s, so the loop is bounded by len s
48fn parse s view uchar * to ulong
49n = ulong 0
50v = long 0
51sg = long 1
52dig = false
53for i ulong len s
54c = get s i
55if eq c 45
56sg = long -1
57else
58if and ge c 48 le c 57
59v = add mul v 10 long sub c 48
60dig = true
61else
62if dig
63put vals n mul sg v
64n = add n 1
65end
66v = long 0
67sg = long 1
68dig = false
69end
70end
71end
72if dig
73put vals n mul sg v
74n = add n 1
75end
76ret n
77end
78note show writes vals[0..n-1] separated by spaces, then a newline
79fn show n ulong to void
80for i ulong n
81if gt i 0
82emit sp
83end
84emiti get vals i
85end
86emit nl
87end
88note sorted says whether vals taken in the order perm[0..n-1] is non-decreasing
89fn sorted n ulong to bool
90for i ulong sub n 1
91if gt get vals get perm i get vals get perm add i 1
92ret false
93end
94end
95ret true
96end
97note nextp steps perm to the next permutation in lexicographic order; false after the last one
98fn nextp n ulong to bool
99i = sub n 1
100while gt i 0 bound 8
101if lt get perm sub i 1 get perm i
102break
103end
104i = sub i 1
105end
106if eq i 0
107ret false
108end
109j = sub n 1
110while le get perm j get perm sub i 1 bound 8
111j = sub j 1
112end
113t = get perm sub i 1
114put perm sub i 1 get perm j
115put perm j t
116k = sub n 1
117while lt i k bound 8
118t2 = get perm i
119put perm i get perm k
120put perm k t2
121i = add i 1
122k = sub k 1
123end
124ret true
125end
126note pmsort tries the index permutations in lexicographic order (at most 6! = 720) until one is sorted
127fn pmsort n ulong to ulong
128for i ulong n
129put perm i i
130end
131c = ulong 1
132while not sorted n bound 720
133if not nextp n
134trap 9
135end
136c = add c 1
137end
138t = new long 8 0
139for i ulong n
140put t i get vals get perm i
141end
142for i ulong n
143put vals i get t i
144end
145ret c
146end
147fn main to int
148n = parse data
149show n
150c = pmsort n
151show n
152emit ttried
153emitu c
154emit nl
155ret 0
156end