首頁 > 後端開發 > php教程 > 可變長字節碼演算法

可變長字節碼演算法

WBOY
發布: 2016-07-29 08:58:46
原創
1491 人瀏覽過

最近在看《大規模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教程有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板