在做位图索引时会遇到要将程序压缩和解压的问题,主要是为了让数据占的空间更小,下边是两个处理压缩和解压的程序欢迎大家批评指导
压缩:将$bitmap = ‘000111000000’; 数据转换为 $bitmap = ‘031306’; 反之将其解压为原始格式
压缩程序:
$bitmap = ‘000111000000’;
$result = compression($bitmap);
echo $result;function compression($bitmap)
{
$index = $count = 0;
$prev_bit = ”;
while(isset($bitmap[$index]))
{
if($bitmap[$index] != $prev_bit)
{
if(!empty($count))
$result .= $count;
$result .= $bitmap[$index];
$prev_bit = $bitmap[$index];
$count = 0;
}
$count++;
$index++;
}
$result .= $count;
return $result;
}
解压:
$bitmap = ‘031306’;
uncompression($bitmap);function uncompression($bitmap)
{
$index = 0; $currentstr = ”;
while(isset($bitmap[$index]))
{
if($index%2 != 0)
{
$count = $bitmap[$index];
$currentstr = $bitmap[$index-1];
$result .= str_repeat($currentstr,$count);
}
$index++;
}
return $result;
}