C++ (Qt)QHash<QString, int> freqHash;freqHash.reserve(itemList.size()); // так должно быть быстрее (правда хз насколько)foreach (QString s, itemList) ++freqHash[s];
C++ (Qt) // ВАРИАНТ БЕЗ СОРТИРОВКИ (frequencyList) if(cfg.preferences.typeOfSort == 2) { QHash<QString, int> frequencyHash; frequencyHash.reserve(itemList.size()); 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; } }
C++ (Qt) if(s.size() < minSizeWord || s.size() > maxSizeWord || frequencyHash[s] < minRepeats )continue;
C++ (Qt) frequencyList.reserve(itemList.size()); // ну "типа грамотно"foreach (QString s, itemList) { if (s.size() < minSizeWord || s.size() > maxSizeWord) continue; // сначала проверились по длине int & num = frequencyHash[s]; if (num < 0) continue; if (num >= minRepeats ) { countItemFull += num; frequencyList.prepend(s + '\t' + QString::number(long(num)); } num = -num;}
[quote author=Igors link=topic=25332.msg181725#msg181725 date=1375375776]Избегайте таких слишком длинных строк. И пробелы ставьте однообразно (напр после if).
C++ (Qt) // ВАРИАНТ БЕЗ СОРТИРОВКИ (frequencyList) if(cfg.preferences.typeOfSort == 2) { QHash<QString, int> frequencyHash; frequencyList.reserve(itemList.size()); foreach (QString s, itemList) { if(s.size() < minSizeWord || s.size() > maxSizeWord )continue; int & num = frequencyHash[s]; if (num < 0) continue; if(num >= minRepeats ) { countItemFull += num; frequencyList.prepend(s + '\t' + QString::number(long(num))); } num = -num; } }
C++ (Qt)void MainWindow::bufferButtonSave(){ QTime tm; tm.start(); QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString countItemFullString; QString 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); countItemFull = itemList.size(); qDebug() << "Data split: " << tm.elapsed(); tm.start(); // ВАРИАНТ БЕЗ СОРТИРОВКИ (frequencyList) if(cfg.preferences.typeOfSort == 2) { QHash<QString, int> frequencyHash; foreach (QString s, itemList) ++frequencyHash[s]; frequencyList.reserve(countItemFull); foreach (QString s, itemList) { if(s.size() < minSizeWord || s.size() > maxSizeWord )continue; int & num = frequencyHash[s]; if (num < 0) continue; if(num >= minRepeats ) { frequencyList.prepend(s + '\t' + QString::number(long(num))); } num = -num; } } // СОРТИРОВКА В ПОРЯДКЕ ЧАСТОТНОСТИ И АЛФАВИТУ if(cfg.preferences.typeOfSort == 0 || cfg.preferences.typeOfSort == 1) { QHash<QString, int> frequencyHash; foreach (QString Item, itemList) ++frequencyHash[Item]; frequencyList.reserve(countItemFull); QHashIterator<QString,int> Iter(frequencyHash); while(Iter.hasNext()) { Iter.next(); if(Iter.key().size() < minSizeWord || Iter.key().size() > maxSizeWord || Iter.value() < minRepeats )continue; countItemStringZero = QString::number(long(Iter.value())).rightJustified(6, '0'); if(cfg.preferences.typeOfSort == 0)frequencyList.append(countItemStringZero + Iter.key() + "\t" + QString::number(long(Iter.value())) ); if(cfg.preferences.typeOfSort == 1 || cfg.preferences.typeOfSort == 2)frequencyList.append(Iter.key() + "\t" + QString::number(long(Iter.value())) ); } } 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,6); 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)void MainWindow::bufferButtonSave(){ QTime tm; tm.start(); QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString countItemFullString; QString 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); countItemFull = itemList.size(); qDebug() << "Data split: " << tm.elapsed(); tm.start(); // СОРТИРОВКА (frequencyList) QHash<QString, int> frequencyHash; foreach (QString s, itemList) ++frequencyHash[s]; frequencyList.reserve(countItemFull); foreach (QString s, itemList) { if(s.size() < minSizeWord || s.size() > maxSizeWord )continue; int & num = frequencyHash[s]; if (num < 0) continue; if(num >= minRepeats ) { if(cfg.preferences.typeOfSort == 2)frequencyList.prepend(s + '\t' + QString::number(long(num))); if(cfg.preferences.typeOfSort == 1)frequencyList.prepend(s + '\t' + QString::number(long(num))); if(cfg.preferences.typeOfSort == 0) { countItemStringZero = QString::number(long(num)).rightJustified(6, '0'); frequencyList.prepend(countItemStringZero + s + '\t' + QString::number(long(num))); } } num = -num; } 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,6); 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)void MainWindow::wordStat(){ QTime tm; tm.start(); QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString addZero; QStringList listFull; QStringList frequencyList; qDebug() << "Reade Data: " << tm.elapsed(); tm.start(); textory.clear(); textory.enableAdd( true ); // ОСТАВЛЯЕМ ТЕКСТ if(cfg.preferences.langStatSort == 0)listFull = originalText.toLower().split(QRegExp("[\\s\\W\\d_]+"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ АНГЛИЙКИЙ if(cfg.preferences.langStatSort == 1)listFull = originalText.toLower().split(QRegExp("[^a-z]+"),QString::SkipEmptyParts); // ОСТАВЛЯЕМ РУССКИЙ if(cfg.preferences.langStatSort == 2)listFull = originalText.toLower().split(QRegExp(QString::fromUtf8("[^а-яё]+")),QString::SkipEmptyParts); qDebug() << "Data split: " << tm.elapsed(); tm.start(); // ПОСТРОЕНИЕ СПИСКА QRegExp rx(muRegExp); rx.setPatternSyntax(QRegExp::Wildcard); QHash<QString, int> frequencyHash; foreach (QString s, listFull) ++frequencyHash[s]; frequencyList.reserve(listFull.size()); foreach (QString s, listFull) { if(s.size() < cfg.preferences.minSizeWord || s.size() > cfg.preferences.maxSizeWord )continue; if(!muRegExp.isEmpty() && !rx.exactMatch(s))continue; int & num = frequencyHash[s]; if(num < 0) continue; if(num >= cfg.preferences.minRepeats ) { if(cfg.preferences.typeOfSort == 2)frequencyList.prepend(s + '\t' + QString::number(long(num))); if(cfg.preferences.typeOfSort == 1)frequencyList.prepend(s + '\t' + QString::number(long(num))); if(cfg.preferences.typeOfSort == 0) { addZero = QString::number(long(num)).rightJustified(6, '0'); frequencyList.prepend(addZero + s + '\t' + QString::number(long(num))); } } num = -num; } qDebug() << "Count: " << tm.elapsed(); tm.start(); if(cfg.preferences.typeOfSort != 2)qSort(frequencyList); // ВЫВОД СПИСКА 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,6); 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() << "Out list: " << tm.elapsed(); tm.start(); ui.textoryPaneWidget->getTextoryLabel().setText( "Frequency list (" + QString::number(long(listFull.size())) + ")" ); qDebug() << "Write name: " << tm.elapsed();} void MainWindow::wordStatWidget(){ WordStatDialog *dialog = new WordStatDialog( cfg ); dialog->setWindowFlags(Qt::WindowStaysOnTopHint); connect(dialog->addFromClipboardButton, SIGNAL(clicked()), this, SLOT(wordStat())); connect(dialog->setWildcardLineEdit, SIGNAL(textChanged(QString)), this, SLOT(regExpChange(QString))); dialog->show();} void MainWindow::regExpChange(QString regExp){ muRegExp = regExp;}
C++ (Qt)#ifndef WORDSTAT_HH#define WORDSTAT_HH #include <QDialog> #include "config.hh"#include "mainwindow.hh" class QLabel;class QSpinBox;class QComboBox;class QPushButton; class WordStatDialog : public QDialog{ Q_OBJECT public: WordStatDialog( Config::Class &cfg_, QWidget * parent = 0 ); QPushButton *addFromClipboardButton; QLineEdit *setWildcardLineEdit; private slots: void addFromClipboardButtonClicked(); void setMinValue(int); void setMinSizeWordButtonClicked(); void setMaxSizeWordButtonClicked(); private: Config::Class &cfg; QLabel *setMinSizeWordLabel; QLabel *setMaxSizeWordLabel; QLabel *setSizeLabel; QLabel *setMinRepeatsLabel; QLabel *setLanguageLabel; QLabel *setTypeOfSortLabel; QLabel *setWildcardLabel; QSpinBox *setMinSizeWordSpinBox; QSpinBox *setMaxSizeWordSpinBox; QSpinBox *setMinRepeatsSpinBox; QPushButton *setMinSizeWordButton; QPushButton *setMaxSizeWordButton; QComboBox *setLanguageComboBox; QComboBox *setTypeOfSortComboBox; }; #endif // WORDSTAT_HH
C++ (Qt)#include <QtGui> #include "wordstat.hh" WordStatDialog::WordStatDialog( Config::Class &cfg_, QWidget *parent ) : QDialog( parent ), cfg(cfg_) { setWindowTitle(tr("WordStat")); setMinimumWidth(210); setMinSizeWordLabel = new QLabel(tr("Min Of Letters:")); setMinSizeWordSpinBox = new QSpinBox(); setMinSizeWordSpinBox->setValue(cfg.preferences.minSizeWord); setMinSizeWordSpinBox->setMinimum(1); setMinSizeWordSpinBox->setMaximum(99); setMaxSizeWordLabel = new QLabel(tr("Max Of Letters:")); setMaxSizeWordSpinBox = new QSpinBox(); setMaxSizeWordSpinBox->setValue(cfg.preferences.maxSizeWord); setMaxSizeWordSpinBox->setMinimum(setMinSizeWordSpinBox->value()); setMaxSizeWordSpinBox->setMaximum(99); connect(setMinSizeWordSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setMinValue(int))); setMinRepeatsLabel = new QLabel(tr("Min Repeats:")); setMinRepeatsSpinBox = new QSpinBox(); setMinRepeatsSpinBox->setValue(cfg.preferences.minRepeats); setMinRepeatsSpinBox->setMinimum(1); setMinRepeatsSpinBox->setMaximum(999); setLanguageLabel = new QLabel(tr("Language:")); setLanguageComboBox = new QComboBox(); setLanguageComboBox->addItem("All Text"); setLanguageComboBox->addItem("English"); setLanguageComboBox->addItem("Russian"); setLanguageComboBox->setCurrentIndex(cfg.preferences.langStatSort); setTypeOfSortLabel = new QLabel(tr("Type Of Sort:")); setTypeOfSortComboBox = new QComboBox(); setTypeOfSortComboBox->addItem("Amount"); setTypeOfSortComboBox->addItem("Alphabet"); setTypeOfSortComboBox->addItem("No Sort"); setTypeOfSortComboBox->setCurrentIndex(cfg.preferences.typeOfSort); setWildcardLabel = new QLabel(tr("Wildcard:")); setWildcardLineEdit = new QLineEdit(); setWildcardLabel->setBuddy(setWildcardLineEdit); setSizeLabel = new QLabel(tr("Set Size Word:")); setMinSizeWordButton = new QPushButton(tr("Min Size")); setMaxSizeWordButton = new QPushButton(tr("Max Size")); addFromClipboardButton = new QPushButton(tr("Clipboard")); QGridLayout *wordStatLayout = new QGridLayout; wordStatLayout->addWidget(setMinSizeWordLabel, 0, 0); wordStatLayout->addWidget(setMinSizeWordSpinBox, 0, 1, 1, 2); wordStatLayout->addWidget(setMaxSizeWordLabel, 1, 0); wordStatLayout->addWidget(setMaxSizeWordSpinBox, 1, 1, 1, 2); wordStatLayout->addWidget(setSizeLabel, 2, 0); wordStatLayout->addWidget(setMinSizeWordButton, 2, 1); wordStatLayout->addWidget(setMaxSizeWordButton, 2, 2); wordStatLayout->addWidget(setMinRepeatsLabel, 3, 0); wordStatLayout->addWidget(setMinRepeatsSpinBox, 3, 1, 1, 2); wordStatLayout->addWidget(setLanguageLabel, 4, 0); wordStatLayout->addWidget(setLanguageComboBox, 4, 1, 1, 2); wordStatLayout->addWidget(setTypeOfSortLabel, 5, 0, 1, 2); wordStatLayout->addWidget(setTypeOfSortComboBox, 5, 1, 1, 2); wordStatLayout->addWidget(setWildcardLabel, 6, 0); wordStatLayout->addWidget(setWildcardLineEdit, 6, 1, 1, 2); wordStatLayout->addWidget(addFromClipboardButton, 7, 0, 1, 3); QVBoxLayout *mainWordStatLayout = new QVBoxLayout; mainWordStatLayout->addLayout(wordStatLayout); setLayout(mainWordStatLayout); connect(addFromClipboardButton, SIGNAL(clicked()), this, SLOT(addFromClipboardButtonClicked())); connect(setMinSizeWordButton, SIGNAL(clicked()), this, SLOT(setMinSizeWordButtonClicked())); connect(setMaxSizeWordButton, SIGNAL(clicked()), this, SLOT(setMaxSizeWordButtonClicked()));} void WordStatDialog::setMinValue(int){ if(setMinSizeWordSpinBox->value() > setMaxSizeWordSpinBox->value()) setMaxSizeWordSpinBox->setValue(setMinSizeWordSpinBox->value()); setMaxSizeWordSpinBox->setMinimum(setMinSizeWordSpinBox->value());} void WordStatDialog::setMinSizeWordButtonClicked(){ setMaxSizeWordSpinBox->setValue(setMinSizeWordSpinBox->value());} void WordStatDialog::setMaxSizeWordButtonClicked(){ setMaxSizeWordSpinBox->setValue(99);} void WordStatDialog::addFromClipboardButtonClicked(){ if(setWildcardLineEdit->text().startsWith(" "))setWildcardLineEdit->clear(); cfg.preferences.minSizeWord = setMinSizeWordSpinBox->value(); cfg.preferences.maxSizeWord = setMaxSizeWordSpinBox->value(); cfg.preferences.minRepeats = setMinRepeatsSpinBox->value(); cfg.preferences.langStatSort = setLanguageComboBox->currentIndex(); cfg.preferences.typeOfSort = setTypeOfSortComboBox->currentIndex();}