This article illustrates the method of realizing the intersection of bitmap bitmap sorting by PHP. Share to everyone for your reference, specific as follows:
Initialization of a string of 0 binary;
An array of unordered integers existing in a sequence;
If the integer x is in the array of integers, the x position of the binary string is 1;
Then sequentially reads the binary string and converts the bits of 1 to integers, sorted into 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 orthopedic array, 50 elements, initialized to integer 0 $int _bit_size = php_int_size * 8; The number of bits for each shape in the $bitmap (in this case int = 8*8 = 64bit; $bitmap array is 50*64 = 3,200 bit bits), that is, it can be sorted for an integer set that has a maximum value of less than or equal to 3200 $a = array (1,4,3,50 , 34,60,100,88,200,150,300);
Defines an ordered array//scan $a for each number, converting 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///The bit position is 1}//The bit bit in the $bitmap is sequentially restored to the integer output, the sorted array $b = Array () is obtained;
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
Ten => int 300
To find the 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 orthopedic array, 50 elements, initialized to integer 0 $int _bit_size = php_int_size * 8; The number of bits for each shape in the $bitmap (in this case int = 8*8 = 64bit; $bitmap array altogether 50*64 = 3,200 bit bits)//$a = Array (1,4,3,50,34,60,100,88,200,150, 300);
Set a sequence of arrays//scan each number in the $a, 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//The bit position is 1} return $bitmap;
The public function intersect () {$int _bit_size = php_int_size * 8;
$a = array (1,4,3,50,34,60,100,88,200,150,300);
$b = Array (1,5,3,50,34,55,100,87,222,150,300);
$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 & Compute 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
More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.