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(clearActionAll);
#if 0
// TESTING :P
qint64 a = QDateTime::currentSecsSinceEpoch();
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))));
auto input = "HELLO BRAVE NEW WORLD!";
auto encoded = Varicode::huffEncode(input);
auto decoded = Varicode::huffDecode(encoded);
qDebug() << input << Varicode::bitsToStr(encoded) << decoded;
auto input = "HELLO BRAVE NEW WORLD!\x04";
auto encoded = Varicode::huffEncode(input) + QList<QVector<bool>> {Varicode::strToBits("000000000")};
auto decoded = Varicode::huffDecode(Varicode::huffFlatten(encoded));
qDebug() << input << Varicode::bitsToStr(Varicode::huffFlatten(encoded)) << decoded;
#endif
// this must be the last statement of constructor
if (!m_valid) throw std::runtime_error {"Fatal initialization exception"};

View File

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

View File

@ -20,7 +20,8 @@ public:
static QStringList parseCallsigns(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 QVector<bool> strToBits(QString const& bitvec);