Java Set Cardinality cardinalityInBitmapRange(long[] bitmap, int start, int end)

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

Description

Hamming weight of the bitset in the range start, start+1,..., end-1

License

Apache License

Parameter

Parameter Description
bitmap array of words representing a bitset
start first index (inclusive)
end last index (exclusive)

Return

the hamming weight of the corresponding range

Declaration

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

Method Source Code

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

public class Main {
    /**
     * Hamming weight of the bitset in the range
     *  start, start+1,..., end-1
     *
     * @param bitmap array of words representing a bitset
     * @param start first index  (inclusive)
     * @param end last index  (exclusive)
     * @return the hamming weight of the corresponding range
     */
    public static int cardinalityInBitmapRange(long[] bitmap, int start, int end) {
        if (start >= end) {
            return 0;
        }
        int firstword = start / 64;
        int endword = (end - 1) / 64;
        if (firstword == endword) {
            return Long.bitCount(bitmap[firstword] & ((~0L << start) & (~0L >>> -end)));
        }
        int answer = Long.bitCount(bitmap[firstword] & (~0L << start));
        for (int i = firstword + 1; i < endword; i++) {
            answer += Long.bitCount(bitmap[i]);
        }
        answer += Long.bitCount(bitmap[endword] & (~0L >>> -end));
        return answer;
    }
}

Related

  1. cardinality(byte b)
  2. cardinality(final int from, final int to, final long[] words)
  3. cardinality(long v)
  4. cardinalityInBitmapWordRange(long[] bitmap, int start, int end)