Mercurial > dedupe
comparison FastBitDecoder.cpp @ 57:c8111de2e0bb
Add functionality to decode directly from bitstring.
Use BitArrays getPaddedChar directly.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 13 Sep 2012 23:53:35 +0200 |
| parents | f8d0ea827db3 |
| children | 247adcbbaf8b |
comparison
equal
deleted
inserted
replaced
| 56:76846cb92b5c | 57:c8111de2e0bb |
|---|---|
| 1 #include "FastBitDecoder.hpp" | 1 #include "FastBitDecoder.hpp" |
| 2 | 2 |
| 3 #include "BitDecoder.hpp" | 3 #include "BitDecoder.hpp" |
| 4 | |
| 5 #include <QtCore/QDebug> | |
| 6 | 4 |
| 7 #include <cassert> | 5 #include <cassert> |
| 8 | 6 |
| 9 unsigned char FastBitDecoder::getPaddedChar(const BitArray& bits, uint offset) | 7 unsigned char FastBitDecoder::getPaddedChar(const BitArray& bits, uint offset) |
| 10 { | 8 { |
| 26 retVal.setBit(i, bits.testBit(i + 8)); | 24 retVal.setBit(i, bits.testBit(i + 8)); |
| 27 } | 25 } |
| 28 return retVal; | 26 return retVal; |
| 29 } | 27 } |
| 30 | 28 |
| 29 | |
| 30 FastBitDecoder::~FastBitDecoder() | |
| 31 { | |
| 32 for (size_t i = 0; i < N; ++i) { | |
| 33 delete decoder[i]; | |
| 34 if (i == 0 || (data[i] != data[i - 1])) | |
| 35 delete data[i]; | |
| 36 } | |
| 37 } | |
| 31 | 38 |
| 32 void FastBitDecoder::fill() | 39 void FastBitDecoder::fill() |
| 33 { | 40 { |
| 34 for (size_t i = 0; i < N; ++i) { | 41 for (size_t i = 0; i < N; ++i) { |
| 35 if (decoder[i] != 0) { | 42 if (decoder[i] != 0) { |
| 69 } | 76 } |
| 70 | 77 |
| 71 | 78 |
| 72 void FastBitDecoder::insert(const BitArray& key, const QString& value) | 79 void FastBitDecoder::insert(const BitArray& key, const QString& value) |
| 73 { | 80 { |
| 74 unsigned char l = getPaddedChar(key); | 81 unsigned char l = key.getPaddedChar(); |
| 75 if (key.size() <= 8) { | 82 if (key.size() <= 8) { |
| 76 data[l] = new QString(value); | 83 data[l] = new QString(value); |
| 77 numBits[l] = key.size(); | 84 numBits[l] = key.size(); |
| 78 } | 85 } |
| 79 else { | 86 else { |
| 80 if (!decoder[l]) | 87 if (!decoder[l]) |
| 81 decoder[l] = new FastBitDecoder(); | 88 decoder[l] = new FastBitDecoder(); |
| 82 decoder[l]->insert(removeFirst(key), value); | 89 decoder[l]->insert(removeFirst(key), value); |
| 83 numBits[l] = 8; | 90 numBits[l] = 8; |
| 84 } | 91 } |
| 85 } | 92 } |
| 86 | 93 |
| 87 | 94 |
| 88 uint FastBitDecoder::decode(QString& resString, const BitArray& bits, uint offset) const | 95 uint FastBitDecoder::decode(QString& resString, |
| 96 const BitArray& bits, | |
| 97 uint offset) const | |
| 89 { | 98 { |
| 90 unsigned char l = getPaddedChar(bits, offset); | 99 unsigned char l = bits.getPaddedChar(offset); |
| 91 if (data[l]) { | 100 if (data[l]) { |
| 92 resString.append(*data[l]); | 101 resString.append(*data[l]); |
| 93 return numBits[l]; | 102 return numBits[l]; |
| 94 } | 103 } |
| 95 else { | 104 else { |
