void prune(Table **stringTable, int PRUNE, char ENCODE_OR_DECODE[25]) { int MAXBITS = (*stringTable)->MAXBITS; int len = (*stringTable)->last; Table *newStringTable = initStringTable(MAXBITS); Pair *pair; Pair *parent; int newTablePrefix = 0; // printTable(*stringTable, ENCODE_OR_DECODE); for (int i = RESERVED; i < len; ++i) { pair = (*stringTable)->table[i]; if(pair->useCount >= PRUNE) { parent = (*stringTable)->table[pair->prefix]; //get PARENT pair from oldTable newTablePrefix = parent->newTableIndex; //by PREFIX PROPERTY, that parent must exist in newTable, and it has been assigned a newTableIndex insertToTable(newTablePrefix, pair->c, &newStringTable, PRUNE); pair->newTableIndex = newStringTable->last; //in oldTable, assign this pair its newTableIndex so future children reference it properly } } (newStringTable->pruned)++; // printTable(newStringTable, ENCODE_OR_DECODE); (newStringTable->pruned)++; moses(*stringTable); (*stringTable) = newStringTable; }
void PrinterWidget::addToQueue(File* fPtr) { if (fPtr == NULL) { return; } const QString& name = fPtr->getName(); QString path = fPtr->getFullName(); QListWidget& lst = *ui->print_list; int item_num = insertToTable(path); QString suffix = "(" + QString::number(item_num) + ")"; path += suffix; QListWidgetItem* item = new QListWidgetItem( item_num == 1 ? name : name + suffix, &lst); item->setData(3,path); item->setFlags( item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(Qt::Unchecked); addCurrentState(path, *fPtr->getImage()); }
int main(int argc, char **argv) { clock_t begin, end; double time_spent; begin = clock(); int NUM_FINDCODE_CALLS = 0; int NUM_HASHCELLS_VISITED = 0; int c = 0; //char in (pref, char) int prefix = EMPTY_CODE; int index = EMPTY_CODE; int MAXBITS = 15; //default char DUMP_TO[PATH_MAX]; char INIT_FROM[PATH_MAX]; int PRUNE = -1; Table *stringTable = initStringTable(MAXBITS); //setting params from cmd line argv's STARTING FROM 2 BC ENCODE/DECODE for (int i = 2; i < argc; ++i) { if (strcmp(argv[i], "-m") == 0) { //ERROR CHECK FOR IF NEXT ARGV IS NOT INTEGER STRING??? MAXBITS = atoi(argv[i + 1]); i++; } else if (strcmp(argv[i], "-o") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); //EDGE CASE: IF -O -O HI, THEN DO WE DUMP TO '-O' AND SIGNAL ERROR ON HI? OR DO WE DUMP TO -O, AND OVERRIDE WITH DUMPING TO 'HI'? } strncpy(DUMP_TO, argv[i + 1], strlen(argv[i + 1])); i++; } else if (strcmp(argv[i], "-i") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } strncpy(INIT_FROM, argv[i + 1], strlen(argv[i + 1])); i++; } else if (strcmp(argv[i], "-p") == 0) { if(i + 1 == argc) { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } PRUNE = atoi(argv[i + 1]); //test if not an integer; what if prune is 0? w i++; if(PRUNE){} } else { fprintf(stderr, "usage: encode [-m MAXBITS | -o NAME | -i NAME | -p USED]* OR decode [-o NAME]*\n"); exit(EXIT_FAILURE); } } fprintf(stderr, "argv[0]: %s\n", argv[0]); if(strcmp(argv[1], "encode") == 0) { //ENCODE while((c = getchar()) != EOF) { index = findCode(prefix, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); if (index != -1) //(pref, char) found in stringTable { prefix = index; (stringTable->table[prefix]->useCount)++; continue; } else { //not found in stringTable putBits(stringTable->nBits, prefix); insertToTable(prefix, c, &stringTable, PRUNE); prefix = findCode(0, c, stringTable, &NUM_FINDCODE_CALLS, &NUM_HASHCELLS_VISITED); //start search again at finalChar of newly found sequence (stringTable->table[prefix]->useCount)++; } } putBits(stringTable->nBits, prefix); flushBits(); // printTable(stringTable, "encode"); // fprintf(stderr, "\nNUM_FINDCODE_CALLS: %d\nNUM_HASHCELLS_VISITED: %d\nAVG_SEARCH_RATIO: %d\n", NUM_FINDCODE_CALLS, NUM_HASHCELLS_VISITED, NUM_HASHCELLS_VISITED / NUM_FINDCODE_CALLS); } else if(strcmp(argv[1], "decode") == 0) { //DECODE char ENCODE_OR_DECODE[25] = "decode"; int oldIndex = 0, firstCharOfNewIndex = 0; //used to build stringTable, 1 step behind encode int newIndex = 0; int cascadingIndex = 0; //used to print a code string recursively int kwkwk = 0; //used in kwkwk case int prunedThisPass = 0; int HIT_THE_LIMIT = 0; while( (newIndex = cascadingIndex = getBits(stringTable->nBits)) != EOF) { //nBits was incremented just before to accomodate increased nBits in ENCODE that hasn't been automatically detected in DECODE //and we decrement it now bc it will be automatically incremented in INSERTTABLE //but if == MAXBITS, then we did not artificially increment nBits last round if(stringTable->last + 1 == stringTable->size && !HIT_THE_LIMIT ) {//2nd condition happens when table is already maxSize, in which case no more doubling or pruning happens if(!prunedThisPass) //when pruning results in an at-limit table stringTable->nBits--; if(stringTable->size != 1<<stringTable->nBits){ fprintf(stderr, "size: %d, nBits: %d\n", stringTable->size, stringTable->nBits); exit(EXIT_FAILURE); } else { } } if(newIndex > stringTable->last) //newIndex is an unknown code (kwkwk abnormal case) { kwkwk = 1; if(newIndex < stringTable->size) stringTable->table[newIndex]->useCount++; //useCount usually incremented in printRecursive function, which will not receive newIndex in this case cascadingIndex = oldIndex; } printRecursive(&cascadingIndex, stringTable); if (kwkwk == 1) { printf("%c", firstCharOfNewIndex); //output should be oldCode, oldCode, firstCharOfOldCode kwkwk = 0; } if(cascadingIndex >= stringTable->size) { fprintf(stderr, "cascadingIndex: %d, size: %d\n", cascadingIndex, stringTable->size); exit(EXIT_FAILURE); } firstCharOfNewIndex = stringTable->table[cascadingIndex]->c; //finalK = char(c) if(oldIndex != 0 && !prunedThisPass) { //every time except first time, and after pruning, add new code to table insertToTable(oldIndex, firstCharOfNewIndex, &stringTable, PRUNE); } prunedThisPass = 0; oldIndex = newIndex; if(stringTable->last + 1 >= stringTable->size) { //decode is one step behind encode; deocde inserts new code every time it reads a code, starting from 2nd code //encode adds a code at every code starting from 1st code. CURRENT CODE + firstChar of NEXT CODE was the last code //encode tried to add to table and PRUNED or DOUBLE'd //=>PRUNING or DOUBLING:: //if DOUBLING: next code will increment nBits //if PRUNING: next code is encoded from a PRUNED table, not CURRENT table (and we don't insert next round) if(stringTable->nBits != stringTable->MAXBITS) { if(stringTable->nBits == 8){ exit(EXIT_FAILURE); } stringTable->nBits++; } else { //next insert exceeds table size, and table size is MAXBITS size, so PRUNE HIT_THE_LIMIT = 1; if(PRUNE != -1) { prune(&stringTable, PRUNE, ENCODE_OR_DECODE); prunedThisPass = 1; if (stringTable->last + 1 == stringTable->size) { fprintf(stderr, "HERE!!! last: %d, size: %d\n", stringTable->last, stringTable->size); char temp[10] = "encode"; printTable(stringTable, temp); } if(stringTable->last + 1 != 1<<stringTable->MAXBITS) HIT_THE_LIMIT = 0; } } } } // printTable(stringTable, "decode"); } moses(stringTable); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; fprintf(stderr, "time_spent: %f\n", time_spent); }