Working through huffman interface

This commit is contained in:
Jordan Sherer 2018-07-12 20:31:45 -04:00
parent 707f577f31
commit 808782b965
3 changed files with 24 additions and 9 deletions

View File

@ -1050,15 +1050,16 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple,
ui->tableWidgetCalls->addAction(clearActionSep); ui->tableWidgetCalls->addAction(clearActionSep);
ui->tableWidgetCalls->addAction(clearActionAll); ui->tableWidgetCalls->addAction(clearActionAll);
#if 0
// TESTING :P // TESTING :P
qint64 a = QDateTime::currentSecsSinceEpoch(); qint64 a = QDateTime::currentSecsSinceEpoch();
qDebug() << a << Varicode::pack64bits(a) << Varicode::unpack64bits(Varicode::pack64bits(a)); qDebug() << a << Varicode::pack64bits(a) << Varicode::unpack64bits(Varicode::pack64bits(a));
qDebug() << a << Varicode::bitsToStr(Varicode::intToBits(a)) << Varicode::bitsToInt(Varicode::strToBits(Varicode::bitsToStr(Varicode::intToBits(a)))); qDebug() << a << Varicode::bitsToStr(Varicode::intToBits(a)) << Varicode::bitsToInt(Varicode::strToBits(Varicode::bitsToStr(Varicode::intToBits(a))));
auto input = "HELLO BRAVE NEW WORLD!\x04";
auto input = "HELLO BRAVE NEW WORLD!"; auto encoded = Varicode::huffEncode(input) + QList<QVector<bool>> {Varicode::strToBits("000000000")};
auto encoded = Varicode::huffEncode(input); auto decoded = Varicode::huffDecode(Varicode::huffFlatten(encoded));
auto decoded = Varicode::huffDecode(encoded); qDebug() << input << Varicode::bitsToStr(Varicode::huffFlatten(encoded)) << decoded;
qDebug() << input << Varicode::bitsToStr(encoded) << decoded; #endif
// this must be the last statement of constructor // this must be the last statement of constructor
if (!m_valid) throw std::runtime_error {"Fatal initialization exception"}; if (!m_valid) throw std::runtime_error {"Fatal initialization exception"};

View File

@ -62,6 +62,8 @@ QMap<QChar, QString> huff = {
{'\x04' , "110101100010" }, // 1 <- eot {'\x04' , "110101100010" }, // 1 <- eot
}; };
QChar huffeot = '\x04';
QStringList Varicode::parseCallsigns(QString const &input){ QStringList Varicode::parseCallsigns(QString const &input){
QStringList callsigns; QStringList callsigns;
QRegularExpression re(callsign_pattern2); QRegularExpression re(callsign_pattern2);
@ -99,19 +101,27 @@ QStringList Varicode::parseGrids(const QString &input){
return grids; return grids;
} }
QVector<bool> Varicode::huffEncode(QString const& text){ QList<QVector<bool>> Varicode::huffEncode(QString const& text){
QVector<bool> out; QList<QVector<bool>> out;
foreach(auto ch, text){ foreach(auto ch, text){
if(!huff.contains(ch)){ if(!huff.contains(ch)){
continue; continue;
} }
out += strToBits(huff[ch]); out.append(strToBits(huff[ch]));
} }
return out; return out;
} }
QVector<bool> Varicode::huffFlatten(QList<QVector<bool>> &list){
QVector<bool> out;
foreach(auto vec, list){
out += vec;
}
return out;
}
QString Varicode::huffDecode(QVector<bool> const& bitvec){ QString Varicode::huffDecode(QVector<bool> const& bitvec){
QString out; QString out;
@ -122,6 +132,9 @@ QString Varicode::huffDecode(QVector<bool> const& bitvec){
bool found = false; bool found = false;
foreach(auto key, huff.keys()){ foreach(auto key, huff.keys()){
if(bits.startsWith(huff[key])){ if(bits.startsWith(huff[key])){
if(key == huffeot){
break;
}
out.append(key); out.append(key);
bits = bits.mid(huff[key].length()); bits = bits.mid(huff[key].length());
found = true; found = true;

View File

@ -20,7 +20,8 @@ public:
static QStringList parseCallsigns(QString const &input); static QStringList parseCallsigns(QString const &input);
static QStringList parseGrids(QString const &input); static QStringList parseGrids(QString const &input);
static QVector<bool> huffEncode(QString const& text); static QList<QVector<bool>> huffEncode(QString const& text);
static QVector<bool> huffFlatten(QList<QVector<bool>> &list);
static QString huffDecode(QVector<bool> const& bitvec); static QString huffDecode(QVector<bool> const& bitvec);
static QVector<bool> strToBits(QString const& bitvec); static QVector<bool> strToBits(QString const& bitvec);