interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / ordered-partitions.wiki

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