static IterableIterator allPermutations_iterator(final L theList) { ret new IterableIterator() { int[] next; int n = l(theList); int[] perm, dirs; { if (n > 0) { perm = new int[n]; dirs = new int[n]; for(int i = 0; i < n; i++) { perm[i] = i; dirs[i] = -1; } dirs[0] = 0; } next = perm; } @Override public O[] next() { int[] r = makeNext(); next = null; O[] out = new O[n]; for i to n: out[i] = theList.get(r[i]); ret out; } @Override public boolean hasNext() { return (makeNext() != null); } @Override public void remove() { throw new UnsupportedOperationException(); } int[] makeNext() { ping(); if (next != null) return next; if (perm == null) return null; // find the largest element with != 0 direction int i = -1, e = -1; for(int j = 0; j < n; j++) if ((dirs[j] != 0) && (perm[j] > e)) { e = perm[j]; i = j; } if (i == -1) // no such element -> no more premutations return (next = (perm = (dirs = null))); // no more permutations // swap with the element in its direction int k = i + dirs[i]; swapIntArray(dirs, i, k); swapIntArray(perm, i, k); // if it's at the start/end or the next element in the direction // is greater, reset its direction. if ((k == 0) || (k == n-1) || (perm[k + dirs[k]] > e)) dirs[k] = 0; // set directions to all greater elements for(int j = 0; j < n; j++) if (perm[j] > e) dirs[j] = (j < k) ? +1 : -1; return (next = perm); } }; }