Exemplo n.º 1
0
bool CompressLZW::CompressImpl(const ByteBuffer& input, ByteBuffer& output)
{
    dict<std::string, int>::type_dense_hash_map dictionary;
    dict<std::string, int>::init(dictionary, "");

    int dictSize = DEFAULT_DICTIONARY_SIZE;
    for (int i = 0; i < DEFAULT_DICTIONARY_SIZE; i++)
        dictionary[std::string(1, i)] = i;

    const char* in = (const char*)input.Data();

    char c;
    std::string w, wc;
    dict<std::string, int>::type_dense_hash_map::iterator itr;
    for (size_t i = 0; i < input.Size(); ++i)
    {
        c = input[i];
        wc = w + c;

        itr = dictionary.find(wc);

        if (itr != dictionary.end())
            w = wc;
        else
        {
            output.WriteDynInt(dictionary[w]);
            dictionary.insert(itr, std::make_pair(wc, dictSize++));
            w = std::string(1, c);
        }
    }

    if (!w.empty())
        output.WriteDynInt(dictionary[w]);

    return true;
}
Exemplo n.º 2
0
void ByteBuffer::Append(const ByteBuffer& from) {
    Write(from.Data(), from.Size());
}