////////////////////////////////////////////////////////////////////////////// // Service mouse button clicks to inc or dec the selected frequency ////////////////////////////////////////////////////////////////////////////// void CFreqCtrl::mousePressEvent(QMouseEvent * event) { QPoint pt = event->pos(); if (event->button() == Qt::LeftButton) { for (int i = m_DigStart; i < m_NumDigits; i++) { if (inRect(m_DigitInfo[i].dQRect, pt)) //if in i'th digit { if (m_LRMouseFreqSel) { incFreq(); } else { if (pt.y() < m_DigitInfo[i].dQRect.bottom()/2) //top half? incFreq(); else decFreq(); //bottom half } } } } else if (event->button() == Qt::RightButton) { for (int i = m_DigStart; i < m_NumDigits; i++) { if (inRect(m_DigitInfo[i].dQRect, pt)) //if in i'th digit { if (m_LRMouseFreqSel) { decFreq(); } else { if (pt.y() < m_DigitInfo[i].dQRect.bottom()/2) //top half? incFreq();//IncDigit(); else decFreq();//DecDigit(); //botom half } } } } }
///////////////////////////////////////////////////////////////////// // Mouse Wheel Event overrides ///////////////////////////////////////////////////////////////////// void CFreqCtrl::wheelEvent(QWheelEvent * event) { QPoint pt = event->pos(); int numDegrees = event->delta() / 8; int numSteps = numDegrees / 15; for (int i = m_DigStart; i < m_NumDigits; i++) { if (inRect(m_DigitInfo[i].dQRect, pt)) //if in i'th digit { if (numSteps > 0) incFreq(); else if (numSteps < 0) decFreq(); } } }
// Fari: // Posto que alpha = num_terminais = valor do primeiro non-terminal. // ** a primeira regla sería: alpha <-- left1,right1 // ** a segunda regla sería: alpha +1 <-- left2, right2 // por iso no ficheiro de reglas só garda <alpha> e despois: left1, right1, left2, right2,... // pois xa sabe que os valores alpha, alpha+1,... son contiguos ;) int repair (FILE *R) { int oid,id,cpos; Trecord *rec,*orec; Tpair pair; if (fwrite(&alph,sizeof(int),1,R) != 1) return -1; if (PRNC) prnC(); int i=0; //fari... solo para mostrar numero de iteracion double prev_ratio =100.0; while (n+1 > 0) { if (PRNR) prnRec(); oid = extractMax(&Heap); if (oid == -1) break; // the end!! orec = &Rec.records[oid]; cpos = orec->cpos; if (fwrite (&orec->pair,sizeof(Tpair),1,R) != 1) return -1; if (PRNP) { printf("Chosen pair %i = (",n); prnSym(orec->pair.left); printf(","); prnSym(orec->pair.right); printf(") (%i occs)\n",orec->freq); } while (cpos != -1) { int ant,sgte,ssgte; // replacing bc->e in abcd, b = cpos, c = sgte, d = ssgte if (C[cpos+1] < 0) sgte = -C[cpos+1]-1; else sgte = cpos+1; if ((sgte+1 < u) && (C[sgte+1] < 0)) ssgte = -C[sgte+1]-1; else ssgte = sgte+1; // remove bc from L if (L[cpos].next != -1) L[L[cpos].next].prev = -oid-1; orec->cpos = L[cpos].next; if (ssgte != u) // there is ssgte { // remove occ of cd pair.left = C[sgte]; pair.right = C[ssgte]; id = searchHash(Hash,pair); if (id != -1) // may not exist if purgeHeap'd { if (id != oid) decFreq (&Heap,id); // not to my pair! if (L[sgte].prev != NullFreq) //still exists(not removed) { rec = &Rec.records[id]; if (L[sgte].prev < 0) // this cd is head of its list rec->cpos = L[sgte].next; else L[L[sgte].prev].next = L[sgte].next; if (L[sgte].next != -1) // not tail of its list L[L[sgte].next].prev = L[sgte].prev; } } // create occ of ed pair.left = n; id = searchHash(Hash,pair); if (id == -1) // new pair, insert { id = insertRecord (&Rec,pair); rec = &Rec.records[id]; L[cpos].next = -1; } else { incFreq (&Heap,id); rec = &Rec.records[id]; L[cpos].next = rec->cpos; L[L[cpos].next].prev = cpos; } L[cpos].prev = -id-1; rec->cpos = cpos; } if (cpos != 0) // there is ant { // remove occ of ab if (C[cpos-1] < 0) { ant = -C[cpos-1]-1; if (ant == cpos) // sgte and ant clashed -> 1 hole ant = cpos-2; } else ant = cpos-1; pair.left = C[ant]; pair.right = C[cpos]; id = searchHash(Hash,pair); if (id != -1) // may not exist if purgeHeap'd { if (id != oid) decFreq (&Heap,id); // not to my pair! if (L[ant].prev != NullFreq) //still exists (not removed) { rec = &Rec.records[id]; if (L[ant].prev < 0) // this ab is head of its list rec->cpos = L[ant].next; else L[L[ant].prev].next = L[ant].next; if (L[ant].next != -1) // it is not tail of its list L[L[ant].next].prev = L[ant].prev; } } // create occ of ae pair.right = n; id = searchHash(Hash,pair); if (id == -1) // new pair, insert { id = insertRecord(&Rec,pair); rec = &Rec.records[id]; L[ant].next = -1; } else { incFreq (&Heap,id); rec = &Rec.records[id]; L[ant].next = rec->cpos; L[L[ant].next].prev = ant; } L[ant].prev = -id-1; rec->cpos = ant; } C[cpos] = n; if (ssgte != u) C[ssgte-1] = -cpos-1; C[cpos+1] = -ssgte-1; c--; orec = &Rec.records[oid]; // just in case of Rec.records realloc'd cpos = orec->cpos; } if (PRNC) prnC(); removeRecord (&Rec,oid); n++; purgeHeap(&Heap); // remove freq 1 from heap if (c < factor * u) // compact C { int i,ni; i = 0; for (ni=0;ni<c-1;ni++) { C[ni] = C[i]; L[ni] = L[i]; if (L[ni].prev < 0) { if (L[ni].prev != NullFreq) // real ptr Rec.records[-L[ni].prev-1].cpos = ni; } else L[L[ni].prev].next = ni; if (L[ni].next != -1) L[L[ni].next].prev = ni; i++; if (C[i] < 0) i = -C[i]-1; } C[ni] = C[i]; u = c; C = realloc (C, c * sizeof(int)); L = realloc (L, c * sizeof(Tlist)); assocRecords (&Rec,&Hash,&Heap,L); } //*fari*/ i++; //fari... para mostrar numero de iteracion if ( ( i<10) || (! ((i-1)%100)) ) printf("Repair.compress: It: %d compressed: %3.15f %% |c| = %d , diff prev iter = %2.15f\n", i, 100.0*c/INI_c, c, (prev_ratio - (100.0*c/INI_c)) ); //cout << "Repair.compress: It: " << i++ << " compressed: " << 100.*c/INI_c << "%" << " |c|=" << c << endl; //if ((100.*m/n)<43.0) break; //if ((100.0*c/INI_c)< CORTE_REPAIR) break; //if ( ((prev_ratio - (100.0*c/INI_c)) < 0.00000009 ) || ((100.0*c/INI_c)< CORTE_REPAIR) ) break; //if ( ((prev_ratio - (100.0*c/INI_c)) < 0.000000005 ) || (((prev_ratio - (100.0*c/INI_c)) < CORTE_REPAIR) ) ) if (((prev_ratio - (100.0*c/INI_c)) < CORTE_REPAIR) ) break; prev_ratio = (100.0*c/INI_c); } //while printf("Repair.compress: It: %d compressed: %3.15f %% |c| = %d , diff prev iter = %2.15f \n", i, 100.0*c/INI_c, c, (prev_ratio - (100.0*c/INI_c)) ); //printf("Repair.compress: It: %d compressed: %3.8f %% |c| = %d \n", i, 100.0*c/INI_c, c); return 0; }
///////////////////////////////////////////////////////////////////// // Keyboard Event overrides ///////////////////////////////////////////////////////////////////// void CFreqCtrl::keyPressEvent( QKeyEvent * event ) { //call base class if dont over ride key bool fSkipMsg = FALSE; qint64 tmp; //qDebug() <<event->key(); switch(event->key()) { case Qt::Key_0: case Qt::Key_1: case Qt::Key_2: case Qt::Key_3: case Qt::Key_4: case Qt::Key_5: case Qt::Key_6: case Qt::Key_7: case Qt::Key_8: case Qt::Key_9: if (m_ActiveEditDigit >= 0) { if (m_DigitInfo[m_ActiveEditDigit].editmode) { tmp = (m_freq/m_DigitInfo[m_ActiveEditDigit].weight) % 10; m_freq -= tmp*m_DigitInfo[m_ActiveEditDigit].weight; m_freq = m_freq+(event->key()-'0')*m_DigitInfo[m_ActiveEditDigit].weight; setFrequency(m_freq); } } moveCursorRight(); fSkipMsg = TRUE; break; case Qt::Key_Left: if (m_ActiveEditDigit != -1) { moveCursorLeft(); fSkipMsg = TRUE; } break; case Qt::Key_Up: if (m_ActiveEditDigit != -1) { incFreq(); fSkipMsg = TRUE; } break; case Qt::Key_Down: if (m_ActiveEditDigit != -1) { decFreq(); fSkipMsg = TRUE; } break; case Qt::Key_Right: if (m_ActiveEditDigit != -1) { moveCursorRight(); fSkipMsg = TRUE; } break; case Qt::Key_Home: cursorHome(); fSkipMsg = TRUE; break; case Qt::Key_End: cursorEnd(); fSkipMsg = TRUE; break; default: break; } if (!fSkipMsg) QFrame::keyPressEvent(event); }