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]

1  
static <A> IterableIterator<L<A>> allPermutations_iterator(L<A> theList) {
2  
  ret iteratorFromFunction(new F0<A> {
3  
    L<A> list = cloneList(theList);
4  
    int[] stack = new int[l(theList)+1];
5  
    int n = l(theList);
6  
    
7  
    A get() {
8  
      findNext();
9  
      if (n <= 1) {
10  
        ++n; // return from subroutine
11  
        ret cloneList(list);
12  
      } else if (stack[n] < n)
13  
        ++stack[n];
14  
      }
15  
      L<A> l = cloneList(list);
16  
      
17  
      if (stackPtr[n] < n)
18  
        ++stackPtr[n];
19  
      ret l;
20  
    }
21  
  };
22  
  new LL<A> out;
23  
  allPermutations_impl(cloneList(l), l(l), out);
24  
  ret out;
25  
}
26  
27  
static <A> void allPermutations_impl(L<A> l, int n, LL<A> out) {
28  
  if (n <= 1) {
29  
    ping();
30  
    out.add(cloneList(l));
31  
  } else for(int i = 0; i < n; i++) {
32  
    allPermutations_impl(l, n-1, out);
33  
    swapElements(l, n % 2 == 0 ? i : 0, n-1);
34  
  }
35  
}

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: 391 / 395
Version history: 2 change(s)
Referenced in: [show references]