質問

C で書かれた LZW コンプレッサー/デコンプレッサーがあります。

初期テーブルは ASCII 文字で構成され、テーブルに保存される各文字列は、 接頭辞 そして キャラクター どちらも int としてリストに保存されます。

圧縮は機能しますが、解凍すると一部の文字が抜けてしまいます。

入力:

<title>Agile</title><body><h1>Agile</h1></body></html>

得られる出力(「e」と「<」が欠落していることに注意してください):

<title>Agile</title><body><h1>Agil</h1></body>/html>

これは私が使用するコードです(関連部分)。

void expand(int * input, int inputSize) {    
    // int prevcode, currcode
    int previousCode; int currentCode;
    int nextCode = 256; // start with the same dictionary of 255 characters
    dictionaryInit();

    // prevcode = read in a code
    previousCode = input[0];

    int pointer = 1;

    // while (there is still data to read)
    while (pointer < inputSize) {
        // currcode = read in a code
        currentCode = input[pointer++];

        if (currentCode >= nextCode) printf("!"); // XXX not yet implemented!
        currentCode = decode(currentCode);

        // add a new code to the string table
        dictionaryAdd(previousCode, currentCode, nextCode++);

        // prevcode = currcode
        previousCode = currentCode;
    }
}

int decode(int code) {
    int character; int temp;

    if (code > 255) { // decode
        character = dictionaryCharacter(code);
        temp = decode(dictionaryPrefix(code)); // recursion
    } else {
        character = code; // ASCII
        temp = code;
    }
    appendCharacter(character); // save to output
    return temp;
}

見つけられますか?感謝いたします。

役に立ちましたか?

解決

デコード関数は文字列の最初の文字を返します。この文字を辞書に追加するには必要ですが、 ない セット previousCode それに。したがって、コードは次のようになります。

...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top