当前位置: 首页>>代码示例>>C++>>正文


C++ BitStream::insert方法代码示例

本文整理汇总了C++中BitStream::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ BitStream::insert方法的具体用法?C++ BitStream::insert怎么用?C++ BitStream::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BitStream的用法示例。


在下文中一共展示了BitStream::insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: getFrequency

std::vector<unsigned char> Compressor::compress(void* data, unsigned length)
{
    mFrequency = getFrequency(data, length);
    int bits = getBestRatio(length);
    char* ptr = (char*)data;
    int i;

    if (bits == -1)
        return std::vector<unsigned char>();

    BitStream bs;

    unsigned totalSize = 27 + ((1 << bits) - 1) * 8;

    if (mIsLittleEndian)
        bs.insert((int)length, 24);
    else
        bs.insert(reverseEndianess((int)length), 24);

    bs.insert(bits, 3);

    for (i = 0; i < (1 << bits) - 1; i++)
    {
        if ((int)mFrequency.size() > i)
            bs.insert(mFrequency[i].character);
        else
            bs.insert(0);
    }

    std::vector<char> uncompressed;

    for (i = 0; i < (int)length; i++)
    {
        int vecPos = findChar(mFrequency, *ptr, (1 << bits) - 1);
        if (vecPos == -1)
        {
            bs.insert(0U, bits);
            uncompressed.push_back(*ptr);
        }
        else
        {
            bs.insert(vecPos + 1, bits);
        }

        totalSize += bits;
        ptr++;
    }

    for (i = 0; i < (int)uncompressed.size(); i++)
    {
        bs.insert(uncompressed[i]);
        totalSize += 8;
    }

    return bs.getData();
}
开发者ID:bosorioo,项目名称:Data-Compression,代码行数:56,代码来源:Compressor.cpp

示例2: string

std::vector<unsigned char> Compressor::decompress(void* data, unsigned length)
{
    BitStream bs;
    bs.insert(data, length);
    bs.read_bytes(&length, 3);

    if (!mIsLittleEndian)
        length = (unsigned)reverseEndianess((int)length);

    unsigned char bits;
    bs.read_bits(bits, 3);

    if (bits < 1 || bits > 7)
    {
        char bStr[4];
        throw (std::string("Bit-size of enconding isn't valid: ") + std::string(itoa(bits, bStr, 10)));
    }

    unsigned char byte;
    int bitsRead = 27;
    int i;
    std::vector<char> charTable;
    std::vector<char> compressedChars;

    for (i = 1; i <= (1 << bits) - 1; i++)
    {
        bs.read_bits(byte, 8);
        charTable.push_back(byte);
        bitsRead += 8;

        if (!bs.canRead())
            throw std::string ("Bad file for decompression [1]");
    }

    for (unsigned j = 1; j <= length; j++)
    {
        bs.read_bits(byte, bits);
        bitsRead += bits;
        compressedChars.push_back(byte);

        if (!bs.canRead())
            throw std::string ("Bad file for decompression [2]");
    }

    std::vector<unsigned char> result(length, 0);

    for (i = 0; i < (int)compressedChars.size(); i++)
    {
        if (compressedChars[i] == 0 || (unsigned char)compressedChars[i] > charTable.size())
        {
            if (!bs.canRead())
                throw std::string ("Bad file for decompression [3]");

            bs.read_bits(result[i], 8);
            bitsRead += 8;
        }
        else
        {
            result[i] = charTable[compressedChars[i] - 1];
        }
    }

    return result;
}
开发者ID:bosorioo,项目名称:Data-Compression,代码行数:64,代码来源:Compressor.cpp


注:本文中的BitStream::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。