Mercurial > dedupe
view FastBitDecoder.cpp @ 56:76846cb92b5c
Clean up at exit.
Add getSet function.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 13 Sep 2012 23:49:18 +0200 |
| parents | f8d0ea827db3 |
| children | c8111de2e0bb |
line wrap: on
line source
#include "FastBitDecoder.hpp" #include "BitDecoder.hpp" #include <QtCore/QDebug> #include <cassert> unsigned char FastBitDecoder::getPaddedChar(const BitArray& bits, uint offset) { unsigned char retVal = 0; size_t n = std::min(8u, bits.size() - offset); for (size_t i = 0; i < n; ++i) { retVal |= (bits.testBit(i + offset) ? 1 : 0) << (7 - i) ; } return retVal; } BitArray FastBitDecoder::removeFirst(const BitArray& bits) { size_t N = bits.size(); assert(N - 8 > 0); size_t n = N - 8; BitArray retVal(n); for (size_t i = 0; i < n; ++i) { retVal.setBit(i, bits.testBit(i + 8)); } return retVal; } void FastBitDecoder::fill() { for (size_t i = 0; i < N; ++i) { if (decoder[i] != 0) { decoder[i]->fill(); } else if (data[i] == 0) { assert(data[i - 1]); data[i] = data[i - 1]; numBits[i] = numBits[i - 1]; } } } void FastBitDecoder::blank() { for (size_t i = 0; i < N; ++i) { decoder[i] = 0; data[i] = 0; numBits[i] = 0; } } FastBitDecoder::FastBitDecoder() { blank(); } FastBitDecoder::FastBitDecoder(const QMap<QString, BitArray>& encoder) { blank(); for(QMap<QString, BitArray>::const_iterator it = encoder.begin(); it != encoder.end(); ++it) { insert(it.value(), it.key()); } fill(); } void FastBitDecoder::insert(const BitArray& key, const QString& value) { unsigned char l = getPaddedChar(key); if (key.size() <= 8) { data[l] = new QString(value); numBits[l] = key.size(); } else { if (!decoder[l]) decoder[l] = new FastBitDecoder(); decoder[l]->insert(removeFirst(key), value); numBits[l] = 8; } } uint FastBitDecoder::decode(QString& resString, const BitArray& bits, uint offset) const { unsigned char l = getPaddedChar(bits, offset); if (data[l]) { resString.append(*data[l]); return numBits[l]; } else { return decoder[l]->decode(resString, bits, offset + 8) + 8; } } QString FastBitDecoder::decode(const QString& bits) const { return decode(BitDecoder::bitsFromString(bits)); }
