omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / permutations-by-swapping / permutations-by-swapping.om
| 1 | note Rosetta Code task "Permutations by swapping": https://rosettacode.org/wiki/Permutations_by_swapping |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text minus "-" |
| 7 | text tperm "Perm: [" |
| 8 | text tsign "] Sign: " |
| 9 | text tcomma ", " |
| 10 | global perm ulong 8 |
| 11 | global dir long 8 |
| 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 Steinhaus-Johnson-Trotter with Even's speedup: every element has a direction (all start left); |
| 48 | note the largest mobile element (one whose neighbour in its direction is smaller) swaps that way, and |
| 49 | note every larger element turns round; each step is one swap, so the sign alternates |
| 50 | fn mobile n ulong to ulong |
| 51 | b = n |
| 52 | for i ulong n |
| 53 | j = add long i get dir i |
| 54 | if and ge j 0 lt j long n |
| 55 | if gt get perm i get perm ulong j |
| 56 | if or eq b n gt get perm i get perm b |
| 57 | b = i |
| 58 | end |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | ret b |
| 63 | end |
| 64 | fn show n ulong s long to void |
| 65 | emit tperm |
| 66 | for i ulong n |
| 67 | if gt i 0 |
| 68 | emit tcomma |
| 69 | end |
| 70 | emitu get perm i |
| 71 | end |
| 72 | emit tsign |
| 73 | emiti s |
| 74 | emit nl |
| 75 | end |
| 76 | fn sjt n ulong to void |
| 77 | for i ulong n |
| 78 | put perm i add i 1 |
| 79 | put dir i -1 |
| 80 | end |
| 81 | s = long 1 |
| 82 | show n s |
| 83 | note each pass makes one new permutation and n! of them end the run: 720 = 6! covers n up to 6 |
| 84 | while true bound 720 |
| 85 | m = mobile n |
| 86 | if eq m n |
| 87 | break |
| 88 | end |
| 89 | v = get perm m |
| 90 | j = ulong add long m get dir m |
| 91 | put perm m get perm j |
| 92 | put perm j v |
| 93 | t = get dir m |
| 94 | put dir m get dir j |
| 95 | put dir j t |
| 96 | for i ulong n |
| 97 | if gt get perm i v |
| 98 | put dir i neg get dir i |
| 99 | end |
| 100 | end |
| 101 | s = neg s |
| 102 | show n s |
| 103 | end |
| 104 | end |
| 105 | note n = 3 and n = 4: 3! + 4! = 30 lines; 6! = 720 bounds the loop for any n up to 6 |
| 106 | fn main to int |
| 107 | sjt 3 |
| 108 | emit nl |
| 109 | sjt 4 |
| 110 | ret 0 |
| 111 | end |