Mercurial > dedupe
changeset 45:41cc0d8ac77f
Decode directly from bitString.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Mon, 10 Sep 2012 21:29:43 +0200 |
| parents | 7348d4efa4f6 |
| children | 877327e9061a |
| files | BitDecoder.cpp BitDecoder.hpp TestBitDecoder.cpp |
| diffstat | 3 files changed, 46 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/BitDecoder.cpp Mon Sep 10 19:18:30 2012 +0200 +++ b/BitDecoder.cpp Mon Sep 10 21:29:43 2012 +0200 @@ -1,5 +1,7 @@ #include "BitDecoder.hpp" +#include "Exception/InvalidDataException.hpp" + BitDecoder::BitDecoder(BitDecoder* low_in, BitDecoder* high_in) : low(low_in), high(high_in) { } @@ -61,3 +63,22 @@ } return retVal; } + +QBitArray BitDecoder::bitsFromString(const QString& str) +{ + QBitArray bits(str.size()); + for (int i = 0; i < str.size(); ++i) { + if (str[i] == '1') + bits[i] = true; + else if (str[i] == '0') + bits[i] = false; + else + throw InvalidDataException(); + } + return bits; +} + +QString BitDecoder::decode(const QString& bits) const +{ + return decode(bitsFromString(bits)); +}
--- a/BitDecoder.hpp Mon Sep 10 19:18:30 2012 +0200 +++ b/BitDecoder.hpp Mon Sep 10 21:29:43 2012 +0200 @@ -13,7 +13,7 @@ private: BitDecoder(BitDecoder* low_in, BitDecoder* high_in); - uint decode(QString& resString, const QBitArray& bits, uint offset) + uint decode(QString& resString, const QBitArray& bits, uint offset) const { //Data is always stored in a leaf node. if (high) { @@ -30,7 +30,7 @@ ~BitDecoder(); const QString& data() const; - QString decode(const QBitArray& bits) + QString decode(const QBitArray& bits) const { QString combined; uint n = bits.size(); @@ -40,8 +40,11 @@ combined.squeeze(); return combined; } + QString decode(const QString& bits) const; static BitDecoder* merge(BitDecoder* low, BitDecoder* high); + static QBitArray bitsFromString(const QString& str); + static QBitArray unite(const QBitArray& first, const QBitArray& second) { int n = first.size();
--- a/TestBitDecoder.cpp Mon Sep 10 19:18:30 2012 +0200 +++ b/TestBitDecoder.cpp Mon Sep 10 21:29:43 2012 +0200 @@ -1,23 +1,8 @@ #include "BitDecoder.hpp" #include "TestFramework.hpp" -#include "Exception/InvalidDataException.hpp" -QBitArray bitsFromString(const QString& str) -{ - QBitArray bits(str.size()); - for (int i = 0; i < str.size(); ++i) { - if (str[i] == '1') - bits[i] = true; - else if (str[i] == '0') - bits[i] = false; - else - throw InvalidDataException(); - } - return bits; -} - BOOST_AUTO_TEST_CASE( TestSimple ) { BitDecoder* up = new BitDecoder("a"); @@ -29,12 +14,12 @@ BOOST_REQUIRE_EQUAL(up->data(), "a"); BOOST_REQUIRE_EQUAL(down->data(), "b"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1")), "a"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0")), "b"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1111")), "aaaa"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000")), "bbbb"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1101")), "aaba"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1111")), "aaaa"); + BOOST_REQUIRE_EQUAL(full->decode("1"), "a"); + BOOST_REQUIRE_EQUAL(full->decode("0"), "b"); + BOOST_REQUIRE_EQUAL(full->decode("1111"), "aaaa"); + BOOST_REQUIRE_EQUAL(full->decode("0000"), "bbbb"); + BOOST_REQUIRE_EQUAL(full->decode("1101"), "aaba"); + BOOST_REQUIRE_EQUAL(full->decode("1111"), "aaaa"); } BOOST_AUTO_TEST_CASE( TestLong ) @@ -49,19 +34,19 @@ BitDecoder* full = BitDecoder::merge(down, up); BOOST_REQUIRE(full->data().isNull()); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("1")), "n"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("01")), "m"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("001")), "l"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0001")), "k"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00001")), "j"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000001")), "i"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000001")), "h"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00000001")), "g"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000000001")), "f"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000001")), "e"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("00000000001")), "d"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("000000000001")), "c"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000000001")), "b"); - BOOST_REQUIRE_EQUAL(full->decode(bitsFromString("0000000000000")), "a"); + BOOST_REQUIRE_EQUAL(full->decode("1"), "n"); + BOOST_REQUIRE_EQUAL(full->decode("01"), "m"); + BOOST_REQUIRE_EQUAL(full->decode("001"), "l"); + BOOST_REQUIRE_EQUAL(full->decode("0001"), "k"); + BOOST_REQUIRE_EQUAL(full->decode("00001"), "j"); + BOOST_REQUIRE_EQUAL(full->decode("000001"), "i"); + BOOST_REQUIRE_EQUAL(full->decode("0000001"), "h"); + BOOST_REQUIRE_EQUAL(full->decode("00000001"), "g"); + BOOST_REQUIRE_EQUAL(full->decode("000000001"), "f"); + BOOST_REQUIRE_EQUAL(full->decode("0000000001"), "e"); + BOOST_REQUIRE_EQUAL(full->decode("00000000001"), "d"); + BOOST_REQUIRE_EQUAL(full->decode("000000000001"), "c"); + BOOST_REQUIRE_EQUAL(full->decode("0000000000001"), "b"); + BOOST_REQUIRE_EQUAL(full->decode("0000000000000"), "a"); }
