Add Tuple struct with precomputed inverse index

This commit is contained in:
Jordan Sherer 2018-10-02 18:03:15 -04:00
parent 2cb1665e2e
commit 7f992433dc
7 changed files with 229498 additions and 467328 deletions

25
jsc.cpp
View File

@ -64,21 +64,14 @@ QList<CodewordPair> JSC::compress(QString text){
bool hasPrefix = false;
auto d = w.toLatin1().data();
for(quint32 i = 0; i < JSC::size; i++){
// TODO: we could probably precompute these sizes
quint32 len = strlen(JSC::list[i]);
if(strncmp(d, JSC::list[i], len) == 0){
quint32 len = JSC::list[i].size;
if(strncmp(d, JSC::list[i].str, len) == 0){
w = QString(w.mid(len));
auto word = JSC::list[i];
auto index = lookup(word, &ok);
if(ok){
bool isLast = w.isEmpty();
out.append({ codeword(index, isLast, b, s, c), len + (isLast ? 1 : 0) /* for the space that follows */});
hasPrefix = true;
break;
}
auto index = JSC::list[i].index;
bool isLast = w.isEmpty();
out.append({ codeword(index, isLast, b, s, c), len + (isLast ? 1 : 0) /* for the space that follows */});
hasPrefix = true;
break;
}
@ -140,8 +133,8 @@ QString JSC::decompress(Codeword const& bitvec){
j = j*s + bytes[start + k] + base[k];
out.append(QString(JSC::map[j])); // table.first.key(j));
if(separators.first() == start + k){
out.append(QString(JSC::map[j].str));
if(!separators.isEmpty() && separators.first() == start + k){
out.append(" ");
separators.removeFirst();
}
@ -158,7 +151,7 @@ quint32 JSC::lookup(QString w, bool * ok){
quint32 JSC::lookup(char const* b, bool *ok){
for(quint32 i = 0; i < JSC::size; i++){
if(strcmp(b, JSC::map[i]) == 0){
if(strcmp(b, JSC::map[i].str) == 0){
if(ok) *ok = true;
return i;
}

11
jsc.h
View File

@ -15,6 +15,11 @@
typedef QPair<QVector<bool>, quint32> CodewordPair; // Tuple(Codeword, N) where N = number of characters
typedef QVector<bool> Codeword; // Codeword bit vector
typedef struct Tuple{
char const * str;
int size;
int index;
} Tuple;
class JSC
{
@ -30,9 +35,9 @@ public:
static quint32 lookup(QString w, bool *ok);
static quint32 lookup(char const* b, bool *ok);
static const quint32 size = 233638;
static char const* map[];
static char const* list[];
static const quint32 size = 114712;
static const Tuple map[114712];
static const Tuple list[114712];
};
#endif // JSC_H

348343
jsc_list.cpp

File diff suppressed because it is too large Load Diff

348354
jsc_map.cpp

File diff suppressed because it is too large Load Diff

View File

@ -6136,7 +6136,7 @@ QStringList MainWindow::buildMessageFrames(const QString &text){
auto frames = Varicode::buildMessageFrames(mycall, basecall, mygrid, compound, selectedCall, text);
#if 1
#if 0
qDebug() << "frames:";
foreach(auto frame, frames){
auto dt = DecodedText(frame);
@ -8740,8 +8740,18 @@ void MainWindow::buildMessageFramesAndUpdateCountDisplay(){
);
connect(t, &BuildMessageFramesThread::finished, t, &QObject::deleteLater);
connect(t, &BuildMessageFramesThread::resultReady, this, [this, text](const QStringList frames){
updateFrameCountDisplay(text, frames.length());
connect(t, &BuildMessageFramesThread::resultReady, this, [this](const QStringList frames){
QStringList text;
qDebug() << "frames:";
foreach(auto frame, frames){
auto dt = DecodedText(frame);
qDebug() << "->" << frame << dt.message() << Varicode::frameTypeString(dt.frameType());
text.append(dt.message());
}
updateFrameCountDisplay(text.join(""), frames.length());
});
t->start();
#endif

View File

@ -882,7 +882,6 @@ private:
bool isMyCallIncluded(QString const &text);
bool isAllCallIncluded(QString const &text);
QString callsignSelected();
void clearCallsignSelected();
bool isRecentOffset(int offset);
void markOffsetRecent(int offset);
bool isDirectedOffset(int offset, bool *pIsAllCall);

View File

@ -1550,42 +1550,8 @@ QString Varicode::packDataMessage(const QString &input, int *n){
static const int frameSize = 72;
QString frame;
QVector<bool> frameBits;
frameBits.append(Varicode::intToBits(FrameDataPadded, 3));
int i = 0;
foreach(auto pair, JSC::compress(input)){
auto bits = pair.first;
auto chars = pair.second;
if(frameBits.length() + bits.length() < frameSize){
frameBits.append(bits);
i += chars;
continue;
}
break;
}
int pad = frameSize - frameBits.length();
if(pad){
// the way we will pad is this...
// set the bit after the frame to 0 and every bit after that a 1
// to unpad, seek from the end of the bits until you hit a zero... the rest is the actual frame.
for(int i = 0; i < pad; i++){
frameBits.append(i == 0 ? (bool)0 : (bool)1);
}
}
quint64 value = Varicode::bitsToInt(frameBits.constBegin(), 64);
quint8 rem = (quint8)Varicode::bitsToInt(frameBits.constBegin() + 64, 8);
frame = Varicode::pack72bits(value, rem);
*n = i;
#if 0
#if USE_HUFF_DATA_PACKING
// [3][69] = 72
QVector<bool> frameDataBits;
@ -1625,6 +1591,40 @@ QString Varicode::packDataMessage(const QString &input, int *n){
quint8 rem = (quint8)Varicode::bitsToInt(allBits.constBegin() + 64, 8);
frame = Varicode::pack72bits(value, rem);
*n = i;
#else
QVector<bool> frameBits;
frameBits.append(Varicode::intToBits(FrameDataPadded, 3));
int i = 0;
foreach(auto pair, JSC::compress(input)){
auto bits = pair.first;
auto chars = pair.second;
if(frameBits.length() + bits.length() < frameSize){
frameBits.append(bits);
i += chars;
continue;
}
break;
}
int pad = frameSize - frameBits.length();
if(pad){
// the way we will pad is this...
// set the bit after the frame to 0 and every bit after that a 1
// to unpad, seek from the end of the bits until you hit a zero... the rest is the actual frame.
for(int i = 0; i < pad; i++){
frameBits.append(i == 0 ? (bool)0 : (bool)1);
}
}
quint64 value = Varicode::bitsToInt(frameBits.constBegin(), 64);
quint8 rem = (quint8)Varicode::bitsToInt(frameBits.constBegin() + 64, 8);
frame = Varicode::pack72bits(value, rem);
*n = i;
#endif
@ -1642,7 +1642,6 @@ QString Varicode::unpackDataMessage(const QString &text, quint8 *pType){
quint64 value = Varicode::unpack72bits(text, &rem);
auto bits = Varicode::intToBits(value, 64) + Varicode::intToBits(rem, 8);
#if 0
quint8 type = Varicode::bitsToInt(bits.mid(0, 3));
if(type == FrameDataUnpadded){
bits = bits.mid(3);
@ -1653,12 +1652,17 @@ QString Varicode::unpackDataMessage(const QString &text, quint8 *pType){
return unpacked;
}
#if USE_HUFF_DATA_PACKING
// huff decode the bits (without escapes)
unpacked = Varicode::huffDecode(hufftable, bits);
// then... unescape special characters
unpacked = Varicode::huffUnescape(unpacked);
if(pType) *pType = type;
#else
unpacked = JSC::decompress(bits);
if(pType) *pType = type;
#endif