LZW压缩&解压缩C++简洁实现

#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <set>
using namespace std;
 
vector<size_t> compress(string& sin)
{
    istringstream in(sin);
    
    // initialize the table, add all visible ascii char into the dictionary
    map<string, int> dic;
    for(char c=32;c<=126;c++)
        dic[string(1,c)]=dic.size();
 
    vector<size_t> ret;
 
    // do the encoding
    string w;
    for(char c=in.get(); c!=EOF; c=in.get())
    {
        string wc=w+c;
 
        if(dic.find(wc)!=dic.end())
            w=wc;
        else
        {
            dic[wc]=dic.size();
            ret.push_back(dic[w]);
            w=c;
        }
    }
    ret.push_back(dic[w]);
    return ret;
}
 
string decompress(vector<size_t>& data)
{
    // the dictionary, a aditional set is used to test whether a word is already in the dictionary
    vector<string> dic;
    set<string> dic_set;
 
    // initialize the dictionary
    for(char c=32; c<=126;c++)
    {
        dic.push_back(string(1, c));
        dic_set.insert(string(1,c));
    }
 
    // decode
    string w, ret;
    for(size_t i=0;i<data.size();i++)
    {
        size_t code=data[i];
 
        // please refer to: http://www.cis.udel.edu/~amer/CISC651/lzw.and.gif.explained.html
        string en = code>=dic.size() ? w+w[0] : dic[code];
 
        ret+=en;
 
        string nw=w+en[0];
        if(dic_set.find(nw)==dic_set.end())
        {
            dic.push_back(nw);
            dic_set.insert(nw);
        }
        w=en;
    }
    return ret;
}
 
int main()
{
    //string s("ToBeOrNotToBe");
    string s("aaaaaffffffffffffffffffffffffffffffaaaafafafafaaaa");
    vector<size_t> ret=compress(s);
    string s2=decompress(ret);
}

你可能感兴趣的:(C++)