Java Array Shift shift(double[] x, int N)

Here you can find the source of shift(double[] x, int N)

Description

shift

License

Open Source License

Declaration

public static double[] shift(double[] x, int N) 

Method Source Code

//package com.java2s;
/**/*from  w ww.  j av a 2 s  . co  m*/
 * Copyright 2000-2009 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * This file is part of MARY TTS.
 *
 * MARY TTS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    public static double[] shift(double[] x, int N) {
        if (N == 0)
            return x;

        double[] y = new double[x.length];
        int i;
        if (N > 0) {
            for (i = 0; i < N; i++)
                y[i] = x[0];
            for (i = N; i < x.length - N; i++)
                y[i] = x[i - N];
        } else {
            N = -1 * N;
            for (i = 0; i < x.length - N; i++)
                y[i] = x[i + N];
            for (i = x.length - N; i < x.length; i++)
                y[i] = x[x.length - 1];
        }

        return y;
    }

    public static float[] shift(float[] x, int N) {
        if (N == 0)
            return x;

        float[] y = new float[x.length];
        int i;

        if (N > 0) // Shift right
        {
            for (i = 0; i < N; i++)
                y[i] = x[0];
            for (i = N; i < x.length; i++)
                y[i] = x[i - N];
        } else // Shift left
        {
            N = -1 * N;
            for (i = 0; i < x.length - N; i++)
                y[i] = x[i + N];
            for (i = x.length - N; i < x.length; i++)
                y[i] = x[x.length - 1];
        }

        return y;
    }
}

Related

  1. arrayShift(Object[] input, int shift)
  2. arrayShiftArgs(int[] array, int a)
  3. shift(double[] a, int shift, double insert)
  4. shift(final byte[] input, final int amount)
  5. shift(final double[] values, final double constant)
  6. shift(final Object[] array, int offset)
  7. Shift(int[] in, int amt, int spare)