1 | scope allSpreads. |
2 | |
3 | sclass #Data { |
4 | int n; |
5 | L<Int> collecting; |
6 | } |
7 | |
8 | sclass #StackEntry { |
9 | int remaining, take; |
10 | int position; // position in code |
11 | |
12 | O step(L<StackEntry> stack, Data d) { |
13 | //printStruct(stack); |
14 | if (position == 0) { |
15 | if (l(d.collecting) >= d.n-1) { |
16 | L<Int> result = listPlus(d.collecting, remaining); |
17 | popLast(stack); |
18 | ret result; |
19 | } |
20 | position++; |
21 | } |
22 | if (position == 1) { |
23 | if (take <= remaining) { |
24 | d.collecting.add(take); |
25 | new StackEntry e; |
26 | e.remaining = remaining-take; |
27 | stack.add(e); |
28 | position++; |
29 | } else |
30 | popLast(stack); |
31 | } else if (position == 2) { |
32 | removeLast(d.collecting); |
33 | take++; |
34 | position = 1; |
35 | } |
36 | null; |
37 | } |
38 | } |
39 | |
40 | static IterableIterator<L<Int>> allSpreads(final int toSpread, final int buckets) { |
41 | final new Data d; |
42 | d.n = buckets; |
43 | d.collecting = new L; |
44 | |
45 | final new L<StackEntry> stack; |
46 | new StackEntry e; |
47 | e.remaining = toSpread; |
48 | stack.add(e); |
49 | |
50 | ret iff(func { |
51 | while (nempty(stack)) { |
52 | ping(); |
53 | try object last(stack).step(stack, d); |
54 | } |
55 | ret endMarker(); |
56 | }); |
57 | } |
58 | |
59 | end scope |
Began life as a copy of #1004581
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1017275 |
Snippet name: | allSpreads - allSpreads(m, n) = all distributions of m items into n buckets |
Eternal ID of this version: | #1017275/35 |
Text MD5: | d88e29f40f3efeb67fb380e2adf1fae2 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-08-13 18:49:48 |
Source code size: | 1219 bytes / 59 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 440 / 520 |
Version history: | 34 change(s) |
Referenced in: | [show references] |