Home > Backend Development > PHP Tutorial > Variable length bytecode algorithm

Variable length bytecode algorithm

WBOY
Release: 2016-07-29 08:58:46
Original
1491 people have browsed it

Recently I was reading the book "Large Scale WEB Service Development Technology". The book mentions the data compression algorithm of "Variable Length Bytecode Algorithm" to compress data and reduce disk IO.
Variable length bytecode algorithm:
The highest bit of any byte (subscript 7) is only used as a flag bit, and it needs to be multiplied by the corresponding power of 128 according to the position of the byte;

This is his pseudo code
Variable length bytecode algorithm

After careful study , I translated it into the PHP version:

<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>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the variable length bytecode algorithm, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template