PHP implements bitmap sorting and intersection calculation, bitmap
This example describes how to implement bitmap sorting and intersection in PHP. We will share this with you for your reference. The details are as follows:
Initializes a string of all 0 binary;
An unordered integer array exists;
If integer x is in this integer array, the x position of the binary string is 1;
Then, read the binary string in sequence and convert the 1 bit to an integer. Then, store the binary string in the order of the new set, which is sorted.
Sort code:
Function sort () {// var_dump (PHP_INT_MAX, PHP_INT_SIZE); // int 9223372036854775807 // int 8 $ bitmap = array_fill (0, 50, 0 ); // apply for an integer array with 50 elements and initialize it as an integer 0 $ int_bit_size = PHP_INT_SIZE * 8; // $ bitmap: the binary digits of each INTEGER (in this example, int = 8*8 = 64bit; $ bitmap array: 50*64 = 3200 bits ), that is to say, it can be sorted for an integer set whose maximum value is less than or equal to 3200 $ a = array (200,150,300 ); // define an unordered array // scan every number in $ a and convert it to x * 64 + y foreach ($ a as $ k => $ v) {$ Shang = $ v/$ int_bit_size; $ yushu = $ v % $ int_bit_size; $ offset = 1 <$ yushu; $ bitmap [$ shang] = $ bitmap [$ shang] | $ offset; // returns the bit position 1} // restores the bit in $ bitmap to the integer output in sequence, the sorted array $ B = array (); foreach ($ bitmap as $ k => $ v) {for ($ I = 0; $ I <$ int_bit_size; $ I ++) {$ tmp = 1 <$ I; $ flag = $ tmp & $ bitmap [$ k]; // $ B [] = $ flag? $ K * $ int_bit_size + $ I: false; if ($ flag) {$ B [] = $ k * $ int_bit_size + $ I ;}} var_dump ($ B ); exit ;}
Browser output:
Array
0 => int 1
1 => int 3
2 => int 4
3 => int 34
4 => int 50
5 => int 60
6 => int 88
7 => int 100
8 => int 150
9 => int 200
10 => int 300
Intersection code:
Public function sort ($ a = array () {// var_dump (PHP_INT_MAX, PHP_INT_SIZE); // int 9223372036854775807 // int 8 $ bitmap = array_fill (0, 50, 0); // apply for an integer array with 50 elements. The value is initialized to an integer of 0 $ int_bit_size = PHP_INT_SIZE * 8; // $ bitmap: the binary digits of each INTEGER (in this example, int = 8*8 = 64bit; $ bitmap array: 50*64 = 3200 bits) // $ a = array (200,150,300,); // you can specify an array in an out-of-order and scan each number in $, convert it to x * 64 + y foreach ($ a as $ k => $ v) {$ shan G = $ v/$ int_bit_size; $ yushu = $ v % $ int_bit_size; $ offset = 1 <$ yushu; $ bitmap [$ shang] = $ bitmap [$ shang] | $ offset; // set the bit position to 1} return $ bitmap;} public function intersect () {$ int_bit_size = PHP_INT_SIZE * 8; $ a = array (200,150,300, 222,150,300,); $ B = array ); $ bit_a = $ this-> sort ($ a); $ bit_ B = $ this-> sort ($ B); $ c = array (); foreach ($ bit_a as $ k => $ V) {$ c [$ k] = $ bit_a [$ k] & $ bit_ B [$ k]; // Binary & calculates the intersection} $ d = array (); foreach ($ c as $ k =>$ v) {for ($ I = 0; $ I <$ int_bit_size; $ I ++) {$ tmp = 1 <$ I; $ flag = $ tmp & $ c [$ k]; // $ B [] = $ flag? $ K * $ int_bit_size + $ I: false; if ($ flag) {$ d [] = $ k * $ int_bit_size + $ I ;}} var_dump ($ d ); exit ;}
Browser output:
Array
0 => int 1
1 => int 3
2 => int 34
3 => int 50
4 => int 100
5 => int 150
6 => int 300