Mercurial > dedupe
comparison FastBitDecoder.cpp @ 49:f8d0ea827db3
Use BitArray.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Mon, 10 Sep 2012 23:59:46 +0200 |
| parents | 877327e9061a |
| children | c8111de2e0bb |
comparison
equal
deleted
inserted
replaced
| 48:ef429402e03b | 49:f8d0ea827db3 |
|---|---|
| 4 | 4 |
| 5 #include <QtCore/QDebug> | 5 #include <QtCore/QDebug> |
| 6 | 6 |
| 7 #include <cassert> | 7 #include <cassert> |
| 8 | 8 |
| 9 unsigned char FastBitDecoder::getPaddedChar(const QBitArray& bits, uint offset) | 9 unsigned char FastBitDecoder::getPaddedChar(const BitArray& bits, uint offset) |
| 10 { | 10 { |
| 11 unsigned char retVal = 0; | 11 unsigned char retVal = 0; |
| 12 size_t n = std::min(8u, bits.size() - offset); | 12 size_t n = std::min(8u, bits.size() - offset); |
| 13 for (size_t i = 0; i < n; ++i) { | 13 for (size_t i = 0; i < n; ++i) { |
| 14 retVal |= (bits.testBit(i + offset) ? 1 : 0) << (7 - i) ; | 14 retVal |= (bits.testBit(i + offset) ? 1 : 0) << (7 - i) ; |
| 15 } | 15 } |
| 16 return retVal; | 16 return retVal; |
| 17 } | 17 } |
| 18 | 18 |
| 19 QBitArray FastBitDecoder::removeFirst(const QBitArray& bits) | 19 BitArray FastBitDecoder::removeFirst(const BitArray& bits) |
| 20 { | 20 { |
| 21 size_t N = bits.size(); | 21 size_t N = bits.size(); |
| 22 assert(N - 8 > 0); | 22 assert(N - 8 > 0); |
| 23 size_t n = N - 8; | 23 size_t n = N - 8; |
| 24 QBitArray retVal(n); | 24 BitArray retVal(n); |
| 25 for (size_t i = 0; i < n; ++i) { | 25 for (size_t i = 0; i < n; ++i) { |
| 26 retVal.setBit(i, bits.at(i + 8)); | 26 retVal.setBit(i, bits.testBit(i + 8)); |
| 27 } | 27 } |
| 28 return retVal; | 28 return retVal; |
| 29 } | 29 } |
| 30 | 30 |
| 31 | 31 |
| 56 { | 56 { |
| 57 blank(); | 57 blank(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 | 60 |
| 61 FastBitDecoder::FastBitDecoder(const QMap<QString, QBitArray>& encoder) | 61 FastBitDecoder::FastBitDecoder(const QMap<QString, BitArray>& encoder) |
| 62 { | 62 { |
| 63 blank(); | 63 blank(); |
| 64 for(QMap<QString, QBitArray>::const_iterator it = encoder.begin(); | 64 for(QMap<QString, BitArray>::const_iterator it = encoder.begin(); |
| 65 it != encoder.end(); ++it) { | 65 it != encoder.end(); ++it) { |
| 66 insert(it.value(), it.key()); | 66 insert(it.value(), it.key()); |
| 67 } | 67 } |
| 68 fill(); | 68 fill(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 | 71 |
| 72 void FastBitDecoder::insert(const QBitArray& key, const QString& value) | 72 void FastBitDecoder::insert(const BitArray& key, const QString& value) |
| 73 { | 73 { |
| 74 unsigned char l = getPaddedChar(key); | 74 unsigned char l = getPaddedChar(key); |
| 75 if (key.size() <= 8) { | 75 if (key.size() <= 8) { |
| 76 data[l] = new QString(value); | 76 data[l] = new QString(value); |
| 77 numBits[l] = key.size(); | 77 numBits[l] = key.size(); |
| 83 numBits[l] = 8; | 83 numBits[l] = 8; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 | 87 |
| 88 uint FastBitDecoder::decode(QString& resString, const QBitArray& bits, uint offset) const | 88 uint FastBitDecoder::decode(QString& resString, const BitArray& bits, uint offset) const |
| 89 { | 89 { |
| 90 unsigned char l = getPaddedChar(bits, offset); | 90 unsigned char l = getPaddedChar(bits, offset); |
| 91 if (data[l]) { | 91 if (data[l]) { |
| 92 resString.append(*data[l]); | 92 resString.append(*data[l]); |
| 93 return numBits[l]; | 93 return numBits[l]; |
