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

35
LINES

< > BotCompany Repo | #1010342 // allPermutations_iterator - return all permutations of a list as iterator [old]

JavaX fragment (include) [tags: archive]

static <A> IterableIterator<L<A>> allPermutations_iterator(L<A> theList) {
  ret iteratorFromFunction(new F0<A> {
    L<A> list = cloneList(theList);
    int[] stack = new int[l(theList)+1];
    int n = l(theList);
    
    A get() {
      findNext();
      if (n <= 1) {
        ++n; // return from subroutine
        ret cloneList(list);
      } else if (stack[n] < n)
        ++stack[n];
      }
      L<A> l = cloneList(list);
      
      if (stackPtr[n] < n)
        ++stackPtr[n];
      ret l;
    }
  };
  new LL<A> out;
  allPermutations_impl(cloneList(l), l(l), out);
  ret out;
}

static <A> void allPermutations_impl(L<A> l, int n, LL<A> out) {
  if (n <= 1) {
    ping();
    out.add(cloneList(l));
  } else for(int i = 0; i < n; i++) {
    allPermutations_impl(l, n-1, out);
    swapElements(l, n % 2 == 0 ? i : 0, n-1);
  }
}

Author comment

Began life as a copy of #1010320

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): cfunsshuasjs, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1010342
Snippet name: allPermutations_iterator - return all permutations of a list as iterator [old]
Eternal ID of this version: #1010342/3
Text MD5: aba1fc9712d08f2fb8507c2e8f7e2f63
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2019-07-15 02:25:32
Source code size: 874 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 326 / 331
Version history: 2 change(s)
Referenced in: [show references]