首页 > php教程 > PHP源码 > 正文

Lzw算法实现php-lzw优化版

PHP中文网
发布: 2016-05-25 17:07:58
原创
1556人浏览过

[PHP]代码 

<?php

/**
 * @link http://www.php.cn/
 * @author Jakub Vrana, http://www.php.cn/
 * @copyright 2009 Jakub Vrana
 * @license http://www.php.cn/ Apache License, Version 2.0
 */

/** LZW compression
 * @param string data to compress
 * @return string binary data
 */
function lzw_compress($string) {
    // compression
    $dict = array_flip(range("\0", "\xFF"));
    $dict_size = 256;
    $word = $string[0];

    $dict_count = 256;
    $bits = 8; 
    $bits_max = 256;
    $return = "";
    $rest = 0;
    $rest_length = 0;

    for ($i = 1, $j = strlen($string); $i < $j; $i++) {
        $x = $string[$i];
        $y = $word . $x;
        if (isset($dict[$y])) {
            $word .= $x;
        } else {
            $rest = ($rest << $bits) + $dict[$word];
            $rest_length += $bits;
            $dict_count++;
            if ($dict_count > $bits_max) {
                $bits_max = 1 << ++$bits;
            }
            while ($rest_length > 7) {
                $rest_length -= 8;
                $return .= chr($rest >> $rest_length);
                $rest &= (1 << $rest_length) - 1;
            }
            $dict[$y] = $dict_size++;
            $word = $x;
        }
    }

    $rest = ($rest << $bits) + $dict[$word];
    $rest_length += $bits;
    $dict_count++;
    if ($dict_count > $bits_max) {
        $bits_max = 1 << ++$bits;
    }
    while ($rest_length > 0) {
        if($rest_length>7){
            $rest_length -= 8;
            $return .= chr($rest >> $rest_length);
            $rest &= (1 << $rest_length) - 1;
        }else{
            $return .= chr($rest << (8 - $rest_length));
            $rest_length = 0;
        }
    }

    return $return;
}

/** LZW decompression
 * @param string compressed binary data
 * @return string original data
 */
function lzw_decompress($binary) {
    // convert binary string to codes
    $rest = 0;
    $rest_length = 0;
    $out_count = 257;
    $bits = 9;
    $bits_max = 512;
    
    // decompression
    $dict = range("\0", "\xFF");
    $w = $binary[0];
    $return = $w;
    
    for ($i = 1, $j = strlen($binary); $i < $j; $i++) {
        $rest = ($rest << 8) + ord($binary[$i]);
        $rest_length += 8;
        if ($rest_length >= $bits) {
            $rest_length -= $bits;
            
            // decompression
            $e = $dict[$rest >> $rest_length];
            if (!isset($e)) {
                $e = $w . $w[0];
            }
            $return .= $e;
            $dict[] = $w . $e[0];
            $w = $e;
            //--decompression
            
            $rest &= (1 << $rest_length) - 1;
            if (++$out_count > $bits_max) {
                $bits_max = 1 << ++$bits;
            }
        }
    }
    
    return $return;
}

?>
登录后复制

                   

超级简历WonderCV
超级简历WonderCV

免费求职简历模版下载制作,应届生职场人必备简历制作神器

超级简历WonderCV 271
查看详情 超级简历WonderCV

                   

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号