Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

59
LINES

< > BotCompany Repo | #1017275 // allSpreads - allSpreads(m, n) = all distributions of m items into n buckets

JavaX fragment (include)

scope allSpreads.

sclass #Data {
  int n;
  L<Int> collecting;
}

sclass #StackEntry {
  int remaining, take;
  int position; // position in code
  
  O step(L<StackEntry> stack, Data d) {
    //printStruct(stack);
    if (position == 0) {
      if (l(d.collecting) >= d.n-1) {
        L<Int> result = listPlus(d.collecting, remaining);
        popLast(stack);
        ret result;
      }
      position++;
    }
    if (position == 1) {
      if (take <= remaining) {
        d.collecting.add(take);
        new StackEntry e;
        e.remaining = remaining-take;
        stack.add(e);
        position++;
      } else
        popLast(stack);
    } else if (position == 2) {
      removeLast(d.collecting);
      take++;
      position = 1;
    }
    null;
  }
}

static IterableIterator<L<Int>> allSpreads(final int toSpread, final int buckets) {
  final new Data d;
  d.n = buckets;
  d.collecting = new L;
  
  final new L<StackEntry> stack;
  new StackEntry e;
  e.remaining = toSpread;
  stack.add(e);
  
  ret iff(func {
    while (nempty(stack)) {
      ping();
      try object last(stack).step(stack, d);
    }
    ret endMarker();
  });
}

end scope

Author comment

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: 439 / 519
Version history: 34 change(s)
Referenced in: [show references]