C++ (Qt) // ВАРИАНТ3 QHash<QString, int> frequencyHash; foreach (QString Item, itemList) ++frequencyHash[Item]; itemList.removeDuplicates(); QString IterW; QListIterator<QString> Iter(itemList); while(Iter.hasNext()) { IterW = Iter.next(); countItemFull = countItemFull + frequencyHash.value(IterW); countItemString.setNum(frequencyHash.value(IterW)); trimmedStr = IterW + "\t" + countItemString; frequencyList.prepend( trimmedStr ); }
C++ (Qt)QHash<QString, int> freqHash;foreach (QString s, itemList) ++freqHash[s];foreach (QString s, itemList) { int & num = freqHash[s]; if (num < 0) continue; frequencyList.prepend(s + '\t' + QString::Number(num)); num = -num;}
C++ (Qt)void MainWindow::bufferButtonSave(){ QTime tm; tm.start(); QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString countItemFullString; QString countItemString, countItemStringZero; QStringList itemList; QStringList frequencyList; int countItemFull(0); int minSizeWord(cfg.preferences.minSizeWord); int maxSizeWord(cfg.preferences.maxSizeWord); int minRepeats(cfg.preferences.minRepeats); qDebug() << "Reade Data: " << tm.elapsed(); tm.start(); originalText = originalText.toLower(); qDebug() << "Data toLower: " << tm.elapsed(); textory.clear(); tm.start(); // ОСТАВЛЯЕМ ТЕКСТ if(cfg.preferences.langStatSort == 0)itemList = originalText.split(QRegExp("[\\s\\W\\d]+"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ АНГЛИЙКИЙ if(cfg.preferences.langStatSort == 1)itemList = originalText.split(QRegExp("[^a-z]"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ РУССКИЙ if(cfg.preferences.langStatSort == 2)itemList = originalText.split(QRegExp(QString::fromUtf8("[^а-яё]")),QString::SkipEmptyParts); qDebug() << "Data split1: " << tm.elapsed(); tm.start(); if(cfg.preferences.typeOfSort == 2) QHash<QString, int> frequencyHash; foreach (QString Item, itemList) ++frequencyHash[Item]; itemList.removeDuplicates(); QString IterW; QListIterator<QString> Iter(itemList); while(Iter.hasNext()) { IterW = Iter.next(); countItemFull = countItemFull + frequencyHash.value(IterW); countItemString.setNum(frequencyHash.value(IterW)); trimmedStr = IterW + "\t" + countItemString; frequencyList.prepend( trimmedStr ); } if(cfg.preferences.typeOfSort == 0 || cfg.preferences.typeOfSort == 1) { QHash<QString, int> frequencyHash; foreach (QString Item, itemList) ++frequencyHash[Item]; qDebug() << "Data split2: " << tm.elapsed(); tm.start(); QHashIterator<QString,int> Iter(frequencyHash); while(Iter.hasNext()) { Iter.next(); countItemFull = countItemFull + Iter.value(); if(Iter.key().size() < minSizeWord || Iter.key().size() > maxSizeWord || Iter.value() < minRepeats )continue; countItemString.setNum(Iter.value()); countItemStringZero = countItemString; // ОРГАНИЗОВЫВАЕМ СОРТИРОВКУ countItemStringZero = QString("%1") .arg(countItemString, 6, '0'); if(cfg.preferences.typeOfSort == 0)frequencyList.append(countItemStringZero + "_" + Iter.key() + "\t" + countItemString ); if(cfg.preferences.typeOfSort == 1 || cfg.preferences.typeOfSort == 2)frequencyList.prepend(Iter.key() + "\t" + countItemString ); } } qDebug() << "Count: " << tm.elapsed(); tm.start(); // СТРОИМ СПИСОК if(cfg.preferences.typeOfSort != 2)qSort(frequencyList); textory.enableAdd( true ); qDebug() << "Data sort: " << tm.elapsed(); tm.start(); if(cfg.preferences.typeOfSort == 0 || cfg.preferences.typeOfSort == 2) { foreach (QString itm, frequencyList) { if(cfg.preferences.typeOfSort == 0)itm.remove(0,7); textory.addItem( Textory::Item( 1, itm ) ); } } if(cfg.preferences.typeOfSort == 1) { QListIterator<QString>itm(frequencyList); itm.toBack(); while (itm.hasPrevious()) textory.addItem( Textory::Item( 1, itm.previous() ) ); } qDebug() << "Clear zero: " << tm.elapsed(); tm.start(); countItemFullString.setNum(countItemFull); ui.textoryPaneWidget->getTextoryLabel().setText( "Frequency list (" + countItemFullString + ")" ); qDebug() << "Write name: " << tm.elapsed();}
C++ (Qt)QHash<QString, int> freqHash;freqHash.reserve(itemList.size()); // так должно быть быстрее (правда хз насколько)foreach (QString s, itemList) ++freqHash[s];
C++ (Qt)void MainWindow::bufferButtonSave(){ QTime tm; tm.start(); QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString countItemFullString; QString countItemString, countItemStringZero; QStringList itemList; QStringList frequencyList; int countItemFull(0); int minSizeWord(cfg.preferences.minSizeWord); int maxSizeWord(cfg.preferences.maxSizeWord); int minRepeats(cfg.preferences.minRepeats); qDebug() << "Reade Data: " << tm.elapsed(); tm.start(); originalText = originalText.toLower(); qDebug() << "Data toLower: " << tm.elapsed(); tm.start(); textory.clear(); // ОСТАВЛЯЕМ ТЕКСТ if(cfg.preferences.langStatSort == 0)itemList = originalText.split(QRegExp("[\\s\\W\\d]+"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ АНГЛИЙКИЙ if(cfg.preferences.langStatSort == 1)itemList = originalText.split(QRegExp("[^a-z]"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ РУССКИЙ if(cfg.preferences.langStatSort == 2)itemList = originalText.split(QRegExp(QString::fromUtf8("[^а-яё]")),QString::SkipEmptyParts); qDebug() << "Data split1: " << tm.elapsed(); tm.start(); // ВАРИАНТ БЕЗ СОРТИРОВКИ (frequencyList) if(cfg.preferences.typeOfSort == 2) { QHash<QString, int> frequencyHash; foreach (QString s, itemList) ++frequencyHash[s]; foreach (QString s, itemList) { int & num = frequencyHash[s]; countItemString.setNum(num); if (num < 0) continue; countItemFull = countItemFull + num; if(s.size() < minSizeWord || s.size() > maxSizeWord || frequencyHash[s] < minRepeats )continue; frequencyList.prepend(s + '\t' + countItemString); num = -num; } } // СОРТИРОВКА В ПОРЯДКЕ ЧАСТОТНОСТИ И АЛФАВИТУ if(cfg.preferences.typeOfSort == 0 || cfg.preferences.typeOfSort == 1) { QHash<QString, int> frequencyHash; foreach (QString Item, itemList) ++frequencyHash[Item]; QHashIterator<QString,int> Iter(frequencyHash); while(Iter.hasNext()) { Iter.next(); countItemFull = countItemFull + Iter.value(); if(Iter.key().size() < minSizeWord || Iter.key().size() > maxSizeWord || Iter.value() < minRepeats )continue; countItemString.setNum(Iter.value()); countItemStringZero = countItemString; // ОРГАНИЗОВЫВАЕМ СОРТИРОВКУ countItemStringZero = QString("%1") .arg(countItemString, 6, '0'); if(cfg.preferences.typeOfSort == 0)frequencyList.append(countItemStringZero + "_" + Iter.key() + "\t" + countItemString ); if(cfg.preferences.typeOfSort == 1 || cfg.preferences.typeOfSort == 2)frequencyList.append(Iter.key() + "\t" + countItemString ); } } qDebug() << "Count: " << tm.elapsed(); tm.start(); // СТРОИМ СПИСОК if(cfg.preferences.typeOfSort != 2)qSort(frequencyList); textory.enableAdd( true ); qDebug() << "Data sort: " << tm.elapsed(); tm.start(); if(cfg.preferences.typeOfSort == 0 || cfg.preferences.typeOfSort == 2) { foreach (QString itm, frequencyList) { if(cfg.preferences.typeOfSort == 0)itm.remove(0,7); // textory.addItem( Textory::Item( 1, itm ) ); } } if(cfg.preferences.typeOfSort == 1) { QListIterator<QString>itm(frequencyList); itm.toBack(); while (itm.hasPrevious()) // textory.addItem( Textory::Item( 1, itm.previous() ) ); } qDebug() << "Clear zero: " << tm.elapsed(); tm.start(); countItemFullString.setNum(countItemFull); ui.textoryPaneWidget->getTextoryLabel().setText( "Frequency list (" + countItemFullString + ")" ); qDebug() << "Write name: " << tm.elapsed();}