@Override
  public int[] next() {
    if (numLeft == 0) {
      throw new NoSuchElementException();
    }

    if (numLeft == total) {
      numLeft--;
      return ArrayUtils.copyOf(permutation, permutation.length);
    }

    // Find largest index j with a[j] < a[j+1]
    int j = permutation.length - 2;
    while (permutation[j] > permutation[j + 1]) {
      j--;
    }

    // Find index k such that a[k] is smallest integer
    // greater than a[j] to the right of a[j]
    int k = permutation.length - 1;
    while (permutation[j] > permutation[k]) {
      k--;
    }

    // Interchange a[j] and a[k]
    int temp = permutation[k];
    permutation[k] = permutation[j];
    permutation[j] = temp;

    // Put tail end of permutation after jth position in increasing order
    int r = permutation.length - 1;
    int s = j + 1;

    while (r > s) {
      temp = permutation[s];
      permutation[s] = permutation[r];
      permutation[r] = temp;
      r--;
      s++;
    }

    numLeft--;
    return ArrayUtils.copyOf(permutation, permutation.length);
  }
Similar code snippets
1.
Match rating: 83.25% · See similar code snippets
java logo
public int test(Permutation permutation) {
        for (int i = 0; i < size; i++) {
            int x = permutation.get(base.get(i));
            Permutation h = permutations[i][x];
            if (h == null) {
                return i;
            } else {
                permutation.setTo(h.invert().multiply(permutation));
            }
        }
        return size;
    }
2.
biojava/biojava · PermutationGroup.java
Match rating: 82.65% · See similar code snippets
java logo
public void addPermutation(List<Integer> permutation) {
		if (!permutations.contains(permutation)) {
			permutations.add(permutation);
		}
	}
3.
Match rating: 82.55% · See similar code snippets
java logo
public Permutation multiply(Permutation other) {
        Permutation newPermutation = new Permutation(values.length);
        for (int i = 0; i < values.length; i++) {
            newPermutation.values[i] = this.values[other.values[i]];
        }
        return newPermutation;
    }
4.
skjolber/3d-bin-container-packing · PermutationRotationIterator.java
Match rating: 81.75% · See similar code snippets
java logo
public boolean nextPermutation() {
		resetRotations();

	    // Find longest non-increasing suffix

	    int i = permutations.length - 1;
	    while (i > 0 && permutations[i - 1] >= permutations[i])
	        i--;
	    // Now i is the head index of the suffix

	    // Are we at the last permutation already?
	    if (i <= 0) {
	        return false;
	    }

	    // Let array[i - 1] be the pivot
	    // Find rightmost element that exceeds the pivot
	    int j = permutations.length - 1;
	    while (permutations[j] <= permutations[i - 1])
	        j--;
	    // Now the value array[j] will become the new pivot
	    // Assertion: j >= i

	    // Swap the pivot with j
	    int temp = permutations[i - 1];
	    permutations[i - 1] = permutations[j];
	    permutations[j] = temp;

	    // Reverse the suffix
	    j = permutations.length - 1;
	    while (i < j) {
	        temp = permutations[i];
	        permutations[i] = permutations[j];
	        permutations[j] = temp;
	        i++;
	        j--;
	    }

	    // Successfully computed the next permutation
	    return true;
	}
5.
apache/groovy · fannkuch.java
Match rating: 81.26% · See similar code snippets
java logo
private static final void next_perm(final int[] permutation, int position)
    {
        int perm0 = permutation[0];

        for (int i = 0; i < position; ++i)
            permutation[i] = permutation[i + 1];
        permutation[position] = perm0;
    }
6.
biojava/biojava · SystematicSolver.java
Match rating: 79.7% · See similar code snippets
java logo
private void solve() {
		initialize();
		int n = subunits.getSubunitCount();
		PermutationGenerator g = new PermutationGenerator(n);

		// loop over all permutations
		while (g.hasMore()) {
			int[] perm = g.getNext();
			List<Integer> permutation = new ArrayList<Integer>(perm.length);
			for (int j = 0; j < n; j++) {
				permutation.add(perm[j]);
			}

			if (! isValidPermutation(permutation)) {
				continue;
			}

			boolean newPermutation = evaluatePermutation(permutation);
			if (newPermutation) {
				completeRotationGroup();
			}

			if (rotations.getOrder() >= subunits.getSubunitCount()) {
				return;
			}
		}
	}
7.
biojava/biojava · RotationSolver.java
Match rating: 79.24% · See similar code snippets
java logo
private Rotation isValidPermutation(List<Integer> permutation) {
		if (permutation.size() == 0) {
			return null;
		}

		// cached value
		if (evaluatedPermutations.containsKey(permutation)) {
			return evaluatedPermutations.get(permutation);
		}

		// check if permutation is allowed
		if (! isAllowedPermutation(permutation)) {
			evaluatedPermutations.put(permutation, null);
			return null;
		}

		// check if superimposes
		Rotation rot = superimposePermutation(permutation);
		return rot;
	}
8.
cdk/cdk · Partition.java
Match rating: 79.14% · See similar code snippets
java logo
public Permutation toPermutation() {
        Permutation p = new Permutation(this.size());
        for (int i = 0; i < this.size(); i++) {
            p.set(i, this.cells.get(i).first());
        }
        return p;
    }
9.
validator/validator · PermutingAttributesWrapper.java
Match rating: 78.98% · See similar code snippets
java logo
public void pushDown(String uri, String localName) {
        int index = getIndex(uri, localName);
        if (index < 0 || index == permutation.length - 1) {
            return;
        }
        int temp = permutation[index];
        System.arraycopy(permutation, index + 1, permutation, index, permutation.length - 1 - index);
        permutation[permutation.length - 1] = temp;
    }
10.
biojava/biojava · PermutationGroup.java
Match rating: 77.76% · See similar code snippets
java logo
public static int getOrder(List<Integer> permutation) {
		List<Integer> copy = new ArrayList<Integer>(permutation);
		for (int i = 0, n = permutation.size(); i < n; i++) {
			copy = combine(copy, permutation);
			if (copy.equals(permutation)) {
				return i + 1;
			}
		}
		return 0;
	}