bitmap bitmap principle and

Primer

First by a question to understand what is bitmap.

Topic: I have a 4 billion integers, to give a new integer, I need to determine whether the new integer integer 4 billion, how would you do?

analysis:

Suppose an int 4 bytes (32-bit), 40 million integers is 16 billion bytes, equivalent to about 16GB, 2GB assumed that only one computer memory, a 16GB finish loading, requires loading points 8 , data is loaded from disk disk io operation is very slow (more than memory operations to be 100 times slower), each time such a large data loads, and to eight times, then the time to find up to minutes or even hours of class.

Another way is to put data scattered on eight computers and data to a new, eight computers at the same time to find and then aggregated results. Thus, each computer can be a one-time data into memory, do not look back and forth to load data, eliminating the overhead of loading data, it is a good way.

But are there any other better way to do that? That's bitmap. stored-value bitmap idea: each 32-bit int, int integer is the range -2147483648 to 2147483647. To simplify the understanding, assume that there are both positive integers each integer (negative integer required separately if present), 2147483647/32 = 67108863, 67108863 i.e. only integer type int can represent numerical range [0,2147483647] , the need 67108863 * 4 = 268,435,452 bytes of memory, is equivalent to 0.2GB, even with a negative integer portion is also required 0.4GB only memory, a computer completely sufficient. This will open up 67108863int array, the array of each representative in turn on behalf of [0,2147483647]. And determining new integer and need only O (1) time complexity, very high performance.

bitmap definitions

Each bit map is a data bit of each data represents an array of 0 indicates absence of data, data indicating a presence.

This number 136 stores, for example:

  1. In section 136 determines that the entire data of 136/32 = 4, i.e., in the fourth section;
  2. In the first of several 136 determines this interval (bit), 136% 32 = 25, i.e., the position 25 in the fourth section;
  3. This position will be set to 1, indicating the presence of this number.

Since the bitmap data is stored, in ascending order of nature.

Code

#include <the iostream> 
#include <Vector> the using namespace STD; class Bitmap {
 public : 
    Bitmap () { 
        bitVec_.resize ((INT_MAX >> . 5 ) + . 1 );   // open up more space, because of the array showing only interval [0, size)     }
     void BitmapSet ( int Val) {
         int index = Val >> . 5 ;   // equivalent to dividing the 32, the shift operation can improve the performance int offset =% Val 32 ; 
        bitVec_ [index] | = ( . 1 << offset);
         int

 



         capacity = bitVec_.capacity();
    }
    bool BitmapGet(int val) {
        int index = val >> 5;
        int offset = val % 32;
        return bitVec_[index] & (1 << offset);
    }
private:
    vector<unsigned int> bitVec_;
};

int main() {
    Bitmap bm;
    //这里只存[0,1000000]的数,
    for (int i = 0; i <= 1000000; ++I) { 
        bm.BitmapSet (I); 
    } 
    BOOL exist1 = bm.BitmapGet ( 100 );        // 100 if there are, returns to true 
    BOOL exist2 bm.BitmapGet = ( 10000000 );   // 10000000 exists returns to false 

    System ( " PAUSE " );
     return  0 ; 
}

 

 

Guess you like

Origin www.cnblogs.com/evenleee/p/12000529.html