Java Array Swap swapBlocks(byte[] array, int fromA, int toA, int fromB, int toB)

Here you can find the source of swapBlocks(byte[] array, int fromA, int toA, int fromB, int toB)

Description

Swaps two non intersecting blocks inside an array of bytes.

License

Apache License

Parameter

Parameter Description
array The array
fromA The starting index of block A (inclusive)
toA The ending index of block B (exclusive)
fromB The starting index of block B (inclusive)
toB The ending index of block B (exclusive)

Declaration

public static void swapBlocks(byte[] array, int fromA, int toA, int fromB, int toB) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  j a  va 2  s.c  om*/
 * Copyright 2014 OpenRQ Team
 * 
 * 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 {
    /**
     * Swaps two non intersecting blocks inside an array of bytes.
     * 
     * @param array
     *            The array
     * @param fromA
     *            The starting index of block A (inclusive)
     * @param toA
     *            The ending index of block B (exclusive)
     * @param fromB
     *            The starting index of block B (inclusive)
     * @param toB
     *            The ending index of block B (exclusive)
     */
    public static void swapBlocks(byte[] array, int fromA, int toA, int fromB, int toB) {

        checkArrayBlocksRanges(fromA, toA, fromB, toB, array.length);

        final int lFrom = Math.min(fromA, fromB);
        final int lTo = Math.min(toA, toB);
        final int rFrom = Math.max(fromA, fromB);
        final int rTo = Math.max(toA, toB);

        reverseBytes(array, lFrom, lTo); // reverse the leftmost block
        reverseBytes(array, lTo, rFrom); // reverse the region between the two blocks
        reverseBytes(array, rFrom, rTo); // reverse the rightmost block

        reverseBytes(array, lFrom, rTo); // reverse the total region spanning the two blocks
    }

    /**
     * Swaps two non intersecting blocks inside an array of ints.
     * 
     * @param array
     *            The array
     * @param fromA
     *            The starting index of block A (inclusive)
     * @param toA
     *            The ending index of block B (exclusive)
     * @param fromB
     *            The starting index of block B (inclusive)
     * @param toB
     *            The ending index of block B (exclusive)
     */
    public static void swapBlocks(int[] array, int fromA, int toA, int fromB, int toB) {

        checkArrayBlocksRanges(fromA, toA, fromB, toB, array.length);

        final int lFrom = Math.min(fromA, fromB);
        final int lTo = Math.min(toA, toB);
        final int rFrom = Math.max(fromA, fromB);
        final int rTo = Math.max(toA, toB);

        reverseInts(array, lFrom, lTo); // reverse the leftmost block
        reverseInts(array, lTo, rFrom); // reverse the region between the two blocks
        reverseInts(array, rFrom, rTo); // reverse the rightmost block

        reverseInts(array, lFrom, rTo); // reverse the total region spanning the two blocks
    }

    private static void checkArrayBlocksRanges(int fromA, int toA, int fromB, int toB, int arrayLen) {

        if (fromA < 0)
            throw new IndexOutOfBoundsException("fromA < 0");
        if (toA < fromA)
            throw new IndexOutOfBoundsException("toA < fromA");
        if (arrayLen < toA)
            throw new IndexOutOfBoundsException("toA > array length");

        if (fromB < 0)
            throw new IndexOutOfBoundsException("fromB < 0");
        if (toB < fromB)
            throw new IndexOutOfBoundsException("toB < fromB");
        if (arrayLen < toB)
            throw new IndexOutOfBoundsException("toB > array length");

        if (Math.max(fromA, fromB) < Math.min(toA, toB)) {
            throw new IllegalArgumentException("the two blocks intersect");
        }
    }

    private static void reverseBytes(byte[] array, int from, int to) {

        while (from < to) {
            swapBytes(array, from++, --to);
        }
    }

    private static void reverseInts(int[] array, int from, int to) {

        while (from < to) {
            swapInts(array, from++, --to);
        }
    }

    /**
     * Swaps two bytes in an array of bytes.
     * 
     * @param array
     *            The array of bytes
     * @param a
     *            One of the indices of the values to be swapped
     * @param b
     *            One of the indices of the values to be swapped
     */
    public static void swapBytes(byte[] array, int a, int b) {

        byte tmp = array[a]; // checks for null array and illegal a index
        array[a] = array[b]; // checks for illegal b index before any changes can be made to the array
        array[b] = tmp;
    }

    /**
     * Swaps two ints in an array of ints.
     * 
     * @param array
     *            The array of ints
     * @param a
     *            One of the indices of the values to be swapped
     * @param b
     *            One of the indices of the values to be swapped
     */
    public static void swapInts(int[] array, int a, int b) {

        int tmp = array[a]; // checks for null array and illegal a index
        array[a] = array[b]; // checks for illegal b index before any changes can be made to the array
        array[b] = tmp;
    }
}

Related

  1. swapAll(final Object[] array)
  2. swapAndPrint(int[] arr, int m, int n)
  3. swapArray(int[] v, int i, int j)
  4. swapArrayStr(String[] arr)
  5. swapArrayValues(int i1, int i2, int[] indices)
  6. swapByteArray(byte[] source)
  7. swapByteOrder(byte[] a)
  8. swapByteOrder32(byte[] data, int ofs, int len)
  9. swapBytes(byte[] bytes)