omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / ordered-partitions.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text tlp "(" |
| 10 | text trp ")" |
| 11 | text tlb "{" |
| 12 | text trb "}" |
| 13 | text tcomma ", " |
| 14 | text tpart "partitions" |
| 15 | global size ulong 4 |
| 16 | global owner ulong 8 |
| 17 | global nb ulong 1 |
| 18 | global nel ulong 1 |
| 19 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 20 | fn emit a view uchar * to void |
| 21 | o = ulong 0 |
| 22 | while lt o len a bound 4096 |
| 23 | k = write 1 a o sub len a o |
| 24 | if le k 0 |
| 25 | exit 1 |
| 26 | end |
| 27 | o = add o ulong k |
| 28 | end |
| 29 | end |
| 30 | note emitu writes n in decimal; 20 digits hold every ulong |
| 31 | fn emitu n ulong to void |
| 32 | d = new uchar 20 0 |
| 33 | i = ulong 20 |
| 34 | v = n |
| 35 | while true bound 20 |
| 36 | i = sub i 1 |
| 37 | put d i uchar add rem v 10 48 |
| 38 | v = div v 10 |
| 39 | if eq v 0 |
| 40 | break |
| 41 | end |
| 42 | end |
| 43 | emit part d i sub 20 i |
| 44 | end |
| 45 | note owner[i] is the block holding element i+1 (nb = unassigned); blocks are filled in order, each with |
| 46 | note its elements chosen as a lexicographic combination of what is left, so the output is in order |
| 47 | fn show to void |
| 48 | emit tlp |
| 49 | for b ulong get nb 0 |
| 50 | if gt b 0 |
| 51 | emit tcomma |
| 52 | end |
| 53 | emit tlb |
| 54 | first = true |
| 55 | for i ulong get nel 0 |
| 56 | if eq get owner i b |
| 57 | if not first |
| 58 | emit tcomma |
| 59 | end |
| 60 | emitu add i 1 |
| 61 | first = false |
| 62 | end |
| 63 | end |
| 64 | emit trb |
| 65 | end |
| 66 | emit trp |
| 67 | emit nl |
| 68 | end |
| 69 | note fill starts block b, or prints the partition once every block is full |
| 70 | fn fill b ulong to void |
| 71 | if eq b get nb 0 |
| 72 | show |
| 73 | ret |
| 74 | end |
| 75 | choose b get size b 0 |
| 76 | end |
| 77 | note choose gives block b `need` more elements, each larger than the last one chosen (at index >= from) |
| 78 | fn choose b ulong need ulong from ulong to void |
| 79 | if eq need 0 |
| 80 | fill add b 1 |
| 81 | ret |
| 82 | end |
| 83 | n = get nel 0 |
| 84 | for x ulong sub n from |
| 85 | i = add from x |
| 86 | if eq get owner i get nb 0 |
| 87 | put owner i b |
| 88 | choose b sub need 1 add i 1 |
| 89 | put owner i get nb 0 |
| 90 | end |
| 91 | end |
| 92 | end |
| 93 | fn run s0 ulong s1 ulong s2 ulong to void |
| 94 | put nb 0 3 |
| 95 | put size 0 s0 |
| 96 | put size 1 s1 |
| 97 | put size 2 s2 |
| 98 | put nel 0 add add s0 s1 s2 |
| 99 | for i ulong get nel 0 |
| 100 | put owner i 3 |
| 101 | end |
| 102 | emit tpart |
| 103 | emit tlp |
| 104 | emitu s0 |
| 105 | emit tcomma |
| 106 | emitu s1 |
| 107 | emit tcomma |
| 108 | emitu s2 |
| 109 | emit trp |
| 110 | emit nl |
| 111 | fill 0 |
| 112 | end |
| 113 | fn main to int |
| 114 | run 2 0 2 |
| 115 | run 1 2 1 |
| 116 | ret 0 |
| 117 | end |
| 118 | </syntaxhighlight> |
| 119 | {{out}} |
| 120 | <pre> |
| 121 | partitions(2, 0, 2) |
| 122 | ({1, 2}, {}, {3, 4}) |
| 123 | ({1, 3}, {}, {2, 4}) |
| 124 | ({1, 4}, {}, {2, 3}) |
| 125 | ({2, 3}, {}, {1, 4}) |
| 126 | ({2, 4}, {}, {1, 3}) |
| 127 | ({3, 4}, {}, {1, 2}) |
| 128 | partitions(1, 2, 1) |
| 129 | ({1}, {2, 3}, {4}) |
| 130 | ({1}, {2, 4}, {3}) |
| 131 | ({1}, {3, 4}, {2}) |
| 132 | ({2}, {1, 3}, {4}) |
| 133 | ({2}, {1, 4}, {3}) |
| 134 | ({2}, {3, 4}, {1}) |
| 135 | ({3}, {1, 2}, {4}) |
| 136 | ({3}, {1, 4}, {2}) |
| 137 | ({3}, {2, 4}, {1}) |
| 138 | ({4}, {1, 2}, {3}) |
| 139 | ({4}, {1, 3}, {2}) |
| 140 | ({4}, {2, 3}, {1}) |
| 141 | </pre> |