// displays prepare exam window void WMain::prepareexam() { if(dictionarySize() == 0) statusBar()->showMessage(tr("Dictionary is empty"), 10000); else if(dictionarySize() == cDocument->passed()) QMessageBox::information(this, tr("Congratulations"), tr("All words learned!")); else { WPrepare *wPrepare=new WPrepare(0, true); wPrepare->show(); } }
// to be called after _values is populated with a sorted list of all unique element values int32_t DictionaryEncoding::generateDictionary(const size_t elementSize) { // cache size in bytes, take no greater than 3/4 of it to prevent thrashing size_t maxCache = Sysinfo::getCPUCacheSize(Sysinfo::CPU_CACHE_L2 | Sysinfo::CPU_CACHE_L3) * 3 / 4; uint32_t entryLengthBits = ceil(log2(_values.size())); entryLengthBits = (entryLengthBits==0) ? 1 : entryLengthBits; uint32_t entriesPerCode = floor(8 / entryLengthBits); uint32_t minWasted = 8 - entriesPerCode * entryLengthBits; uint32_t wasted; _encodeDictionary.clear(); _decodeDictionary.clear(); if(dictionarySize(_values.size(), elementSize, 1) > maxCache) { // it won't fit in L2 cache return -1; } // prime the loop with one _codeLength = 1; // find maximum bytes per code // solve for the smallest number of wasted bits in our n byte codes for(uint32_t i = 2; i <= 4; ++i) { if(dictionarySize(_values.size(), elementSize, i) <= maxCache) { entriesPerCode = floor(i * 8 / entryLengthBits); wasted = i * 8 - entriesPerCode * entryLengthBits; if(wasted <= minWasted) // less hashmap lookups == better { _codeLength = i; minWasted = wasted; } } else { break; } } // now build all possible combos for _codeLength codes - recurse _entriesPerCode = floor(_codeLength * 8 / entryLengthBits); // recursively generate byte-aligned dictionary codes std::string blank = ""; // prime the value generateAllCodes(_entriesPerCode, 0, blank, entryLengthBits, elementSize); return 1; }
// updates statusbar void WMain::updateStatusbar() { if(this->mode == normalMode) { if(dictionarySize() > 0) { progressBar->setMaximum(dictionarySize()); progressBar->setValue(cDocument->passed()); } else { progressBar->setMaximum(1); progressBar->setValue(0); } } else if(this->mode == testMode || this->mode == examMode) { progressBar->setMaximum(this->howmany); progressBar->setValue(howmany-countdown); } }
// starts test void WMain::test(unsigned howmany, bool intoforeign, bool include, bool ignoreSynonyms) { // prepare test variables setMode(testMode); this->howmany = howmany; this->countdown = howmany; this->intoforeign = intoforeign; this->include = include; this->ignoreSynonyms = ignoreSynonyms; this->answered = true; this->hintsize = 1; this->caseSensitive = QSettings("dicto.ini", QSettings::IniFormat).value("testing/case_sensitive").value<bool>(); // reset all words to unpassed for(unsigned i=0; i<dictionarySize(); i++) cDocument->dictionary[i].passed = false; // prepare test queue testQueue.clear(); int i; for(i=0; i<howmany; i++) { int newWord = pickWord(include); if(newWord == -1) { setMode(normalMode); updateStatusbar(); break; } testQueue.enqueue(&cDocument->dictionary[newWord]); cDocument->dictionary[newWord].passed = true; } if(testQueue.empty()) { // not supposed to happen endTest(true, tr("No words match your criteria")); return; } check(); }
// invokes dictionary sort void WMain::sortall() { if(dictionarySize() > 2) cDocument->sortDictionary(); else { statusBar()->showMessage(tr("Dictionary is empty"), 10000); return; } }
// shows edit entry window void WMain::editentry() { if(dictionarySize() > 0 && currentRow()) { WDialog *wDialog=new WDialog(0, currentRow()); wDialog->show(); setMode(disabledMode); } else statusBar()->showMessage(tr("Entry not selected"), 10000); }
// shows print window void WMain::print() { if(dictionarySize() == 0) { statusBar()->showMessage(tr("Dictionary is empty"), 10000); return; } WPrint *wPrint = new WPrint(0); wPrint->show(); this->setMode(disabledMode); }
// deletes entry void WMain::deleteentry() { if(dictionarySize() > 0) { cDocument->dictionary.erase(currentRow()); cDocument->filechanged=true; updateList(); } else { statusBar()->showMessage(tr("Dictionary is empty"), 10000); return; } }
// updates word list void WMain::updateList() { QString search = searchBar->text(); listWidget->clear(); currentList.clear(); for(int i=0; i<dictionarySize(); i++) { QString word = cDocument->dictionary[i].word; QString translation = cDocument->dictionary[i].translation; QString result=translation+" - "+word; if(search==""||word.contains(search)||translation.contains(search)) { QListWidgetItem *newItem = new QListWidgetItem(processToNice(result, " | "), listWidget); listWidget->addItem(newItem); currentList.push_back(&cDocument->dictionary[i]); } } }