[Algorithm] BitMap

 Transfer: https://www.seoxiehui.cn/article-45186-1.html

demand:

  To meet the statistical needs of the user tags, use a small gray Mysql designed the following table structure, each dimension of the label corresponds with a Mysql table: To all statistical programmer after 90 how to do it 

"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


Two months ago -


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


To meet the needs of users statistical labels, small gray Mysql designed using the following table structure, each dimension corresponds with a label Mysql table:


"One day algorithm" Bitmap Algorithm


To all statistical programmer after 90 how to do it?

With the intersection of a SQL statement can be:

the Select COUNT (DISTINCT the Name) AS the number of users from table whare age = after '90 'and Occupation =' programmer ';

order statistics for all users of Apple mobile phones or the sum after 00 How to do?

And with a set of SQL statements required to:

the Select COUNT (DISTINCT the Name) AS users from table whare Phone = 'apple' or age = after 00 ';


"One day algorithm" Bitmap Algorithm


Two months later -


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


———————————————


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


1. The bitmap 10 is given length, each bit correspond respectively to 10-bit integers from 0 to 9. At this point all the bitmap bits are 0.


"One day algorithm" Bitmap Algorithm


2. 4 integer stored bitmap, the storage position corresponding to the position index is 4, this bit is set to 1.


"One day algorithm" Bitmap Algorithm


3. The integer 2 is stored bitmap, the corresponding storage location is at the position labeled 2, this bit is set to 1.


"One day algorithm" Bitmap Algorithm


1 to 4. The integer stored bitmap, the storage position corresponding to the position of the index is 1, this bit is set to 1.


"One day algorithm" Bitmap Algorithm


3 to 5. The integer stored bitmap, the storage position corresponding to the position index is 3, this bit is set to 1.


"One day algorithm" Bitmap Algorithm


To ask this time bitmap in memory which elements? 4,3,2,1 clearly at a glance.

Bitmap inquiry is not only convenient, but also to get rid of duplicated integers.


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


1. To establish a mapping user name and user ID's:


"One day algorithm" Bitmap Algorithm


2. Let each label store contains all the user ID for this label, each label is an independent Bitmap.


"One day algorithm" Bitmap Algorithm


3. In this way, to achieve the user's weight and query statistics, it becomes clear:


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


1. How to find programmers to use Apple's mobile phone users?


"One day algorithm" Bitmap Algorithm


2. How do you find all male or after the 00 users?


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


one week later......


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


We have more than one user data, for example, the user basic information is as follows. By age label, it can be divided into 90, after two Bitmap 00:


"One day algorithm" Bitmap Algorithm


With more visual representation of the user's 90 Bitmap as follows:


"One day algorithm" Bitmap Algorithm


This time can be directly obtained non- user 90 after it? Direct non-op?


"One day algorithm" Bitmap Algorithm


Clearly, the non-user 90 actually only one, instead of FIG. 8 obtained results, it is not directly non-operation.


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


The same is just an example, we give the user a given Bitmap 90, to give the full amount of a given user's Bitmap. The final requirement is present in a total amount of users, but not present in the user portion 90.


"One day algorithm" Bitmap Algorithm


How to find it? We can use the XOR operation, i.e., the same bit is 0, a bit different.


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


25769803776L = 11000000000000000000000000000000000B

8589947086L = 1000000000000000000011000011001110B


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


Resolving Word0, that the current number of empty Word across RLW is 0, followed by three consecutive normal Word.

ID 2. calculate the current maximum rearward RLW continuous normal Word is 64 X (0 + 3) -1 = 191.

3. Since 191 <400 003, so the new ID inevitable next RLW (Word4) after.

4. Parse word4, that the current number of empty RLW Word for spanning 6247, followed by an ordinary consecutive Word.

5. calculate the current RLW (Word4) Maximum continuous rearward normal Word ID is 191 + (6247 + 1) X64 = 400063.

6. Since 400003 <400 063, so the new ID in the correct position 400003 of the current RLW (Word4) behind ordinary Word, which is Word5.

Insert the following final results:


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


"One day algorithm" Bitmap Algorithm


The official description is as follows:

* Though you can set the bits in any order (e.g., set(100), set(10), set(1),* you will typically get better performance if you set the bits in increasing order (e.g., set(1), set(10), set(100)).* * Setting a bit that is larger than any of the current set bit* is a constant time operation. Setting a bit that is smaller than an * already set bit can require time proportional to the compressed* size of the bitmap, as the bitmap may need to be rewritten.

"One day algorithm" Bitmap Algorithm


Some explanations:

1. The project is not the first technology selection Mysql, but memory database hana. Herein for ease of understanding, the original program written Mysq storage database.

Bitmap Optimization 1. This paper introduces a certain extent, make simplified, logical source code much more complicated. For example, the inserted data location 400,003, and practical steps are discrepancies.

2. If students are interested, you can go and read the source code, or even to try to realize their Bitmap algorithm. Though it may take a lot of time, but it really is a good way to learn.

EWAHCompressedBitmap corresponding maven dependency follows:
<dependency>
  <groupId>com.googlecode.javaewah</groupId>
  <artifactId>JavaEWAH</artifactId>
  <version>1.1.0</version>
</dependency>

 

Guess you like

Origin www.cnblogs.com/itplay/p/11089233.html