ホームページ  >  記事  >  バックエンド開発  >  C# で LZW 圧縮アルゴリズムを実装する方法

C# で LZW 圧縮アルゴリズムを実装する方法

WBOY
WBOYオリジナル
2023-09-19 18:03:27752ブラウズ

C# で LZW 圧縮アルゴリズムを実装する方法

LZW 圧縮アルゴリズムを C で実装する方法

#はじめに:
データの継続的な増加に伴い、データの保存と送信が重要なタスクになっています。 LZW (Lempel-Ziv-Welch) 圧縮アルゴリズムは、データのサイズを効果的に削減できる一般的に使用される可逆圧縮アルゴリズムです。この記事では、C# で LZW 圧縮アルゴリズムを実装する方法と具体的なコード例を紹介します。

  1. LZW 圧縮アルゴリズムの原理
    LZW 圧縮アルゴリズムは辞書圧縮アルゴリズムであり、その基本原理は、入力データ ストリームに出現する連続文字列を独自のエンコードにマッピングすることです。圧縮時は文字列を徐々に辞書に追加して対応するエンコードを出力し、伸長時はエンコードにより辞書内の対応する文字列を見つけて出力します。アルゴリズムの核心は、入力データ ストリームと一致するように辞書を継続的に更新することです。
  2. LZW 圧縮アルゴリズムの実装手順
    (1) 辞書の初期化: 入力データ ストリーム内の各文字を独立したエンコーディングに初期化します。
    (2) 入力データ ストリームの最初の文字を現在の文字として読み取ります。
    (3) データ フローが終了するまで次の手順を繰り返します:
    a. 次の文字を読み取り、現在の文字と次の文字を新しい文字シーケンスに結合します。
    b. 文字シーケンスが辞書にすでに存在する場合は、現在の文字を新しい文字シーケンスに更新し、次の文字の読み取りを続けます。
    c. 文字シーケンスが辞書に存在しない場合は、現在の文字を出力し、新しい文字シーケンスを辞書に追加し、現在の文字を次の文字に更新します。
    (4) 現在の残りの文字を出力します。
  3. C# コード例
    次は、C# で LZW 圧縮アルゴリズムを実装するコード例です:
using System;
using System.Collections.Generic;
using System.Text;

class LZWCompression
{
    public static List<int> Compress(string data)
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        List<int> compressedData = new List<int>();

        int currentCode = 256;

        for (int i = 0; i < 256; i++)
        {
            dictionary.Add(((char)i).ToString(), i);
        }

        string currentString = "";

        foreach (char c in data)
        {
            string newString = currentString + c;

            if (dictionary.ContainsKey(newString))
            {
                currentString = newString;
            }
            else
            {
                compressedData.Add(dictionary[currentString]);
                dictionary.Add(newString, currentCode);
                currentCode++;
                currentString = c.ToString();
            }
        }

        if (currentString != "")
        {
            compressedData.Add(dictionary[currentString]);
        }

        return compressedData;
    }

    public static string Decompress(List<int> compressedData)
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        StringBuilder decompressedData = new StringBuilder();

        int currentCode = 256;

        for (int i = 0; i < 256; i++)
        {
            dictionary.Add(i, ((char)i).ToString());
        }

        int previousCode = compressedData[0].Value.ToString();

        decompressedData.Append(dictionary[previousCode]);

        for (int i = 1; i < compressedData.Count; i++)
        {
            int currentCode = compressedData[i];

            if (dictionary.ContainsKey(currentCode))
            {
                decompressedData.Append(dictionary[currentCode]);
                string newEntry = dictionary[previousCode] + dictionary[currentCode][0];
                dictionary.Add(currentCode, newEntry);
                previousCode = currentCode;
            }
            else
            {
                string newEntry = dictionary[previousCode] + dictionary[previousCode][0];
                decompressedData.Append(newEntry);
                dictionary.Add(currentCode, newEntry);
                previousCode = currentCode;
            }
        }

        return decompressedData.ToString();
    }
}

次は、LZW 圧縮アルゴリズムの使用例です。 :

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        string originalData = "AAAAABBBBCCCCCDDDDDEE";
        
        Console.WriteLine("原始数据: " + originalData);
        
        List<int> compressedData = LZWCompression.Compress(originalData);
        
        Console.WriteLine("压缩后的数据: " + string.Join(",", compressedData));

        string decompressedData = LZWCompression.Decompress(compressedData);

        Console.WriteLine("解压缩后的数据: " + decompressedData);

        Console.ReadLine();
    }
}

上記のコード例では、LZWCompression クラスを使用してデータを圧縮および解凍します。圧縮には Compress メソッドが使用され、解凍には 解凍メソッド。

結論:
この記事では、C# で LZW 圧縮アルゴリズムを実装する方法を紹介し、具体的なコード例を示します。 LZW 圧縮アルゴリズムは、一般的に使用されている効果的な可逆圧縮アルゴリズムで、データのサイズを削減し、データの保存と送信の効率を向上させるのに役立ちます。

以上がC# で LZW 圧縮アルゴリズムを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。