Java implementation Bitmap

bitmap is useful structures. The so-called bitmap is to use one bit to mark an element, but the array subscript is that element . bitmap often used in big data problems, such as one billion the number of type int, if int array to store, then takes about 4G memory, memory is wasted. If the solution with a bitmap, it is more convenient. int bitmap can be used to simulate, can also be used to simulate the byte, it is only logical concept, in java language to write, we use both analog and byte int.

The method uses byte :( reprinted from the realization of Java in Bitmap )

A byte representing 8 bit, each bit value if there is or not, i.e., 1 or 0, as shown below:

For this example, it does not explain the original, individual understood: the length of the byte array is not dependent on the maximum value of int array, for example, where the maximum is 11, the length byte array equals 11/8 + 1 = 2. But this seems to be the starting point of any connection with the contradiction between the length of the large data ( write this blog when this place has yet to be confirmed )

bitmap code implementation

Step: Construction of a particular byte array (new byte [capacity / 8 + 1]) length, wherein the capacity is a integer array length (eg: 1000000000 numbers, etc.)

byte[] bits = new byte[getIndex(n) + 1];

If the figure above is an example, the length of 1 byte.

The second step: calculating the numerical position num byte [] in the (num / 8 >> 3 and the same num), that is to say in a num byte [k], k is a count that several

/**
     * num/8得到byte[]的index
     * @param num
     * @return
     */
    public int getIndex(int num){
        return num >> 3;
    }

Similarly, for the example of the figure, the first element int array 1, corresponding to the 0 byte of the array, i.e., byte [0] in this sense can be referred to as a "line" within.

Third step: calculating the number num byte positions [index] is, that is, byte [index] in the first of several, each byte has eight (num% 8)

/**
     * num%8得到在byte[index]的位置
     * @param num
     * @return
     */
    public int getPosition(int num){
        return num % 8;
    }

Similarly, for the example of the figure, the first element int array 1, corresponding to the 0 byte of the array, i.e., byte [0] in this sense can be referred to as a "line" within. Then, the modulus corresponding to byte [0] bit 1.

Step 4: location changed from 0 to 1, other positions unchanged

/**
     * 标记指定数字(num)在bitmap中的值,标记其已经出现过
     * 将1左移position后,那个位置自然就是1,然后和以前的数据做|,这样,那个位置就替换成1了
     * @param bits
     * @param num
     */
    public void add(byte[] bits, int num){
        bits[getIndex(num)] |= 1 << getPosition(num);
    }

bits [0] has 8 'bit, the value of the getPosition (1) is 1, the result of a left shift is a binary "1" bit position 1 (an integer of 1 0000 0001, left after a change 0000 0010). At the same time, or after the operation done, if a bit value is changed to 1, that the arithmetic operations or to replace one of the positions.

Step five: to determine whether there is a specified number num

/**
     * 判断指定数字num是否存在<br/>
     * 将1左移position后,那个位置自然就是1,然后和以前的数据做&,判断是否为0即可
     * @param bits
     * @param num
     * @return
     */
    public boolean contains(byte[] bits, int num){
        return (bits[getIndex(num)] & 1 << getPosition(num)) != 0;
    }

Step Six: Reset the bitmap corresponds to a digital value

/**
     * 重置某一数字对应在bitmap中的值<br/>
     * 对1进行左移,然后取反,最后与byte[index]作与操作。
     * @param bits
     * @param num
     */
    public void clear(byte[] bits, int num){
        bits[getIndex(num)] &= ~(1 << getPosition(num));
    }

All code is as follows:

public class Test {

    /**
     * 创建bitmap数组
     */
    public byte[] create(int n){
        byte[] bits = new byte[getIndex(n) + 1];
        
        for(int i = 0; i < n; i++){
            add(bits, i);
        }
        
        System.out.println(contains(bits, 11));
        
        int index = 1;
        for(byte bit : bits){
            System.out.println("-------" + index++ + "-------");
            showByte(bit);

        }
        
        return bits;
    }
    
    /**
     * 标记指定数字(num)在bitmap中的值,标记其已经出现过<br/>
     * 将1左移position后,那个位置自然就是1,然后和以前的数据做|,这样,那个位置就替换成1了
     * @param bits
     * @param num
     */
    public void add(byte[] bits, int num){
        bits[getIndex(num)] |= 1 << getPosition(num);
    }
    
    /**
     * 判断指定数字num是否存在<br/>
     * 将1左移position后,那个位置自然就是1,然后和以前的数据做&,判断是否为0即可
     * @param bits
     * @param num
     * @return
     */
    public boolean contains(byte[] bits, int num){
        return (bits[getIndex(num)] & 1 << getPosition(num)) != 0;
    }
    
    /**
     * num/8得到byte[]的index
     * @param num
     * @return
     */
    public int getIndex(int num){
        return num >> 3;
    }
    
    /**
     * num%8得到在byte[index]的位置
     * @param num
     * @return
     */
    public int getPosition(int num){
        return num & 0x07;
    }
    
    /**
     * 重置某一数字对应在bitmap中的值<br/>
     * 对1进行左移,然后取反,最后与byte[index]作与操作。
     * @param bits
     * @param num
     */
    public void clear(byte[] bits, int num){
        bits[getIndex(num)] &= ~(1 << getPosition(num));
    }
    
    /**
     * 打印byte类型的变量<br/>
     * 将byte转换为一个长度为8的byte数组,数组每个值代表bit
     */
    
    public void showByte(byte b){
        byte[] array = new byte[8];
        for(int i = 7; i >= 0; i--){
            array[i] = (byte)(b & 1);
            b = (byte)(b >> 1);
        }
        
        for (byte b1 : array) {
            System.out.print(b1);
            System.out.print(" ");
        }
        
        System.out.println();
    }
    
    public static void main(String[] args) {
        int n = 100;
        new Test().create(n);
    }
}

The results are shown, a total of 100 numbers: (PS this case seems to run is not very good, so can not tell what)

Int methods employed: The thinking byte, because int is 32 bits, so just put into 8 to 32.

Published 61 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_33204444/article/details/94738210