Java Bit Set setBitmapRangeAndCardinalityChange(long[] bitmap, int start, int end)

Here you can find the source of setBitmapRangeAndCardinalityChange(long[] bitmap, int start, int end)

Description

set bits at start, start+1,..., end-1 and report the cardinality change

License

Apache License

Parameter

Parameter Description
bitmap array of words to be modified
start first index to be modified (inclusive)
end last index to be modified (exclusive)

Return

cardinality change

Declaration

public static int setBitmapRangeAndCardinalityChange(long[] bitmap, int start, int end) 

Method Source Code

//package com.java2s;
/*/*from w w  w  .j a va 2  s .c  om*/
 * (c) the authors Licensed under the Apache License, Version 2.0.
 */

public class Main {
    /**
     * set bits at start, start+1,..., end-1 and report the
     * cardinality change
     * 
     * @param bitmap array of words to be modified
     * @param start first index to be modified (inclusive)
     * @param end last index to be modified (exclusive)
     * @return cardinality change
     */
    public static int setBitmapRangeAndCardinalityChange(long[] bitmap, int start, int end) {
        int cardbefore = cardinalityInBitmapWordRange(bitmap, start, end);
        setBitmapRange(bitmap, start, end);
        int cardafter = cardinalityInBitmapWordRange(bitmap, start, end);
        return cardafter - cardbefore;
    }

    /**
     * Hamming weight of the 64-bit words involved in the range
     *  start, start+1,..., end-1
     * 
     * @param bitmap array of words to be modified
     * @param start first index to be modified (inclusive)
     * @param end last index to be modified (exclusive)
     * @return the hamming weight
     */
    public static int cardinalityInBitmapWordRange(long[] bitmap, int start, int end) {
        if (start == end) {
            return 0;
        }
        int firstword = start / 64;
        int endword = (end - 1) / 64;
        int answer = 0;
        for (int i = firstword; i <= endword; i++) {
            answer += Long.bitCount(bitmap[i]);
        }
        return answer;
    }

    /**
     * set bits at start, start+1,..., end-1
     * 
     * @param bitmap array of words to be modified
     * @param start first index to be modified (inclusive)
     * @param end last index to be modified (exclusive)
     */
    public static void setBitmapRange(long[] bitmap, int start, int end) {
        if (start == end) {
            return;
        }
        int firstword = start / 64;
        int endword = (end - 1) / 64;
        if (firstword == endword) {
            bitmap[firstword] |= (~0L << start) & (~0L >>> -end);
            return;
        }
        bitmap[firstword] |= ~0L << start;
        for (int i = firstword + 1; i < endword; i++) {
            bitmap[i] = ~0L;
        }
        bitmap[endword] |= ~0L >>> -end;
    }
}

Related

  1. setBitByPos(byte byt, boolean bool, int pos)
  2. setBitInInt(int bits, int bitIndex, boolean flag)
  3. setBitInLong(long l, long n, int v)
  4. setBitLE(byte[] data, int index)
  5. setBitmapRange(long[] bitmap, int start, int end)
  6. setBitRange(final int val, final int start, final int len, final int newVal)
  7. setBits(byte in, byte data, int position, int fillBits)
  8. setBits(final byte value, final int bitMask, final boolean val)
  9. setBits(int lowBit, int numBits)