Here you can find the source of shiftLeftI(long[] v, int off)
Parameter | Description |
---|---|
v | existing bitset |
off | Offset to shift by |
public static long[] shiftLeftI(long[] v, int off)
//package com.java2s; /*// w w w. j a v a 2 s. co 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; /** * Shift a long[] bitset inplace. * * Low-endian layout for the array. * * @param v existing bitset * @param off Offset to shift by * @return Bitset */ public static long[] shiftLeftI(long[] v, int off) { if (off == 0) { return v; } if (off < 0) { return shiftRightI(v, -off); } // Break shift into integers to shift and bits to shift final int shiftWords = off >>> LONG_LOG2_SIZE; final int shiftBits = off & LONG_LOG2_MASK; if (shiftWords >= v.length) { return zeroI(v); } // Simple case - multiple of word size if (shiftBits == 0) { // Move whole words up System.arraycopy(v, 0, v, shiftWords, v.length - shiftWords); // Fill the initial words with zeros Arrays.fill(v, 0, shiftWords, 0); return v; } // Overlapping case final int unshiftBits = Long.SIZE - shiftBits; // Top-Down to not overlap the operations. for (int i = v.length - 1; i > shiftWords; i--) { final int src = i - shiftWords; v[i] = (v[src] << shiftBits) | (v[src - 1] >>> unshiftBits); } v[shiftWords] = v[0] << shiftBits; // Fill the initial words with zeros Arrays.fill(v, 0, shiftWords, 0); return v; } /** * Shift a long[] bitset inplace. * * Low-endian layout for the array. * * @param v existing bitset * @param off Offset to shift by * @return Bitset */ public static long[] shiftRightI(long[] v, int off) { if (off == 0) { return v; } if (off < 0) { return shiftLeftI(v, -off); } // Break shift into integers to shift and bits to shift final int shiftWords = off >>> LONG_LOG2_SIZE; final int shiftBits = off & LONG_LOG2_MASK; if (shiftWords >= v.length) { return zeroI(v); } // Simple case - multiple of word size if (shiftBits == 0) { // Move whole words down System.arraycopy(v, shiftWords, v, 0, v.length - shiftWords); // Fill top words with zeros Arrays.fill(v, v.length - shiftWords, v.length, 0); return v; } // Overlapping case final int unshiftBits = Long.SIZE - shiftBits; // Bottom-up to not overlap the operations. for (int i = 0; i < v.length - shiftWords - 1; i++) { final int src = i + shiftWords; v[i] = (v[src + 1] << unshiftBits) | (v[src] >>> shiftBits); } // The last original word v[v.length - shiftWords - 1] = v[v.length - 1] >>> shiftBits; // Fill whole words "lost" by the shift Arrays.fill(v, v.length - shiftWords, v.length, 0); return v; } /** * Zero the given set * * Low-endian layout for the array. * * @param v existing set * @return array set to zero */ public static long[] zeroI(long[] v) { Arrays.fill(v, 0); return v; } }