Java Array Copy copy(long[] v)

Here you can find the source of copy(long[] v)

Description

Copy a bitset

License

Open Source License

Parameter

Parameter Description
v Array to copy

Return

Copy

Declaration

public static long[] copy(long[] v) 

Method Source Code

//package com.java2s;
/*//w w  w .j a  v a  2  s .  c o  m
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Shift factor for a long: 2^6 == 64 == Long.SIZE
     */
    private static final int LONG_LOG2_SIZE = 6;
    /** Masking for long shifts. */
    private static final int LONG_LOG2_MASK = 0x3f;

    /**
     * Copy a bitset
     * 
     * @param v Array to copy
     * @return Copy
     */
    public static long[] copy(long[] v) {
        return Arrays.copyOf(v, v.length);
    }

    /**
     * Copy a bitset.
     * 
     * Note: Bits beyond mincap <em>may</em> be retained!
     * 
     * @param v Array to copy
     * @param mincap Target <em>minimum</em> capacity
     * @return Copy with space for at least "capacity" bits
     */
    public static long[] copy(long[] v, int mincap) {
        int words = ((mincap - 1) >>> LONG_LOG2_SIZE) + 1;
        if (v.length == words) {
            return Arrays.copyOf(v, v.length);
        }
        long[] ret = new long[words];
        System.arraycopy(v, 0, ret, 0, Math.min(v.length, words));
        return ret;
    }

    /**
     * Copy a bitset.
     * 
     * Note: Bits beyond mincap <em>may</em> be retained!
     * 
     * @param v Array to copy
     * @param mincap Target <em>minimum</em> capacity
     * @param shift Number of bits to shift left
     * @return Copy with space for at least "capacity" bits
     */
    public static long[] copy(long[] v, int mincap, int shift) {
        int words = ((mincap - 1) >>> LONG_LOG2_SIZE) + 1;
        if (v.length == words && shift == 0) {
            return Arrays.copyOf(v, v.length);
        }
        long[] ret = new long[words];
        final int shiftWords = shift >>> LONG_LOG2_SIZE;
        final int shiftBits = shift & LONG_LOG2_MASK;
        // Simple case - multiple of word size
        if (shiftBits == 0) {
            for (int i = shiftWords; i < ret.length; i++) {
                ret[i] |= v[i - shiftWords];
            }
            return ret;
        }
        // Overlapping case
        final int unshiftBits = Long.SIZE - shiftBits;
        final int end = Math.min(ret.length, v.length + shiftWords) - 1;
        for (int i = end; i > shiftWords; i--) {
            final int src = i - shiftWords;
            ret[i] |= (v[src] << shiftBits) | (v[src - 1] >>> unshiftBits);
        }
        ret[shiftWords] |= v[0] << shiftBits;
        return ret;
    }
}

Related

  1. copy(final byte[] inBytes)
  2. copy(int[] array)
  3. copy(int[] array)
  4. copy(int[][] input)
  5. copy(long[] array)
  6. copy(T[] array)
  7. copy(T[] array)
  8. copy2DArray(char[][] original)
  9. copy5DArray(char[][][][][] original)