comparison HuffmanSet.cpp @ 49:f8d0ea827db3

Use BitArray.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Mon, 10 Sep 2012 23:59:46 +0200
parents f711ddb56ae7
children 19b2a2d98788
comparison
equal deleted inserted replaced
48:ef429402e03b 49:f8d0ea827db3
1 #include "HuffmanString.hpp" 1 #include "HuffmanString.hpp"
2
3 #include "FastBitDecoder.hpp"
4 #include "BitDecoder.hpp"
2 5
3 #include "Exception/InvalidDataException.hpp" 6 #include "Exception/InvalidDataException.hpp"
4 #include "Exception/NoSuchValueException.hpp" 7 #include "Exception/NoSuchValueException.hpp"
5 8
6 #include <QtCore/QHash> 9 #include <QtCore/QHash>
46 } 49 }
47 BitDecoder* retVal = freqs.values()[0]; 50 BitDecoder* retVal = freqs.values()[0];
48 return retVal; 51 return retVal;
49 } 52 }
50 53
51 QString HuffmanSet::decode(const QBitArray& bits) const 54 QString HuffmanSet::decode(const BitArray& bits) const
52 { 55 {
53 return lut->decode(bits); 56 return lut->decode(bits);
54 } 57 }
55 58
56 QBitArray HuffmanSet::encode(const QString& string, const QMap<QString, QBitArray>& encoder) 59 BitArray HuffmanSet::encode(const QString& string, const QMap<QString, BitArray>& encoder)
57 { 60 {
58 QBitArray retVal; 61 BitArray retVal;
59 QStringList c = chunks(string); 62 QStringList c = chunks(string);
60 foreach(const QString& fragment, c) { 63 foreach(const QString& fragment, c) {
61 if (encoder.contains(fragment)) 64 if (encoder.contains(fragment))
62 retVal = BitDecoder::unite(retVal, encoder[fragment]); 65 retVal = BitDecoder::unite(retVal, encoder[fragment]);
63 else 66 else
96 foreach(key_t key, newStrings.keys()) { 99 foreach(key_t key, newStrings.keys()) {
97 map.insert(key, encode(newStrings.value(key), encoder)); 100 map.insert(key, encode(newStrings.value(key), encoder));
98 } 101 }
99 numInserts = 0; 102 numInserts = 0;
100 delete lut; 103 delete lut;
104 /*
105 lut = new FastBitDecoder(encoder);
106 delete newLut;
107 */
101 lut = newLut; 108 lut = newLut;
102 newStrings.clear(); 109 newStrings.clear();
103 } 110 }
104 111
105 bool HuffmanSet::contains(key_t key) const 112 bool HuffmanSet::contains(key_t key) const
118 HuffmanSet::key_t HuffmanSet::insert(const QString& str) 125 HuffmanSet::key_t HuffmanSet::insert(const QString& str)
119 { 126 {
120 key_t key = hash(str); 127 key_t key = hash(str);
121 if (!contains(key)) { 128 if (!contains(key)) {
122 try { 129 try {
123 QBitArray bits = encode(str, encoder); 130 BitArray bits = encode(str, encoder);
124 map.insert(key, bits); 131 map.insert(key, bits);
125 } 132 }
126 catch (InvalidDataException& e) { 133 catch (InvalidDataException& e) {
127 newStrings.insert(key, str); 134 newStrings.insert(key, str);
128 } 135 }