Java Array Permute permute(int[] p, Object[] data, boolean clone)

Here you can find the source of permute(int[] p, Object[] data, boolean clone)

Description

Rearrange the specified data according to the specified permutation.

License

Open Source License

Parameter

Parameter Description
data data to be permuted
p the permutation
clone if true, rearrange a clone instead of the original data;

Return

the permuted array (which is the original reference if clone == false)

Declaration

public static Object[] permute(int[] p, Object[] data, boolean clone) 

Method Source Code

//package com.java2s;
/*/*from w  ww.  j  a va2  s. com*/
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

public class Main {
    /**
    * Rearrange the specified data according to the specified permutation.
    * That is, the array is rearranged, such that
    * <code>data_after[p[i]] == data_before[i]</code>.
    * @param data data to be permuted
    * @param p the permutation
    * @param clone if true, rearrange a clone instead of the original data;
    * @return the permuted array (which is the original reference if clone == false)
    */
    public static Object[] permute(int[] p, Object[] data, boolean clone) {
        Object[] permuted = null;

        if (clone) {
            permuted = (Object[]) data.clone();
            for (int i = 0; i < data.length; i++)
                permuted[p[i]] = data[i];
        } else {
            // run thru cycles
            int i = 0;
            while (i < p.length) {
                if (p[i] < 0 || p[i] == i) // skip already handled and cycles of length 1
                    ++i;
                else { // start a new cycle
                    int j = p[i];
                    Object save = data[i];
                    while (p[j] >= 0) {
                        Object tmp = data[j];
                        data[j] = save;
                        save = tmp;
                        i = j;
                        j = p[j];
                        p[i] = -1;
                    }
                }
            }
            permuted = data;
        }
        return permuted;
    }
}

Related

  1. permutation(final int start, final int end)
  2. permutation(int n)
  3. permute(int k, Object[] os)
  4. permute(int[] elements, int i, int j)
  5. permute(int[] in, int[] idx)
  6. permute(String str, int startIndex, int endIndex)
  7. permute(T[] array)
  8. permute(T[] values)
  9. permuteArray(int[] array, Integer[] permutation)