首页 >后端开发 >php教程 >可变长字节码算法

可变长字节码算法

WBOY
WBOY原创
2016-07-29 08:58:461492浏览

最近在看《大规模WEB服务开发技术》这本书中。书中提到“可变长字节码算法”的压缩数据的算法,以达到压缩数据,减少磁盘IO。
可变长字节码算法:
任意一个字节的最高位(下标7)均只作为标志位,而且根据字节所在位置需要乘以128的相应幂次;

这是他的伪代码
可变长字节码算法

仔细研究后,我翻译成PHP版的:

<code><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><?php function codeNumber<span>(<span>$n</span>)</span>{
        <span>$bytes</span> = [];
        while <span>(true)</span>{
            array_unshift<span>(<span>$bytes</span>, bcmod<span>(<span>$n</span>, <span>128</span>)</span>)</span>;
            if<span>(<span>$n</span> 128</span>)</span>{
                break;
            }else{
                <span>$n</span> = intval<span>(<span>$n</span>/<span>128</span>)</span>;
            }
        }
        <span>$bytes</span>[count<span>(<span>$bytes</span>)</span> - <span>1</span>] += <span>128</span>;
        return <span>$bytes</span>;
    }

    function encode<span>(<span>$numbers</span>)</span>{
        <span>$bytestream</span> = [];
        foreach <span>(<span>$numbers</span> as <span>$n</span>)</span>{
            <span>$bytestream</span> = array_merge<span>(<span>$bytestream</span>, codeNumber<span>(<span>$n</span>)</span>)</span>;
        }
        return <span>$bytestream</span>;
    }

    function decode<span>(<span>$bytestream</span>)</span>{
        <span>$numbers</span> = [];
        <span>$n</span> = <span>0</span>;
        for <span>(<span>$i</span> = <span>0</span>; <span>$i</span> (<span>$bytestream</span>)</span>; <span>$i</span>++)</span>{
            if<span>(<span>$bytestream</span>[<span>$i</span>] 128</span>)</span>{
                <span>$n</span> = <span>128</span> * <span>$n</span> + <span>$bytestream</span>[<span>$i</span>];
            }else{
                <span>$n</span> = <span>128</span> * <span>$n</span> + <span>(<span>$bytestream</span>[<span>$i</span>] - <span>128</span>)</span>;
                array_push<span>(<span>$numbers</span>, <span>$n</span>)</span>;
                <span>$n</span> = <span>0</span>;
            }
        }
        return <span>$numbers</span>;
    }
    <span>$a</span> = encode<span>([<span>5</span>, <span>130</span>, <span>288</span>])</span>;
    var_dump<span>(<span>$a</span>)</span>;
    var_dump<span>(decode<span>(<span>$a</span>)</span>)</span>;

打印出来的内容是:
array<span>(<span>5</span>)</span> { [<span>0</span>]=> int<span>(<span>133</span>)</span> [<span>1</span>]=> string<span>(<span>1</span>)</span><span>"1"</span> [<span>2</span>]=> int<span>(<span>130</span>)</span> [<span>3</span>]=> string<span>(<span>1</span>)</span><span>"2"</span> [<span>4</span>]=> int<span>(<span>160</span>)</span> }
array<span>(<span>3</span>)</span> { [<span>0</span>]=> int<span>(<span>5</span>)</span> [<span>1</span>]=> int<span>(<span>130</span>)</span> [<span>2</span>]=> int<span>(<span>288</span>)</span> }

    //写二进制
    <span>$h</span> = fopen<span>(<span>'ejz3.txt'</span>, <span>'wb'</span>)</span>;
    foreach <span>(<span>$a</span> as <span>$k</span> => <span>$v</span>)</span>
    {
      <span>$str3</span> =  pack<span>(<span>'H*'</span>, sprintf<span>(<span>"%02x"</span>, <span>$v</span>)</span>)</span>;
      fwrite<span>(<span>$h</span>,  <span>$str3</span>)</span>;
    }
    fclose<span>(<span>$h</span>)</span>;

    //读二进制
    <span>$str2</span> = file_get_contents<span>(<span>'ejz3.txt'</span>)</span>;
    <span>$str2</span> = unpack<span>(<span>"H*"</span>, <span>$str2</span>)</span>;
    <span>$value</span> = str_split<span>(<span>$str2</span>[<span>1</span>], <span>2</span>)</span>;
    foreach <span>(<span>$value</span> as <span>$k</span> => <span>$v</span>)</span>
    {
      <span>$value</span>[<span>$k</span>] = base_convert<span>(<span>$v</span>, <span>16</span>, <span>10</span>)</span>;
    }
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了可变长字节码算法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn