Warning: session_start(): open(/var/lib/php/sessions/sess_bh25mia091pmffouc1f62vch9i, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
static IterableIterator allPermutations_iterator(L theList) {
ret new IterableIterator {
O[] next;
int n = l(theList);
O[] perm;
int[] dirs;
{
if (n > 0) {
perm = toObjectArray(theList);
dirs = new int[n];
for(int i = 0; i < n; i++) {
dirs[i] = -1;
}
dirs[0] = 0;
}
next = perm;
}
@Override
public int[] next() {
int[] r = makeNext();
next = null;
return r;
}
@Override
public boolean hasNext() {
return (makeNext() != null);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
O[] makeNext() {
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];
swap(i, k, dirs);
swap(i, k, perm);
// 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);
}
};
}